File: make-array-zero-by-subtracting-equal-amounts/solution.py

Date: 2026-06-06

Time: 17:30

make-array-zero-by-subtracting-equal-amounts/solution.py

Purpose

This file solves LeetCode 2357: Make Array Zero by Subtracting Equal Amounts. It belongs to a repository of LeetCode solutions, each in its own directory with a standard layout (solution.py, test_solution.py, plan.md, review.md).

The problem: given a non-negative integer array, in each operation you choose the smallest positive element x and subtract x from every positive element. Return how many operations it takes to make the entire array zero.

Key Components

Solution.minOperations(self, nums: List[int]) -> int — The single method. It computes the answer in O(n) time and O(n) space with a one-liner:


return len(set(nums) - {0})

This converts nums to a set (deduplicating values), removes 0, and returns the count of remaining distinct values.

Patterns

Set-based reduction — The solution recognizes that each operation eliminates exactly one distinct positive value from the array. When you subtract the current minimum positive value, all elements equal to that minimum become zero, and the relative differences between larger elements are preserved. So the total number of operations equals the number of distinct positive values.

This is a common LeetCode pattern: translating a simulation problem into a counting/set problem by identifying what each operation actually eliminates.

Dependencies

Imports: List from typing — used only for the type annotation on the method signature.

Imported by: make-array-zero-by-subtracting-equal-amounts/test_solution.py imports the Solution class. The large "Imported By" list in the prompt is an artifact of the repository tooling and reflects test files across the entire repo, not direct consumers of this module.

Flow

1. set(nums) — deduplicate all values → O(n)

2. - {0} — set difference removes zero → O(1)

3. len(...) — count remaining distinct positive values → O(1)

No loops, no mutation, no intermediate state.

Invariants

Error Handling

None. The method trusts the caller to pass a valid List[int]. An empty list returns 0 (correct). No exceptions are raised or caught.

Topics to Explore

Beliefs