LeetCode 上1769号 面试编程题,python编程

原题地址:

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)

======================================

本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注者,如有侵权请与博主联系。
原文地址:https://www.cnblogs.com/devilmaycry812839668/p/14535046.html