Show problems as
Classic problem #2357EasyArrayHash TableGreedySortingHeap (Priority Queue)Simulation

Make Array Zero by Subtracting Equal Amounts

Real-world version: Rounds Needed to Clear Every Reading
Tracked 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)