-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
41 lines (31 loc) · 940 Bytes
/
Solution.java
File metadata and controls
41 lines (31 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package leetcode.minimumNumberOfOperationsToMakeElementsInArrayDistinct;
import java.util.HashSet;
import java.util.Set;
class Solution {
public int minimumOperations(int[] nums) {
if(nums.length <= 1) return 0;
int start = 0;
int end = nums.length - 1;
int qtdOperations = 0;
while(start <= end){
Set<Integer> numbers = new HashSet<>();
for(int i = start ; i <= end; i++){
int num = nums[i];
if(!numbers.contains(num)){
numbers.add(num);
continue;
}
numbers = new HashSet<>();
break;
}
if(numbers.isEmpty()){
qtdOperations++;
start += 3;
continue;
} else {
return qtdOperations;
}
}
return qtdOperations;
}
}