|
| 1 | +# Approach: BFS + Priority Queue |
| 2 | + |
| 3 | +# m = no. of rows, n = no. of cols |
| 4 | +# Time: O(m * n * log(mn)) |
| 5 | +# Space: O(m * n) |
| 6 | + |
| 7 | +import heapq |
| 8 | + |
| 9 | +class Solution: |
| 10 | + # Class to store the height and coordinates of a cell in the grid |
| 11 | + class Cell: |
| 12 | + def __init__(self, height, row, col): |
| 13 | + self.height = height |
| 14 | + self.row = row |
| 15 | + self.col = col |
| 16 | + |
| 17 | + # Comparison method for the priority queue (min-heap) |
| 18 | + def __lt__(self, other): |
| 19 | + return self.height < other.height |
| 20 | + |
| 21 | + def _is_valid_cell(self, row, col, num_rows, num_cols): |
| 22 | + return 0 <= row < num_rows and 0 <= col < num_cols |
| 23 | + |
| 24 | + def trapRainWater(self, heightMap: List[List[int]]) -> int: |
| 25 | + # Direction arrays |
| 26 | + d_row = [0, 0, -1, 1] |
| 27 | + d_col = [-1, 1, 0, 0] |
| 28 | + |
| 29 | + num_rows = len(heightMap) |
| 30 | + num_cols = len(heightMap[0]) |
| 31 | + |
| 32 | + visited = [[False] * num_cols for _ in range(num_rows)] |
| 33 | + |
| 34 | + # Priority queue (min-heap) to process boundary cells in increasing height order |
| 35 | + boundary = [] |
| 36 | + |
| 37 | + # Add the first and last column cells to the boundary and mark them as visited |
| 38 | + for i in range(num_rows): |
| 39 | + heapq.heappush(boundary, self.Cell(heightMap[i][0], i, 0)) |
| 40 | + heapq.heappush(boundary, self.Cell(heightMap[i][num_cols - 1], i, num_cols - 1)) |
| 41 | + visited[i][0] = visited[i][num_cols - 1] = True |
| 42 | + |
| 43 | + # Add the first and last row cells to the boundary and mark them as visited |
| 44 | + for i in range(num_cols): |
| 45 | + heapq.heappush(boundary, self.Cell(heightMap[0][i], 0, i)) |
| 46 | + heapq.heappush(boundary, self.Cell(heightMap[num_rows - 1][i], num_rows - 1, i)) |
| 47 | + visited[0][i] = visited[num_rows - 1][i] = True |
| 48 | + |
| 49 | + # Result |
| 50 | + total_water_volume = 0 |
| 51 | + |
| 52 | + # Process cells in the boundary (min-heap will always pop the smallest height) |
| 53 | + while boundary: |
| 54 | + # Pop the cell with the smallest height from the boundary |
| 55 | + current_cell = heapq.heappop(boundary) |
| 56 | + |
| 57 | + current_row = current_cell.row |
| 58 | + current_col = current_cell.col |
| 59 | + min_boundary_height = current_cell.height |
| 60 | + |
| 61 | + # Explore all 4 neighboring cells |
| 62 | + for direction in range(4): |
| 63 | + neighbor_row = current_row + d_row[direction] |
| 64 | + neighbor_col = current_col + d_col[direction] |
| 65 | + |
| 66 | + if (self._is_valid_cell(neighbor_row, neighbor_col, num_rows, num_cols) and not visited[neighbor_row][neighbor_col]): |
| 67 | + neighbor_height = heightMap[neighbor_row][neighbor_col] |
| 68 | + |
| 69 | + # If the neighbor's height is less than the current boundary height, water can be trapped |
| 70 | + if neighbor_height < min_boundary_height: |
| 71 | + total_water_volume += min_boundary_height - neighbor_height |
| 72 | + |
| 73 | + # Push the neighbor into the boundary with updated height (to prevent water leakage) |
| 74 | + heapq.heappush(boundary, self.Cell(max(neighbor_height, min_boundary_height), neighbor_row, neighbor_col)) |
| 75 | + visited[neighbor_row][neighbor_col] = True |
| 76 | + |
| 77 | + return total_water_volume |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
0 commit comments