-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3354-Make_Array_Elements_Equal_to_Zero.py
More file actions
76 lines (64 loc) · 2.28 KB
/
Copy path3354-Make_Array_Elements_Equal_to_Zero.py
File metadata and controls
76 lines (64 loc) · 2.28 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
"""
3354-Make_Array_Elements_Equal_to_Zero.py
Billy.Ljm
28 October 2025
=======
Problem
=======
https://leetcode.com/problems/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:
- If curr is out of the range [0, n - 1], this process ends.
- 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.
- Else if nums[curr] > 0:
- Decrement nums[curr] by 1.
- Reverse your movement direction (left becomes right and vice versa).
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.
===========
My Approach
===========
We will iterate through the array and simply compare the left and right sum.
The array will be zero if the left and right sum differ by -1, 0 or 1. This is
because the sums represent the number of times we can bounce off each side, and
all bounces will be consumed and elements made zero when the bounces on each
side differ by at most 1.
This has a time complexity of O(n), and a space complexity of O(n), where n is
the size of the array.
"""
class Solution(object):
def countValidSelections(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
out = 0
leftsum = 0
rightsum = sum(nums)
for i in range(len(nums)):
if i-1 >= 0: leftsum += nums[i-1]
rightsum -= nums[i]
if nums[i] == 0:
if leftsum - rightsum == 0: # both directions work
out += 2
elif abs(leftsum - rightsum) == 1: # only 1 direction works
out += 1
return out
"""
Test cases
"""
if __name__ == "__main__":
sol = Solution()
# test case 1
nums = [1,0,2,0,3]
print(f"countValidSelections({nums}) = " +
str(sol.countValidSelections(nums)))
# test case 2
nums = [2,3,4,0,4,1,0]
print(f"countValidSelections({nums}) = " +
str(sol.countValidSelections(nums)))