Minimum number of operations above threshold I
title
Given an array of integers indexed starting from 0 nums and an integer k. In each operation you can delete nums the smallest element in . The goal is to make all elements in the array greater than or equal to the minimum number of operations k. Needs to return the minimum number of operations required to achieve this goal.
Example
Example 1:
- Input:
nums = [2, 11, 10, 1, 3], k = 10 - Output:
3 - Explanation:
- After the first operation,
numsbecome[2, 11, 10, 3]。 - After the second operation,
numsbecome[11, 10, 3]。 - After the third operation,
numsbecome[11, 10]。 - At this point, all elements in the array are greater than or equal to
10, so stop the operation. - Make all elements in the array greater than or equal to
10The minimum number of operations required is3。
- After the first operation,
Example 2:
- Input:
nums = [1, 1, 2, 4, 9], k = 1 - Output:
0 - Explanation: All elements in the array are greater than or equal to
1, so there is no need tonumsDo any operation.
Example 3:
- Input:
nums = [1, 1, 2, 4, 9], k = 9 - Output:
4 - Explanation:
numsOnly one element in is greater than or equal to9, so it is necessary to execute4operations.
Tips
1 <= nums.length <= 501 <= nums[i] <= 10^91 <= k <= 10^9- The input is guaranteed to satisfy at least one
nums[i] >= ksubscriptiexists.
Problem-solving ideas
To solve this problem, we first need to nums to sort. After sorting, the smallest element in the array will be at the beginning of the array. Starting from the smallest end of the array, each time it encounters a value less than k element, delete it and increase the operation counter by one. When encountering the first greater than or equal to k of elements, stop the deletion operation.
Given an array of integers starting from 0 nums and an integer k, the purpose is to make all elements in the array greater than or equal to the smallest number of operations k. The operation is defined as removing the smallest element from the array.
Algorithm steps
- pair array
numsto sort. - The initialization operation counter is
0。 - Iterate over the sorted array, for each item less than
kelement, incrementing the operation count. - When encountering the first greater than or equal to
kof elements, stop traversing. - Returns the operation count.
solution
Algorithm ideas
- sort array: Sort the array first, this ensures that we always remove the smallest element when performing an operation.
- counting operation: Starting from the smallest end of the array, for each item less than
kelement, we remove it and increment the operation counter. - Stop condition: When all remaining elements in the array are greater than or equal to
kwhen, the operation ends.
Algorithm steps
- pair array
numsto sort. - Initialize an operation counter.
- Iterate over the sorted array, for each item less than
kelement, incrementing the operation count. - When encountering the first greater than or equal to
kof elements, stop traversing. - Returns the operation count.
Code implementation
1 | def min_operations(nums, k): |
test case
1 | print(min_operations([2, 11, 10, 1, 3], 10)) # 应输出: 3 |
Complexity analysis
- time complexity:O(n log n), mainly determined by the sorting step.
- space complexity: O(n) or O(1), depending on the sorting algorithm used. If an in-place sorting algorithm is used, the space complexity can be reduced to O(1).
Conclusion
This solution effectively finds all elements in the array that become greater than or equal to k Minimum number of operations required. It is suitable for different input scenarios and performs well in terms of time and space efficiency.