3354. Make Array Elements Equal to Zero Easy

@problem@discussion
#Array#Simulation#Prefix Sum



1/**
2 * [3354] Make Array Elements Equal to Zero
3 *
4 * You are given an integer array nums.
5 * Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement direction of either left or right.
6 * After that, you repeat the following process:
7 * 
8 * 	If curr is out of the range [0, n - 1], this process ends.
9 * 	If nums[curr] == 0, move in the current direction by incrementing curr if you are moving right, or decrementing curr if you are moving left.
10 * 	Else if nums[curr] > 0:
11 * 	
12 * 		Decrement nums[curr] by 1.
13 * 		Reverse your movement direction (left becomes right and vice versa).
14 * 		Take a step in your new direction.
15 * 	
16 * 	
17 * 
18 * A selection of the initial position curr and movement direction is considered valid if every element in nums becomes 0 by the end of the process.
19 * Return the number of possible valid selections.
20 *  
21 * <strong class="example">Example 1:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [1,0,2,0,3]</span>
24 * Output: <span class="example-io">2</span>
25 * Explanation:
26 * The only possible valid selections are the following:
27 * 
28 * 	Choose curr = 3, and a movement direction to the left.
29 * 	
30 * 		[1,0,2,<u>0</u>,3] -> [1,0,<u>2</u>,0,3] -> [1,0,1,<u>0</u>,3] -> [1,0,1,0,<u>3</u>] -> [1,0,1,<u>0</u>,2] -> [1,0,<u>1</u>,0,2] -> [1,0,0,<u>0</u>,2] -> [1,0,0,0,<u>2</u>] -> [1,0,0,<u>0</u>,1] -> [1,0,<u>0</u>,0,1] -> [1,<u>0</u>,0,0,1] -> [<u>1</u>,0,0,0,1] -> [0,<u>0</u>,0,0,1] -> [0,0,<u>0</u>,0,1] -> [0,0,0,<u>0</u>,1] -> [0,0,0,0,<u>1</u>] -> [0,0,0,0,0].
31 * 	
32 * 	
33 * 	Choose curr = 3, and a movement direction to the right.
34 * 	
35 * 		[1,0,2,<u>0</u>,3] -> [1,0,2,0,<u>3</u>] -> [1,0,2,<u>0</u>,2] -> [1,0,<u>2</u>,0,2] -> [1,0,1,<u>0</u>,2] -> [1,0,1,0,<u>2</u>] -> [1,0,1,<u>0</u>,1] -> [1,0,<u>1</u>,0,1] -> [1,0,0,<u>0</u>,1] -> [1,0,0,0,<u>1</u>] -> [1,0,0,<u>0</u>,0] -> [1,0,<u>0</u>,0,0] -> [1,<u>0</u>,0,0,0] -> [<u>1</u>,0,0,0,0] -> [0,0,0,0,0].
36 * 	
37 * 	
38 * </div>
39 * <strong class="example">Example 2:
40 * <div class="example-block">
41 * Input: <span class="example-io">nums = [2,3,4,0,4,1,0]</span>
42 * Output: <span class="example-io">0</span>
43 * Explanation:
44 * There are no possible valid selections.
45 * </div>
46 *  
47 * Constraints:
48 * 
49 * 	1 <= nums.length <= 100
50 * 	0 <= nums[i] <= 100
51 * 	There is at least one element i where nums[i] == 0.
52 * 
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/make-array-elements-equal-to-zero/
57// discuss: https://leetcode.com/problems/make-array-elements-equal-to-zero/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61impl Solution {
62    pub fn count_valid_selections(nums: Vec<i32>) -> i32 {
63        0
64    }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_3354() {
75    }
76}
77


Back
© 2025 bowen.ge All Rights Reserved.