Skip to content

Commit eef5bff

Browse files
refactor 417
1 parent 17e4d6d commit eef5bff

File tree

2 files changed

+111
-72
lines changed

2 files changed

+111
-72
lines changed

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

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5-
/**Given the following 5x5 matrix:
5+
/**
6+
* 417. Pacific Atlantic Water Flow
7+
*
8+
* Given the following 5x5 matrix:
69
710
Pacific ~ ~ ~ ~ ~
811
~ 1 2 2 3 (5) *
@@ -17,86 +20,57 @@
1720
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).*/
1821

1922
public class _417 {
20-
//looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean
21-
22-
/**
23-
* One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS.
24-
*/
25-
public List<int[]> pacificAtlantic(int[][] matrix) {
26-
27-
List<int[]> result = new ArrayList();
28-
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
29-
return result;
30-
}
23+
public static class Solution1 {
24+
/**
25+
* Credit: looked at this post: https://discuss.leetcode.com/topic/62379/java-bfs-dfs-from-ocean
26+
* <p>
27+
* One typical trick to work on 2d grid problems is to go through the border and put proper ones into a queue if using BFS.
28+
*/
29+
public List<int[]> pacificAtlantic(int[][] matrix) {
30+
31+
List<int[]> result = new ArrayList();
32+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
33+
return result;
34+
}
3135

32-
int m = matrix.length;
33-
int n = matrix[0].length;
34-
boolean[][] pacific = new boolean[m][n];
35-
boolean[][] atlantic = new boolean[m][n];
36+
int m = matrix.length;
37+
int n = matrix[0].length;
38+
boolean[][] pacific = new boolean[m][n];
39+
boolean[][] atlantic = new boolean[m][n];
3640

37-
for (int i = 0; i < m; i++) {
38-
dfs(matrix, pacific, Integer.MIN_VALUE, i, 0);
39-
dfs(matrix, atlantic, Integer.MIN_VALUE, i, n - 1);
40-
}
41+
for (int i = 0; i < m; i++) {
42+
dfs(matrix, pacific, Integer.MIN_VALUE, i, 0);
43+
dfs(matrix, atlantic, Integer.MIN_VALUE, i, n - 1);
44+
}
4145

42-
for (int i = 0; i < n; i++) {
43-
dfs(matrix, pacific, Integer.MIN_VALUE, 0, i);
44-
dfs(matrix, atlantic, Integer.MIN_VALUE, m - 1, i);
45-
}
46+
for (int i = 0; i < n; i++) {
47+
dfs(matrix, pacific, Integer.MIN_VALUE, 0, i);
48+
dfs(matrix, atlantic, Integer.MIN_VALUE, m - 1, i);
49+
}
4650

47-
for (int i = 0; i < m; i++) {
48-
for (int j = 0; j < n; j++) {
49-
if (pacific[i][j] && atlantic[i][j]) {
50-
result.add(new int[]{i, j});
51+
for (int i = 0; i < m; i++) {
52+
for (int j = 0; j < n; j++) {
53+
if (pacific[i][j] && atlantic[i][j]) {
54+
result.add(new int[]{i, j});
55+
}
5156
}
5257
}
53-
}
54-
55-
return result;
56-
}
5758

58-
void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) {
59-
int m = matrix.length;
60-
int n = matrix[0].length;
61-
if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) {
62-
return;
59+
return result;
6360
}
64-
visited[x][y] = true;
65-
dfs(matrix, visited, matrix[x][y], x + 1, y);
66-
dfs(matrix, visited, matrix[x][y], x, y + 1);
67-
dfs(matrix, visited, matrix[x][y], x - 1, y);
68-
dfs(matrix, visited, matrix[x][y], x, y - 1);
69-
}
7061

71-
public static void main(String... args) {
72-
_417 test = new _417();
73-
// int[][] matrix = new int[][]{
74-
// {1,2,2,3,5},
75-
// {3,2,3,4,4},
76-
// {2,4,5,3,1},
77-
// {6,7,1,4,5},
78-
// {5,1,1,2,4},
79-
// };
80-
81-
// int[][] matrix = new int[][]{//this one is correct
82-
// {3,5},
83-
// {4,4},
84-
// };
85-
86-
// int[][] matrix = new int[][]{//this one is correct
87-
// {2,3,5},
88-
// {3,4,4},
89-
// };
90-
91-
int[][] matrix = new int[][]{
92-
{2, 3, 5},
93-
{3, 4, 4},
94-
{5, 3, 1},
95-
};
96-
List<int[]> result = test.pacificAtlantic(matrix);
97-
for (int[] point : result) {
98-
System.out.println(point[0] + "\t" + point[1]);
62+
void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) {
63+
int m = matrix.length;
64+
int n = matrix[0].length;
65+
if (x < 0 || y < 0 || x >= m || y >= n || matrix[x][y] < height || visited[x][y]) {
66+
return;
67+
}
68+
visited[x][y] = true;
69+
dfs(matrix, visited, matrix[x][y], x + 1, y);
70+
dfs(matrix, visited, matrix[x][y], x, y + 1);
71+
dfs(matrix, visited, matrix[x][y], x - 1, y);
72+
dfs(matrix, visited, matrix[x][y], x, y - 1);
9973
}
10074
}
10175

102-
}
76+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._417;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
public class _417Test {
9+
private static _417.Solution1 solution1;
10+
private static int[][] matrix;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _417.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
matrix = new int[][]{
20+
{2, 3, 5},
21+
{3, 4, 4},
22+
{5, 3, 1},
23+
};
24+
for (int[] arr : solution1.pacificAtlantic(matrix)) {
25+
CommonUtils.printArray(arr);
26+
}
27+
}
28+
29+
@Test
30+
public void test2() {
31+
matrix = new int[][]{
32+
{3, 5},
33+
{4, 4},
34+
};
35+
for (int[] arr : solution1.pacificAtlantic(matrix)) {
36+
CommonUtils.printArray(arr);
37+
}
38+
}
39+
40+
@Test
41+
public void test3() {
42+
matrix = new int[][]{
43+
{1, 2, 2, 3, 5},
44+
{3, 2, 3, 4, 4},
45+
{2, 4, 5, 3, 1},
46+
{6, 7, 1, 4, 5},
47+
{5, 1, 1, 2, 4},
48+
};
49+
for (int[] arr : solution1.pacificAtlantic(matrix)) {
50+
CommonUtils.printArray(arr);
51+
}
52+
}
53+
54+
@Test
55+
public void test4() {
56+
matrix = new int[][]{
57+
{2, 3, 5},
58+
{3, 4, 4},
59+
};
60+
for (int[] arr : solution1.pacificAtlantic(matrix)) {
61+
CommonUtils.printArray(arr);
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)