您好,欢迎来到叨叨游戏网。
搜索
您的当前位置:首页Python | Leetcode Python题解之第554题砖墙

Python | Leetcode Python题解之第554题砖墙

来源:叨叨游戏网

题目:

题解:

class Solution:
    def leastBricks(self, wall: List[List[int]]) -> int:
        n = len(wall)

        # heap内存放的格式为(前缀和、行、列)
        heap = list()
        ans = n

        for i in range(n):
            if len(wall[i]) > 1:
                heapq.heappush(heap, [wall[i][0], i, 1])

        while heap:
            # 最左边的位置
            count = 0
            cur = heap[0][0]
            while heap and heap[0][0] == cur:
                count += 1
                tmp = heapq.heappop(heap)
                if tmp[2] + 1 < len(wall[tmp[1]]):
                    tmp[0] += wall[tmp[1]][tmp[2]]
                    tmp[2] += 1
                    heapq.heappush(heap, tmp)
            if n - count < ans:
                ans = n - count

        return ans

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.net 版权所有 湘ICP备2024080961号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务