Skip to content

Commit 57654cd

Browse files
authored
Create minimum-number-of-operations-to-move-all-balls-to-each-box.py
1 parent 9d81844 commit 57654cd

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def minOperations(self, boxes):
6+
"""
7+
:type boxes: str
8+
:rtype: List[int]
9+
"""
10+
result = [0]*len(boxes)
11+
for direction in (lambda x:x, reversed):
12+
cnt = accu = 0
13+
for i in direction(xrange(len(boxes))):
14+
result[i] += accu
15+
if boxes[i] == '1':
16+
cnt += 1
17+
accu += cnt
18+
return result

0 commit comments

Comments
 (0)