-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1769.py
More file actions
22 lines (22 loc) · 708 Bytes
/
Copy path1769.py
File metadata and controls
22 lines (22 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def minOperations(self, boxes: str) -> List[int]:
#sliding window?
ones = []
for i,j in enumerate(boxes):
if j == '1':
ones.append(i)
left, right = 0, len(ones) #record the number of balls stay on LHS or equal/ RHS the current position
ini, ini_val = 0, sum(ones)
ans = []
while ini < len(boxes):
if ini == 0:
ans.append(ini_val)
else:
ini_val += left
ini_val -= right
ans.append(ini_val)
if boxes[ini] == '1':
left += 1
right -= 1
ini += 1
return ans