Python | Leetcode Python题解之第554题砖墙
题目:
题解:
class Solution:def leastBricks(self, wall: List[List[int]]) -> int:n = len(wall)# heap内存放的格式为(前缀和、行、列)heap = list()ans = nfor i in range(n):if len(wall[i]) > 1:heapq.heappush(heap, [wall[i][0], i, 1])while heap:# 最左边的位置count = 0cur = heap[0][0]while heap and heap[0][0] == cur:count += 1tmp = heapq.heappop(heap)if tmp[2] + 1 < len(wall[tmp[1]]):tmp[0] += wall[tmp[1]][tmp[2]]tmp[2] += 1heapq.heappush(heap, tmp)if n - count < ans:ans = n - countreturn ans