소프트웨어에 대한 모든 것

LeetCode 풀이 - 1769. Minimum Number of Operations to Move All Balls to Each Box 본문

알고리즘/LeetCode

LeetCode 풀이 - 1769. Minimum Number of Operations to Move All Balls to Each Box

앤테바 2021. 10. 19. 00:00
반응형

1769. Minimum Number of Operations to Move All Balls to Each Box

문제)

솔루션1)

class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        res = [0] * len(boxes)
        for i in range(len(boxes)):            
            for j in range(len(boxes)):
                if i == j:
                    continue
                if boxes[j] == '1':
                    res[i] += abs(i - j)
        return res

솔루션2)

class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        res = [0] * len(boxes)

        fill_idxs = []
        for idx, ch in enumerate(boxes):
            if ch == '1':
                fill_idxs.append(idx)

        for i in range(len(boxes)):
            for j in fill_idxs:
                if i == j:
                    continue
                if boxes[j] == '1':
                    res[i] += abs(i - j)
        return res

 

반응형
Comments