classSolution { public: inttrapRainWater(vector<vector<int>>& heightMap){ if (heightMap.size() <= 2 || heightMap[0].size() <= 2) { return0; } int m = heightMap.size(); int n = heightMap[0].size(); priority_queue<pii, vector<pii>, greater<pii>> pq; vector<vector<bool>> visit(m, vector<bool>(n, false)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (i == 0 || i == m - 1 || j == 0 || j == n - 1) { pq.push({heightMap[i][j], i * n + j}); visit[i][j] = true; } } }
int res = 0; int dirs[] = {-1, 0, 1, 0, -1}; while (!pq.empty()) { pii curr = pq.top(); pq.pop(); for (int k = 0; k < 4; ++k) { int nx = curr.second / n + dirs[k]; int ny = curr.second % n + dirs[k + 1]; if( nx >= 0 && nx < m && ny >= 0 && ny < n && !visit[nx][ny]) { if (heightMap[nx][ny] < curr.first) { res += curr.first - heightMap[nx][ny]; } visit[nx][ny] = true; pq.push({max(heightMap[nx][ny], curr.first), nx * n + ny}); } } } return res; } };
m, n = len(heightMap), len(heightMap[0]) visited = [[0for _ inrange(n)] for _ inrange(m)] pq = [] for i inrange(m): for j inrange(n): if i == 0or i == m - 1or j == 0or j == n - 1: visited[i][j] = 1 heapq.heappush(pq, (heightMap[i][j], i * n + j)) res = 0 dirs = [-1, 0, 1, 0, -1] while pq: height, position = heapq.heappop(pq) for k inrange(4): nx, ny = position // n + dirs[k], position % n + dirs[k + 1] if nx >= 0and nx < m and ny >= 0and ny < n and visited[nx][ny] == 0: if height > heightMap[nx][ny]: res += height - heightMap[nx][ny] visited[nx][ny] = 1 heapq.heappush(pq, (max(height, heightMap[nx][ny]), nx * n + ny)) return res
functrapRainWater(heightMap [][]int) (ans int) { m, n := len(heightMap), len(heightMap[0]) if m <= 2 || n <= 2 { return }
vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) } h := &hp{} for i, row := range heightMap { for j, v := range row { if i == 0 || i == m-1 || j == 0 || j == n-1 { heap.Push(h, cell{v, i, j}) vis[i][j] = true } } }
dirs := []int{-1, 0, 1, 0, -1} for h.Len() > 0 { cur := heap.Pop(h).(cell) for k := 0; k < 4; k++ { nx, ny := cur.x+dirs[k], cur.y+dirs[k+1] if0 <= nx && nx < m && 0 <= ny && ny < n && !vis[nx][ny] { if heightMap[nx][ny] < cur.h { ans += cur.h - heightMap[nx][ny] } vis[nx][ny] = true heap.Push(h, cell{max(heightMap[nx][ny], cur.h), nx, ny}) } } } return }
type cell struct{ h, x, y int } type hp []cell func(h hp) Len() int { returnlen(h) } func(h hp) Less(i, j int) bool { return h[i].h < h[j].h } func(h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func(h *hp) Push(v interface{}) { *h = append(*h, v.(cell)) } func(h *hp) Pop() interface{} { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
funcmax(a, b int)int { if b > a { return b } return a }
classSolution { public: inttrapRainWater(vector<vector<int>>& heightMap){ int m = heightMap.size(), n = heightMap[0].size(); int maxHeight = 0; int dirs[] = {-1, 0, 1, 0, -1};
for (int i = 0; i < m; ++i) { maxHeight = max(maxHeight, *max_element(heightMap[i].begin(), heightMap[i].end())); } vector<vector<int>> water(m, vector<int>(n, maxHeight)); queue<pair<int,int>> qu; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (i == 0 || i == m - 1 || j == 0 || j == n - 1) { if (water[i][j] > heightMap[i][j]) { water[i][j] = heightMap[i][j]; qu.push(make_pair(i, j)); } } } } while (!qu.empty()) { int x = qu.front().first, y = qu.front().second; qu.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dirs[i], ny = y + dirs[i + 1]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) { continue; } if (water[x][y] < water[nx][ny] && water[nx][ny] > heightMap[nx][ny]) { water[nx][ny] = max(water[x][y], heightMap[nx][ny]); qu.push(make_pair(nx, ny)); } } }
int res = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { res += water[i][j] - heightMap[i][j]; } } return res; } };
publicclassSolution { publicintTrapRainWater(int[][] heightMap) { int m = heightMap.Length; int n = heightMap[0].Length; int[] dirs = {-1, 0, 1, 0, -1}; int maxHeight = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { maxHeight = Math.Max(maxHeight, heightMap[i][j]); } } int[,] water = newint[m, n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j){ water[i, j] = maxHeight; } }
Queue<int[]> qu = new Queue<int[]>(); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (i == 0 || i == m - 1 || j == 0 || j == n - 1) { if (water[i, j] > heightMap[i][j]) { water[i, j] = heightMap[i][j]; qu.Enqueue(newint[]{i, j}); } } } }
while (qu.Count > 0) { int[] curr = qu.Dequeue(); int x = curr[0]; int y = curr[1]; for (int i = 0; i < 4; ++i) { int nx = x + dirs[i], ny = y + dirs[i + 1]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) { continue; } if (water[x, y] < water[nx, ny] && water[nx, ny] > heightMap[nx][ny]) { water[nx, ny] = Math.Max(water[x, y], heightMap[nx][ny]); qu.Enqueue(newint[]{nx, ny}); } } }
int res = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { res += water[i, j] - heightMap[i][j]; } } return res; } }
classSolution: deftrapRainWater(self, heightMap: List[List[int]]) -> int: m, n = len(heightMap), len(heightMap[0]) maxHeight = max(max(row) for row in heightMap) water = [[maxHeight for _ inrange(n)] for _ inrange(m)] dirs = [-1, 0, 1, 0, -1]
qu = [] for i inrange(m): for j inrange(n): if i == 0or i == m - 1or j == 0or j == n - 1: if water[i][j] > heightMap[i][j]: water[i][j] = heightMap[i][j] qu.append([i, j]) whilelen(qu) > 0: [x, y] = qu.pop(0) for i inrange(4): nx, ny = x + dirs[i], y + dirs[i + 1] if nx < 0or nx >= m or ny < 0or ny >= n: continue if water[x][y] < water[nx][ny] and water[nx][ny] > heightMap[nx][ny]: water[nx][ny] = max(water[x][y], heightMap[nx][ny]) qu.append([nx, ny])
ans = 0 for i inrange(m): for j inrange(n): ans = ans + water[i][j] - heightMap[i][j] return ans
functrapRainWater(heightMap [][]int) (ans int) { m, n := len(heightMap), len(heightMap[0]) maxHeight := 0 for _, row := range heightMap { for _, h := range row { maxHeight = max(maxHeight, h) } }
water := make([][]int, m) for i := range water { water[i] = make([]int, n) for j := range water[i] { water[i][j] = maxHeight } } type pair struct{ x, y int } q := []pair{} for i, row := range heightMap { for j, h := range row { if (i == 0 || i == m-1 || j == 0 || j == n-1) && h < water[i][j] { water[i][j] = h q = append(q, pair{i, j}) } } }
dirs := []int{-1, 0, 1, 0, -1} forlen(q) > 0 { p := q[0] q = q[1:] x, y := p.x, p.y for i := 0; i < 4; i++ { nx, ny := x+dirs[i], y+dirs[i+1] if0 <= nx && nx < m && 0 <= ny && ny < n && water[nx][ny] > water[x][y] && water[nx][ny] > heightMap[nx][ny] { water[nx][ny] = max(water[x][y], heightMap[nx][ny]) q = append(q, pair{nx, ny}) } } }
for i, row := range heightMap { for j, h := range row { ans += water[i][j] - h } } return }
funcmax(a, b int)int { if b > a { return b } return a }