Skip to content

2357. Make Array Zero by Subtracting Equal Amounts 👍

  • Time: $O(n)$
  • Space: $O(n)$
1
2
3
4
5
6
7
class Solution {
 public:
  int minimumOperations(vector<int>& nums) {
    unordered_set<int> seen(nums.begin(), nums.end());
    return seen.size() - seen.contains(0);
  }
};
1
2
3
4
5
6
class Solution {
  public int minimumOperations(int[] nums) {
    Set<Integer> seen = Arrays.stream(nums).boxed().collect(Collectors.toSet());
    return seen.size() - (seen.contains(0) ? 1 : 0);
  }
}
1
2
3
class Solution:
  def minimumOperations(self, nums: list[int]) -> int:
    return len(set(nums) - {0})