[LeetCode] Minimum Operations to Exceed Threshold Value I

3065. Minimum Operations to Exceed Threshold Value I

You are given a 0-indexed integer array nums, and an integer k.

In one operation, you can remove one occurrence of the smallest element of nums.

Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int minOperations(vector<int>& A, int k) {
sort(begin(A), end(A));
for(int i = 0; i < A.size(); i++) {
if(A[i] >= k) return i;
}
return A.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/03/03/PS/LeetCode/minimum-operations-to-exceed-threshold-value-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.