先扫一次目前cost
然后计算目前index以右跟以左 有多少'1'
就知道每动一步cost会加减多少
def minOperations(self, boxes: str) -> List[int]:
cur_cost = 0
for i in range(len(boxes)):
if boxes[i] == '1':
cur_cost += i
ones_right = boxes.count('1')
ones_left = 0
ans = []
for i in range(len(boxes)):
ones_right -= (boxes[i]=='1')
ones_left += (boxes[i]=='1')
ans.append(cur_cost)
cur_cost -= ones_right
cur_cost += ones_left
return ans