|
27 | 27 |
|
28 | 28 | */ |
29 | 29 | public class _407 { |
30 | | - /**Reference: https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue*/ |
31 | | - public class Cell { |
32 | | - int row; |
33 | | - int col; |
34 | | - int height; |
35 | | - |
36 | | - public Cell(int row, int col, int height) { |
37 | | - this.row = row; |
38 | | - this.col = col; |
39 | | - this.height = height; |
| 30 | + public static class Solution1 { |
| 31 | + /** Reference: https://discuss.leetcode.com/topic/60418/java-solution-using-priorityqueue */ |
| 32 | + public class Cell { |
| 33 | + int row; |
| 34 | + int col; |
| 35 | + int height; |
| 36 | + |
| 37 | + public Cell(int row, int col, int height) { |
| 38 | + this.row = row; |
| 39 | + this.col = col; |
| 40 | + this.height = height; |
| 41 | + } |
40 | 42 | } |
41 | | - } |
42 | 43 |
|
43 | | - public int trapRainWater(int[][] heights) { |
44 | | - if (heights == null || heights.length == 0 || heights[0].length == 0) { |
45 | | - return 0; |
46 | | - } |
| 44 | + public int trapRainWater(int[][] heights) { |
| 45 | + if (heights == null || heights.length == 0 || heights[0].length == 0) { |
| 46 | + return 0; |
| 47 | + } |
47 | 48 |
|
48 | | - PriorityQueue<Cell> queue = new PriorityQueue<>(1, (a, b) -> a.height - b.height); |
| 49 | + PriorityQueue<Cell> queue = new PriorityQueue<>(1, (a, b) -> a.height - b.height); |
49 | 50 |
|
50 | | - int m = heights.length; |
51 | | - int n = heights[0].length; |
52 | | - boolean[][] visited = new boolean[m][n]; |
| 51 | + int m = heights.length; |
| 52 | + int n = heights[0].length; |
| 53 | + boolean[][] visited = new boolean[m][n]; |
53 | 54 |
|
54 | | - // Initially, add all the Cells which are on borders to the queue. |
55 | | - for (int i = 0; i < m; i++) { |
56 | | - visited[i][0] = true; |
57 | | - visited[i][n - 1] = true; |
58 | | - queue.offer(new Cell(i, 0, heights[i][0])); |
59 | | - queue.offer(new Cell(i, n - 1, heights[i][n - 1])); |
60 | | - } |
| 55 | + // Initially, add all the Cells which are on borders to the queue. |
| 56 | + for (int i = 0; i < m; i++) { |
| 57 | + visited[i][0] = true; |
| 58 | + visited[i][n - 1] = true; |
| 59 | + queue.offer(new Cell(i, 0, heights[i][0])); |
| 60 | + queue.offer(new Cell(i, n - 1, heights[i][n - 1])); |
| 61 | + } |
61 | 62 |
|
62 | | - for (int i = 0; i < n; i++) { |
63 | | - visited[0][i] = true; |
64 | | - visited[m - 1][i] = true; |
65 | | - queue.offer(new Cell(0, i, heights[0][i])); |
66 | | - queue.offer(new Cell(m - 1, i, heights[m - 1][i])); |
67 | | - } |
| 63 | + for (int i = 0; i < n; i++) { |
| 64 | + visited[0][i] = true; |
| 65 | + visited[m - 1][i] = true; |
| 66 | + queue.offer(new Cell(0, i, heights[0][i])); |
| 67 | + queue.offer(new Cell(m - 1, i, heights[m - 1][i])); |
| 68 | + } |
68 | 69 |
|
69 | | - // from the borders, pick the shortest cell visited and check its neighbors: |
70 | | - // if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped |
71 | | - // add all its neighbors to the queue. |
72 | | - int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; |
73 | | - int res = 0; |
74 | | - while (!queue.isEmpty()) { |
75 | | - Cell cell = queue.poll(); |
76 | | - for (int[] dir : dirs) { |
77 | | - int row = cell.row + dir[0]; |
78 | | - int col = cell.col + dir[1]; |
79 | | - if (row >= 0 && row < m && col >= 0 && col < n && !visited[row][col]) { |
80 | | - visited[row][col] = true; |
81 | | - res += Math.max(0, cell.height - heights[row][col]); |
82 | | - queue.offer(new Cell(row, col, Math.max(heights[row][col], cell.height))); |
| 70 | + // from the borders, pick the shortest cell visited and check its neighbors: |
| 71 | + // if the neighbor is shorter, collect the water it can trap and update its height as its height plus the water trapped |
| 72 | + // add all its neighbors to the queue. |
| 73 | + int[][] dirs = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; |
| 74 | + int res = 0; |
| 75 | + while (!queue.isEmpty()) { |
| 76 | + Cell cell = queue.poll(); |
| 77 | + for (int[] dir : dirs) { |
| 78 | + int row = cell.row + dir[0]; |
| 79 | + int col = cell.col + dir[1]; |
| 80 | + if (row >= 0 && row < m && col >= 0 && col < n && !visited[row][col]) { |
| 81 | + visited[row][col] = true; |
| 82 | + res += Math.max(0, cell.height - heights[row][col]); |
| 83 | + queue.offer(new Cell(row, col, Math.max(heights[row][col], cell.height))); |
| 84 | + } |
83 | 85 | } |
84 | 86 | } |
85 | | - } |
86 | 87 |
|
87 | | - return res; |
| 88 | + return res; |
| 89 | + } |
88 | 90 | } |
89 | 91 | } |
0 commit comments