Formatted question description: https://leetcode.ca/all/417.html

417. Pacific Atlantic Water Flow

Medium

Description

Given an m 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:

1. The order of returned grid coordinates does not matter.
2. Both m and n are less than 150.

Example:

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).


Solution

Since water can only flow from a cell to another one with height equal or lower, think of the solution in the opposite direction. Starting from the “Pacific ocean” and the “Atlantic ocean” respectively, find the cells from which water can flow to the oceans. If a cell A can make water flow to the ocean, then consider the adjacent cells of A. If a cell is adjacent to A and has an equal or higher height, then water can flow from the cell to A, and thus water can flow from the cell to the ocean.

For both “Pacific ocean” and “Atlantic ocean”, use the idea of breadth first search and for each cell, determine whether water can flow to the ocean starting from the cell.

If a cell can make water flow to both oceans, add it to the result list.

class Solution {
public List<List<Integer>> pacificAtlantic(int[][] matrix) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
return list;
int rows = matrix.length, columns = matrix[0].length;
boolean[][] pacificFlows = new boolean[rows][columns];
boolean[][] atlanticFlows = new boolean[rows][columns];
for (int i = 0; i < rows; i++) {
pacificFlows[i][0] = true;
pacificQueue.offer(new int[]{i, 0});
}
for (int i = 1; i < columns; i++) {
pacificFlows[0][i] = true;
pacificQueue.offer(new int[]{0, i});
}
for (int i = 0; i < rows; i++) {
atlanticFlows[i][columns - 1] = true;
atlanticQueue.offer(new int[]{i, columns - 1});
}
for (int i = 0; i < columns - 1; i++) {
atlanticFlows[rows - 1][i] = true;
atlanticQueue.offer(new int[]{rows - 1, i});
}
int[][] directions = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
while (!pacificQueue.isEmpty()) {
int[] cell = pacificQueue.poll();
int row = cell[0], column = cell[1];
int height = matrix[row][column];
for (int[] direction : directions) {
int newRow = row + direction[0], newColumn = column + direction[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns) {
if (matrix[newRow][newColumn] >= height && !pacificFlows[newRow][newColumn]) {
pacificFlows[newRow][newColumn] = true;
pacificQueue.offer(new int[]{newRow, newColumn});
}
}
}
}
while (!atlanticQueue.isEmpty()) {
int[] cell = atlanticQueue.poll();
int row = cell[0], column = cell[1];
int height = matrix[row][column];
for (int[] direction : directions) {
int newRow = row + direction[0], newColumn = column + direction[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns) {
if (matrix[newRow][newColumn] >= height && !atlanticFlows[newRow][newColumn]) {
atlanticFlows[newRow][newColumn] = true;
atlanticQueue.offer(new int[]{newRow, newColumn});
}
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (pacificFlows[i][j] && atlanticFlows[i][j]) {
List<Integer> coordinate = new ArrayList<Integer>();