Skip to content
Discussion options

You must be logged in to vote

We want to find the minimum number of operations to make all elements in the array nums equal to zero.
The key observation is that we only need to count the number of increasing “steps” in the array when we move from left to right.

Approach:

  • In each operation, we can select a subarray and make the minimum non-zero element in it zero.
  • So, every time the array increases to a new positive height, we need a new operation to handle that value later.
  • When the array decreases or remains the same, we don’t need to increment the operation count, because smaller values (or zeros) can be handled in the same or earlier operations.

Thus, we can use a monotonic stack to track the increasing sequence …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Nov 10, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Nov 10, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants