#3354
Make Array Elements Equal to Zero
pupil · 325 · lc easy +22 · 68.3% accepted · 554 likes · top 75%
Description
You are given an integer array nums.
Choose a starting position curr where nums[curr] == 0 and pick a direction: left or right.
Then repeat:
- If curr is outside [0, n - 1], stop.
- If nums[curr] == 0, step in the current direction (increment curr if moving right, decrement if moving left).
- If nums[curr] > 0:
- Decrement nums[curr] by 1.
- Reverse your direction.
- Step in the new direction.
A (starting position, direction) pair is valid if every element of nums becomes 0 by the end of the process.
Return the number of valid selections.
Code
1
2
3