417. Pacific Atlantic Water Flow
There is an m × n m \times n m×n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island’s left and top edges, and the Atlantic Ocean touches the island’s right and bottom edges.
The island is partitioned into a grid of square cells. You are given an m × n m \times n m×n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell’s height is less than or equal to the current cell’s height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates result where r e s u l t [ i ] = [ r i , c i ] result[i] = [r_i, c_i] result[i]=[ri,ci] denotes that rain water can flow from cell ( r i , c i ) (r_i, c_i) (ri,ci) to both the Pacific and Atlantic oceans.
Example 1:
Input: heights = [[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]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
Constraints:
- m == heights.length
- n == heights[r].length
- 1 <= m, n <= 200
- 0 < = h e i g h t s [ r ] [ c ] < = 1 0 5 0 <= heights[r][c] <= 10^5 0<=heights[r][c]<=105
From: LeetCode
Link: 417. Pacific Atlantic Water Flow
Solution:
Ideas:
1. DFS (Depth-First Search) Function:
- The dfs function explores all possible paths where water can flow. It uses a visited matrix to track which cells have been visited and continues exploring only if the neighboring cell is higher or equal in height.
2. Pacific and Atlantic Matrices:
- Two matrices (pacific and atlantic) are used to keep track of which cells can flow to the respective oceans. These matrices are initialized to zero and updated as DFS explores the grid.
3. Running DFS for Border Cells:
- DFS is run starting from the cells along the edges of the grid. All cells in the top row and left column can flow to the Pacific, and all cells in the bottom row and right column can flow to the Atlantic.
4. Storing Result:
- The result stores all the cells that can flow to both oceans, which is done by checking if both pacific[i][j] and atlantic[i][j] are marked as reachable.
Code:
#define MAX(a, b) ((a) > (b) ? (a) : (b))
// Directions to move in the grid (up, down, left, right)
int directions[4][2] = {
{
-1, 0}, {
1, 0}, {
0, -1}, {
0, 1}};
// Helper function to perform DFS
void dfs(int** heights, int heightsSize, int* heightsColSize, int r, int c, int** visited) {
visited[r][c] = 1;
for (int i = 0; i < 4; i++) {
int newRow = r + directions[i][0];
int newCol = c + directions[i][1];
if (newRow >= 0 && newRow < heightsSize && newCol >= 0 && newCol < heightsColSize[r] &&
!visited[newRow][newCol] && heights[newRow][newCol] >= heights[r][c]) {
dfs(heights, heightsSize, heightsColSize, newRow, newCol, visited);
}
}
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** pacificAtlantic(int** heights, int heightsSize, int* heightsColSize, int* returnSize, int** returnColumnSizes) {
if (heightsSize == 0 || heightsColSize[0] == 0) {
*returnSize = 0;
*returnColumnSizes = NULL;
return NULL;
}
int m = heightsSize, n = heightsColSize[0];
// Create two matrices to mark cells that can reach Pacific and Atlantic oceans
int** pacific = (int**)malloc(m * sizeof(int*));
int** atlantic = (int**)malloc(m * sizeof(int*));
for (int i = 0; i < m; i++) {
pacific[i] = (int*)calloc(n, sizeof(int));
atlantic[i] = (int*)calloc(n, sizeof(int));
}
// Perform DFS for each cell adjacent to the Pacific and Atlantic oceans
for (int i = 0; i < m; i++) {
dfs(heights, heightsSize, heightsColSize, i, 0, pacific); // Pacific - Left column
dfs(heights, heightsSize, heightsColSize, i, n - 1, atlantic); // Atlantic - Right column
}
for (int j = 0; j < n; j++) {
dfs(heights, heightsSize, heightsColSize, 0, j, pacific); // Pacific - Top row
dfs(heights, heightsSize, heightsColSize, m - 1, j, atlantic); // Atlantic - Bottom row
}
// Prepare the result arrays
int** result = (int**)malloc(m * n * sizeof(int*));
*returnColumnSizes = (int*)malloc(m * n * sizeof(int));
*returnSize = 0;
// Collect cells that can reach both oceans
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result[*returnSize] = (int*)malloc(2 * sizeof(int));
result[*returnSize][0] = i;
result[*returnSize][1] = j;
(*returnColumnSizes)[*returnSize] = 2;
(*returnSize)++;
}
}
}
// Free allocated memory for visited arrays
for (int i = 0; i < m; i++) {
free(pacific[i]);
free(atlantic[i]);
}
free(pacific);
free(atlantic);
return result;
}