The answer can be simplified down to:
- if there is any number in
numssuch thatnums[i] < k, return-1- else, return the number of unique integers inside
numsminus1
code
class Solution {
public:
int minOperations(vector<int>& nums, int k) {
int mp[101] = {0};
int ans = 0;
for (int num : nums) {
if (num < k) return -1;
if (mp[num] == 0) {
++ans;
mp[num] = 1;
}
}
if (mp[k] == 0) ++ans;
return ans-1;
}
};
complexity
- Time:
O(n)- Space:
O(n)