Skip to content

Commit 6bdffaa

Browse files
authored
Create minimum-operations-to-collect-elements.cpp
1 parent 9f27845 commit 6bdffaa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(k)
3+
4+
// hash table
5+
class Solution {
6+
public:
7+
int minOperations(vector<int>& nums, int k) {
8+
vector<bool> lookup(k);
9+
int i = size(nums) - 1;
10+
for (; i >= 0; --i) {
11+
if (nums[i] > size(lookup) || lookup[nums[i] - 1]) {
12+
continue;
13+
}
14+
lookup[nums[i] - 1] = true;
15+
if (!--k) {
16+
break;
17+
}
18+
}
19+
return size(nums) - i;
20+
}
21+
};

0 commit comments

Comments
 (0)