417. Pacific Atlantic Water Flow
1. Question
Given anm x n
matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.
Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.
Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.
Note:
The order of returned grid coordinates does not matter.
Both m and n are less than 150.
Example:
Copy Given the following 5x5 matrix:
Pacific ~ ~ ~ ~ ~
~ 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 *
* * * * * Atlantic
Return:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
2. Implementation
(1) BFS
Copy class Solution {
public List < int []> pacificAtlantic ( int [][] matrix) {
List < int []> res = new ArrayList <>();
if (matrix == null || matrix . length == 0 || matrix[ 0 ] . length == 0 ) {
return res;
}
int m = matrix . length , n = matrix[ 0 ] . length ;
boolean [][] pVisited = new boolean [m][n];
boolean [][] aVisited = new boolean [m][n];
int [][] directions = {{ - 1 , 0 } , { 1 , 0 } , { 0 , - 1 } , { 0 , 1 }};
for ( int i = 0 ; i < m; i ++ ) {
searchByBFS(matrix , i , 0 , pVisited , directions) ;
searchByBFS(matrix , i , n - 1 , aVisited , directions) ;
}
for ( int j = 0 ; j < n; j ++ ) {
searchByBFS(matrix , 0 , j , pVisited , directions) ;
searchByBFS(matrix , m - 1 , j , aVisited , directions) ;
}
for ( int i = 0 ; i < m; i ++ ) {
for ( int j = 0 ; j < n; j ++ ) {
if (pVisited[i][j] && aVisited[i][j]) {
res . add ( new int [] {i , j});
}
}
}
return res;
}
public void searchByBFS ( int [][] matrix , int i , int j , boolean [][] visited , int [][] directions) {
visited[i][j] = true ;
Queue < int []> queue = new LinkedList <>();
queue . add ( new int [] {i , j});
while ( ! queue . isEmpty ()) {
int [] curCell = queue . remove ();
int curRow = curCell[ 0 ] , curCol = curCell[ 1 ];
for ( int [] direction : directions) {
int nextRow = curRow + direction[ 0 ];
int nextCol = curCol + direction[ 1 ];
if ( isValid(matrix , curRow , curCol , nextRow , nextCol , visited) ) {
visited[nextRow][nextCol] = true ;
queue . add ( new int [] {nextRow , nextCol});
}
}
}
}
public boolean isValid ( int [][] matrix , int curRow , int curCol , int nextRow , int nextCol , boolean [][] visited) {
return nextRow >= 0 && nextRow < matrix.length && nextCol >= 0 && nextCol < matrix[0].length && !visited[nextRow][nextCol] && matrix[curRow][curCol] <= matrix[nextRow][nextCol];
}
}
(2) DFS
Copy public class Solution {
public List < int []> pacificAtlantic ( int [][] matrix) {
List < int []> res = new ArrayList <>();
if (matrix == null || matrix . length == 0 || matrix[ 0 ] . length == 0 ) {
return res;
}
int m = matrix . length , n = matrix[ 0 ] . length ;
boolean [][] aVisited = new boolean [m][n];
boolean [][] pVisited = new boolean [m][n];
int [][] directions = {{ - 1 , 0 } , { 1 , 0 } , { 0 , - 1 } , { 0 , 1 }};
for ( int i = 0 ; i < m; i ++ ) {
searchByDFS(i , 0 , matrix , directions , pVisited) ;
searchByDFS(i , n - 1 , matrix , directions , aVisited) ;
}
for ( int j = 0 ; j < n; j ++ ) {
searchByDFS( 0 , j , matrix , directions , pVisited) ;
searchByDFS(m - 1 , j , matrix , directions , aVisited) ;
}
for ( int i = 0 ; i < m; i ++ ) {
for ( int j = 0 ; j < n; j ++ ) {
if (pVisited[i][j] && aVisited[i][j]) {
res . add ( new int [] {i , j});
}
}
}
return res;
}
public void searchByDFS ( int row , int col , int [][] matrix , int [][] directions , boolean [][] visited) {
visited[row][col] = true ;
for ( int [] direction : directions) {
int nextRow = row + direction[ 0 ];
int nextCol = col + direction[ 1 ];
if ( isValid(row , col , nextRow , nextCol , matrix , visited) ) {
searchByDFS(nextRow , nextCol , matrix , directions , visited) ;
}
}
}
public boolean isValid ( int curRow , int curCol , int nextRow , int nextCol , int [][] matrix , boolean [][] visited) {
return nextRow >= 0 && nextRow < matrix.length && nextCol >= 0 && nextCol < matrix[0].length && !visited[nextRow][nextCol] && matrix[curRow][curCol] <= matrix[nextRow][nextCol];
}
}
3. Time & Space Complexity
BFS: 时间复杂度O(mn) , 空间复杂度:O(mn)
DFS: 时间复杂度O(mn) , 空间复杂度:O(mn)