This commit is contained in:
parent
018bef7505
commit
de3aca0d83
1 changed files with 21 additions and 0 deletions
|
|
@ -0,0 +1,21 @@
|
|||
# https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box
|
||||
|
||||
from typing import List
|
||||
|
||||
class Solution:
|
||||
def minOperations(self, boxes: str) -> List[int]:
|
||||
l = len(boxes)
|
||||
result = [0] * (l + 1)
|
||||
left, right = 0, 0
|
||||
for i in range(l):
|
||||
if boxes[i] == "1":
|
||||
result[0] += i + 1
|
||||
right += 1
|
||||
for i in range(l):
|
||||
result[i + 1] = result[i] - right + left
|
||||
if boxes[i] == "1":
|
||||
right -= 1
|
||||
left += 1
|
||||
|
||||
return result[1:]
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue