forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
79 lines (63 loc) · 2.14 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2018-06-01
import java.util.List;
import java.util.ArrayList;
/// Floodfill
/// Time Complexity: O(m*n)
/// Space Complexity: O(m*n)
class Solution {
private int n, m;
private boolean[][] pacific, atlantic;
private int[][] d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public List<int[]> pacificAtlantic(int[][] matrix) {
ArrayList<int[]> res = new ArrayList<>();
n = matrix.length;
if(n == 0)
return res;
m = matrix[0].length;
pacific = new boolean[n][m];
atlantic = new boolean[n][m];
for(int i = 0 ; i < n ; i ++){
dfs(matrix, pacific, i, 0);
dfs(matrix, atlantic, i, m - 1);
}
for(int j = 0 ; j < m ; j ++){
dfs(matrix, pacific, 0, j);
dfs(matrix, atlantic, n - 1, j);
}
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
if(pacific[i][j] && atlantic[i][j])
res.add(new int[]{i, j});
return res;
}
private void dfs(int[][] matrix, boolean[][] visited, int x, int y){
visited[x][y] = true;
for(int i = 0 ; i < 4 ; i ++){
int newX = x + d[i][0];
int newY = y + d[i][1];
if(inArea(newX, newY) && !visited[newX][newY] && matrix[newX][newY] >= matrix[x][y])
dfs(matrix, visited, newX, newY);
}
}
private boolean inArea(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m;
}
public static void printList(List<int[]> list){
for(int[] e: list)
System.out.print("[ " + e[0] + " , " + e[1] + " ] ");
System.out.println();
}
public static void main(String[] args){
int[][] matrix = new int[][]{
{1, 2, 2, 3, 5},
{3, 2, 3, 4, 4},
{2, 4, 5, 3, 1},
{6, 7, 1, 4, 5},
{5, 1, 1, 2, 4}
};
List<int[]> res = (new Solution()).pacificAtlantic(matrix);
printList(res);
}
}