Skip to content

Commit 96025bc

Browse files
refactor 407
1 parent 708fe3e commit 96025bc

File tree

1 file changed

+50
-48
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+50
-48
lines changed

src/main/java/com/fishercoder/solutions/_407.java

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,65 @@
2727
2828
*/
2929
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+
}
4042
}
41-
}
4243

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+
}
4748

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);
4950

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];
5354

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+
}
6162

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+
}
6869

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+
}
8385
}
8486
}
85-
}
8687

87-
return res;
88+
return res;
89+
}
8890
}
8991
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy