-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3354-MakeArrayElementsEqualToZero.go
More file actions
107 lines (93 loc) · 4.22 KB
/
3354-MakeArrayElementsEqualToZero.go
File metadata and controls
107 lines (93 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
// 3354. Make Array Elements Equal to Zero
// You are given an integer array nums.
// Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement direction of either left or right.
// After that, you repeat the following process:
// 1. If curr is out of the range [0, n - 1], this process ends.
// 2. 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.
// 3. Else if nums[curr] > 0:
// 3.1 Decrement nums[curr] by 1.
// 3.2 Reverse your movement direction (left becomes right and vice versa).
// 3.3 Take a step in your new direction.
// 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.
// Return the number of possible valid selections.
// Example 1:
// Input: nums = [1,0,2,0,3]
// Output: 2
// Explanation:
// The only possible valid selections are the following:
// Choose curr = 3, and a movement direction to the left.
// [1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].
// Choose curr = 3, and a movement direction to the right.
// [1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].
// Example 2:
// Input: nums = [2,3,4,0,4,1,0]
// Output: 0
// Explanation:
// There are no possible valid selections.
// Constraints:
// 1 <= nums.length <= 100
// 0 <= nums[i] <= 100
// There is at least one element i where nums[i] == 0.
import "fmt"
func countValidSelections(nums []int) int {
res, curr, sum := 0, 0, 0
for _, v := range nums {
sum += v
}
abs := func(x int) int { if x < 0 { return -x; }; return x; }
for _, v := range nums {
if v == 0 {
s := abs(curr * 2 - sum)
if s == 0 { // both left and right direction for moving is okay
res += 2
} else if s == 1 { // either move left or right
res++
}
}
curr += v
}
return res
}
func countValidSelections1(nums []int) int {
res, suffix, prefix := 0, 0, 0
for _, v := range nums {
suffix += v
}
abs := func(x int) int { if x < 0 { return -x; }; return x; }
for _, v := range nums {
prefix += v
suffix -= v
diff := abs(prefix - suffix)
if v > 0 || diff > 1 { continue }
if diff == 0 {
res++
}
res++
}
return res
}
func main() {
// Example 1:
// Input: nums = [1,0,2,0,3]
// Output: 2
// Explanation:
// The only possible valid selections are the following:
// Choose curr = 3, and a movement direction to the left.
// [1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,1,0,3] -> [1,0,1,0,3] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,0,0,2] -> [1,0,0,0,2] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,1] -> [0,0,0,0,0].
// Choose curr = 3, and a movement direction to the right.
// [1,0,2,0,3] -> [1,0,2,0,3] -> [1,0,2,0,2] -> [1,0,2,0,2] -> [1,0,1,0,2] -> [1,0,1,0,2] -> [1,0,1,0,1] -> [1,0,1,0,1] -> [1,0,0,0,1] -> [1,0,0,0,1] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [1,0,0,0,0] -> [0,0,0,0,0].
fmt.Println(countValidSelections([]int{1,0,2,0,3})) // 2
// Example 2:
// Input: nums = [2,3,4,0,4,1,0]
// Output: 0
// Explanation:
// There are no possible valid selections.
fmt.Println(countValidSelections([]int{2,3,4,0,4,1,0})) // 0
fmt.Println(countValidSelections([]int{1,2,3,4,5,6,7,8,9})) // 0
fmt.Println(countValidSelections([]int{9,8,7,6,5,4,3,2,1})) // 0
fmt.Println(countValidSelections1([]int{1,0,2,0,3})) // 2
fmt.Println(countValidSelections1([]int{2,3,4,0,4,1,0})) // 0
fmt.Println(countValidSelections1([]int{1,2,3,4,5,6,7,8,9})) // 0
fmt.Println(countValidSelections1([]int{9,8,7,6,5,4,3,2,1})) // 0
}