You are given an integer array nums of non-negative integers. In one operation you choose a positive integer x that is less than or equal to the smallest non-zero element of nums, and subtract x from every non-zero element of nums.
Return the minimum number of operations required to reduce all elements of nums to zero.
Function Signature
def minimum_operations(nums: list[int]) -> int:
Rules
-
Elements equal to
0
are left unchanged by every operation.
-
The subtraction applies to all non-zero elements at once. Because
x
never exceeds the smallest non-zero element, no element becomes negative.
-
If every element is already
0
, return
0
.
Constraints
-
1 <= len(nums) <= 10^5
-
0 <= nums[i] <= 10^9
Examples
Input: nums = [1,5,0,3,5]
Output: 3
Choose x = 1 to get [0,4,0,2,4], then x = 2 to get [0,2,0,0,2], then x = 2 again to get [0,0,0,0,0].
Input: nums = [0]
Output: 0
The array is already all zeros, so no operation is needed.
Input: nums = [4,4,4]
Output: 1
Choosing x = 4 turns every element into 0 in a single operation.