3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution

In this guide, you will get 3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Exceed Threshold Value I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Exceed Threshold Value I solution in C++
  4. Minimum Operations to Exceed Threshold Value I solution in Java
  5. Minimum Operations to Exceed Threshold Value I solution in Python
  6. Additional Resources
3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: nums = [2,11,10,1,3], k = 10
Output: 3
Explanation: After one operation, nums becomes equal to [2, 11, 10, 3].
After two operations, nums becomes equal to [11, 10, 3].
After three operations, nums becomes equal to [11, 10].
At this stage, all the elements of nums are greater than or equal to 10 so we can stop.
It can be shown that 3 is the minimum number of operations needed so that all elements of the array are greater than or equal to 10.

Example 2:

Input: nums = [1,1,2,4,9], k = 1
Output: 0
Explanation: All elements of the array are greater than or equal to 1 so we do not need to apply any operations on nums.
Example 3:

See also  1671. Minimum Number of Removals to Make Mountain Array LeetCode Solution

Input: nums = [1,1,2,4,9], k = 9
Output: 4
Explanation: only a single element of nums is greater than or equal to 9 so we need to apply the operations 4 times on nums.

Constraints:

1 <= nums.length <= 50
1 <= nums[i] <= 109
1 <= k = k.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution in C++

class Solution {
 public:
  int minOperations(vector<int>& nums, int k) {
    return ranges::count_if(nums, [k](int num) { return num < k; });
  }
};
/* code provided by PROGIEZ */

3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution in Java

class Solution {
  public int minOperations(int[] nums, int k) {
    return (int) Arrays.stream(nums).filter(num -> num < k).count();
  }
}
// code provided by PROGIEZ

3065. Minimum Operations to Exceed Threshold Value I LeetCode Solution in Python

class Solution:
  def minOperations(self, nums: list[int], k: int) -> int:
    return sum(num < k for num in nums)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.