[LeetCode] Make Array Zero by Subtracting Equal Amounts

2357. Make Array Zero by Subtracting Equal Amounts

You are given a non-negative integer array nums. In one operation, you must:

  • Choose a positive integer x such that x is less than or equal to the smallest non-zero element in nums.
  • Subtract x from every positive element in nums.

Return the minimum number of operations to make every element in nums equal to 0.

1
2
3
4
5
6
7
8
class Solution {
public:
int minimumOperations(vector<int>& nums) {
unordered_set<int> us(begin(nums), end(nums));
return us.size() - us.count(0);
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/31/PS/LeetCode/make-array-zero-by-subtracting-equal-amounts/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.