-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 22
More file actions
32 lines (24 loc) · 832 Bytes
/
Day 22
File metadata and controls
32 lines (24 loc) · 832 Bytes
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
#3354.make-array-elements-equal-to-zero:-
class Solution:
def countValidSelections(self, nums: List[int]) -> int:
n = len(nums)
def simulate(start, direction):
arr = nums[:]
curr = start
dirn = direction
while 0 <= curr < n:
if arr[curr] == 0:
curr += dirn
else:
arr[curr] -= 1
dirn *= -1
curr += dirn
return all(x == 0 for x in arr)
count = 0
for i in range(n):
if nums[i] == 0:
if simulate(i, 1):
count += 1
if simulate(i, -1):
count += 1
return count