Problem
You are given a non-negative integer array nums. In one operation, you must:
- Choose a positive integer
xsuch thatxis less than or equal to the smallest non-zero element innums. - Subtract
xfrom every positive element innums.
Return the minimum number of operations to make every element in nums equal to 0.
Examples
Example 1:
| |
Example 2:
| |
Solution
Method 1 - Using Set
Observation
- Identical elements remain identical, so → Deduplicate them.
- Distinct elements remain distinct until zero, so → Count the unique elements.
Explanation
Return the count of distinct positive elements.
Code
| |
Complexity
- ⏰ Time complexity:
O(n) - 🧺 Space complexity:
O(n)