Classic problem #2357EasyArrayHash TableGreedySortingHeap (Priority Queue)Simulation
Make Array Zero by Subtracting Equal Amounts
Real-world version: Rounds Needed to Clear Every ReadingTracked in this browser only
Read Make Array Zero by Subtracting Equal Amounts on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
Readings are given as nums.
One round picks a number k above zero, no larger than the smallest reading that is still above zero, and takes k off every reading that is above zero.
Return the fewest rounds that leave every reading at zero.
Examples
Example 1
- Input
- nums = [14, 3, 27, 3, 9, 0]
- Output
- 4
The distinct readings above zero are 14, 3, 27 and 9, so four rounds are needed: taking off 3, then 6, then 5, then 13.
Example 2
- Input
- nums = [5, 5, 5, 5]
- Output
- 1
All four readings are the same, so one round of five clears them together.
Example 3
- Input
- nums = [0, 0, 0]
- Output
- 0
Everything already reads zero, so no rounds are needed.
Constraints
- 1 <= nums.length <= 100
- 0 <= nums[i] <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def minimum_operations(nums: list[int]) -> int:Java
public int minimumOperations(int[] nums)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's