Skip to content

Commit d202d37

Browse files
authored
Create Minimum Operations to Collect Elements.java
1 parent 88f4c84 commit d202d37

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int minOperations(List<Integer> nums, int k) {
3+
boolean[] found = new boolean[k];
4+
int foundCount = 0;
5+
for (int i = nums.size() - 1; i >= 0; i--) {
6+
if (nums.get(i) <= k && !found[nums.get(i) - 1]) {
7+
found[nums.get(i) - 1] = true;
8+
foundCount++;
9+
}
10+
if (foundCount == k) {
11+
return nums.size() - i;
12+
}
13+
}
14+
return -1;
15+
}
16+
}

0 commit comments

Comments
 (0)