|
1 | 1 | __________________________________________________________________________________________________
|
2 |
| - |
| 2 | +sample 164 ms submission |
| 3 | +class Solution: |
| 4 | + def trapRainWater(self, heightMap: List[List[int]]) -> int: |
| 5 | + if not heightMap or not heightMap[0]: |
| 6 | + return 0 |
| 7 | + m = len(heightMap) |
| 8 | + n = len(heightMap[0]) |
| 9 | + |
| 10 | + heap = [] |
| 11 | + seen = [[False] * n for _ in range(m)] |
| 12 | + |
| 13 | + # Puts the edges on the heap |
| 14 | + for i in range(m): |
| 15 | + heap.append( (heightMap[i][0], i, 0) ) |
| 16 | + heap.append( (heightMap[i][n - 1], i, n - 1) ) |
| 17 | + seen[i][0] = True |
| 18 | + seen[i][n - 1] = True |
| 19 | + for j in range(n): |
| 20 | + heap.append( (heightMap[0][j], 0, j) ) |
| 21 | + heap.append( (heightMap[m - 1][j], m - 1, j) ) |
| 22 | + seen[0][j] = True |
| 23 | + seen[m - 1][j] = True |
| 24 | + heapq.heapify(heap) |
| 25 | + |
| 26 | + area = 0 |
| 27 | + while heap: |
| 28 | + h, i, j = heapq.heappop(heap) |
| 29 | + if h > heightMap[i][j]: |
| 30 | + area += h - heightMap[i][j] |
| 31 | + |
| 32 | + if i > 0 and not seen[i - 1][j]: |
| 33 | + heapq.heappush(heap, (max(h, heightMap[i - 1][j]), i - 1, j)) |
| 34 | + seen[i - 1][j] = True |
| 35 | + if i + 1 < m and not seen[i + 1][j]: |
| 36 | + heapq.heappush(heap, (max(h, heightMap[i + 1][j]), i + 1, j)) |
| 37 | + seen[i + 1][j] = True |
| 38 | + if j > 0 and not seen[i][j - 1]: |
| 39 | + heapq.heappush(heap, (max(h, heightMap[i][j - 1]), i, j - 1)) |
| 40 | + seen[i][j - 1] = True |
| 41 | + if j + 1 < n and not seen[i][j + 1]: |
| 42 | + heapq.heappush(heap, (max(h, heightMap[i][j + 1]), i, j + 1)) |
| 43 | + seen[i][j + 1] = True |
| 44 | + |
| 45 | + return area |
3 | 46 | __________________________________________________________________________________________________
|
4 |
| - |
| 47 | +sample 14168 kb submission |
| 48 | +class Solution: |
| 49 | + def trapRainWater(self, heightMap): |
| 50 | + """ |
| 51 | + :type heightMap: List[List[int]] |
| 52 | + :rtype: int |
| 53 | + """ |
| 54 | + if not any(heightMap): |
| 55 | + return 0 |
| 56 | + m, n = len(heightMap), len(heightMap[0]) |
| 57 | + boundaries = [] |
| 58 | + for i, row in enumerate(heightMap): |
| 59 | + for j, val in enumerate(row): |
| 60 | + if i in (0, m - 1) or j in (0, n - 1): |
| 61 | + boundaries.append((val, i, j)) |
| 62 | + heightMap[i][j] = -1 |
| 63 | + heapq.heapify(boundaries) |
| 64 | + ans = 0 |
| 65 | + while boundaries: |
| 66 | + height, i, j = heapq.heappop(boundaries) |
| 67 | + for newi, newj in (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1): |
| 68 | + if 0 <= newi < m and 0 <= newj < n and heightMap[newi][newj] != -1: |
| 69 | + ans += max(0, height - heightMap[newi][newj]) |
| 70 | + heapq.heappush(boundaries, (max(height, heightMap[newi][newj]), newi, newj)) |
| 71 | + heightMap[newi][newj] = -1 |
| 72 | + return ans |
5 | 73 | __________________________________________________________________________________________________
|
0 commit comments