原题地址:
https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/
----------------------------------------------------------------
事件起源于实验室的慕师弟马上要博士毕业,意向是要去互联网公司,于是建议其去网上练练编程题,也正因此见到了第一道编程题,一时无思路便发给了我。想想这题也是蛮典型的一道题,而且还有些意思,于是便在闲着无聊时写了写,虽然自己毕业遥遥无望但是对其他人马上要脱离苦海还是很祝贺的。
给出自己的code :
import sys
class Solution:
def minOperations(self, boxes: str):# -> List[int]:
left_one = 0
right_one = 0
dist = []
temp = 0
for index, number in enumerate(boxes[1:]):
if int(number)==1:
temp += index+1
right_one += 1
dist.append(temp)
for i in range(1, len(boxes)):
if int(boxes[i-1]) == 1:
left_one += 1
temp = temp + left_one - right_one
if int(boxes[i]) == 1:
right_one -= 1
dist.append(temp)
return dist
if __name__ == "__main__":
a=Solution().minOperations(sys.argv[1:][0])
print(a)