Skip to content

Commit 8a12698

Browse files
chore: add LeetCode daily solution
1 parent 621d23e commit 8a12698

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Pacific Atlantic Water Flow (Medium)
2+
3+
**Problem ID:** 417
4+
**Date:** 2025-10-05
5+
**Link:** https://leetcode.com/problems/pacific-atlantic-water-flow/
6+
7+
## Approach
8+
9+
To solve the "Pacific Atlantic Water Flow" problem, we can utilize a depth-first search (DFS) or breadth-first search (BFS) approach to explore the grid and determine which cells can flow to both oceans. The main idea is to perform two separate searches: one starting from the cells adjacent to the Pacific Ocean and another from the cells adjacent to the Atlantic Ocean.
10+
11+
### Approach:
12+
13+
1. **Initialization**:
14+
- Create two boolean matrices, `pacific_reachable` and `atlantic_reachable`, both of the same dimensions as the input matrix. These will keep track of which cells can reach the Pacific and Atlantic oceans, respectively.
15+
16+
2. **DFS/BFS Traversal**:
17+
- For each ocean, start from the border cells that are adjacent to that ocean. For the Pacific Ocean, this includes the top row and the left column; for the Atlantic Ocean, this includes the bottom row and the right column.
18+
- Use a DFS or BFS to explore from these starting cells. During the traversal:
19+
- Mark the current cell as reachable for the corresponding ocean.
20+
- For each neighboring cell (up, down, left, right), check if it can be reached (i.e., its height is less than or equal to the current cell's height). If it can be reached and hasn't been visited yet, continue the search from that cell.
21+
22+
3. **Collect Results**:
23+
- After both searches are complete, iterate through the grid and collect the coordinates of cells that are marked as reachable in both `pacific_reachable` and `atlantic_reachable`.
24+
25+
### Data Structures:
26+
- Two 2D boolean arrays for tracking reachability.
27+
- A stack or queue for implementing the DFS or BFS.
28+
29+
### Complexity:
30+
- **Time Complexity**: O(m * n), where m is the number of rows and n is the number of columns. Each cell is visited at most twice (once for each ocean).
31+
- **Space Complexity**: O(m * n) for the two boolean matrices used to track reachability.
32+
33+
This approach efficiently determines the cells from which water can flow to both oceans by leveraging the properties of DFS/BFS to explore the grid based on height constraints.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Solution {
5+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
6+
if (heights == null || heights.length == 0 || heights[0].length == 0) {
7+
return new ArrayList<>();
8+
}
9+
10+
int m = heights.length, n = heights[0].length;
11+
boolean[][] pacific = new boolean[m][n];
12+
boolean[][] atlantic = new boolean[m][n];
13+
List<List<Integer>> result = new ArrayList<>();
14+
15+
for (int i = 0; i < m; i++) {
16+
dfs(heights, pacific, i, 0); // Pacific Ocean
17+
dfs(heights, atlantic, i, n - 1); // Atlantic Ocean
18+
}
19+
20+
for (int j = 0; j < n; j++) {
21+
dfs(heights, pacific, 0, j); // Pacific Ocean
22+
dfs(heights, atlantic, m - 1, j); // Atlantic Ocean
23+
}
24+
25+
for (int i = 0; i < m; i++) {
26+
for (int j = 0; j < n; j++) {
27+
if (pacific[i][j] && atlantic[i][j]) {
28+
List<Integer> cell = new ArrayList<>();
29+
cell.add(i);
30+
cell.add(j);
31+
result.add(cell);
32+
}
33+
}
34+
}
35+
36+
return result;
37+
}
38+
39+
private void dfs(int[][] heights, boolean[][] ocean, int i, int j) {
40+
ocean[i][j] = true;
41+
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
42+
43+
for (int[] dir : directions) {
44+
int ni = i + dir[0], nj = j + dir[1];
45+
if (ni >= 0 && ni < heights.length && nj >= 0 && nj < heights[0].length
46+
&& !ocean[ni][nj] && heights[ni][nj] >= heights[i][j]) {
47+
dfs(heights, ocean, ni, nj);
48+
}
49+
}
50+
}
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var pacificAtlantic = function(heights) {
2+
if (!heights.length || !heights[0].length) return [];
3+
4+
const m = heights.length, n = heights[0].length;
5+
const pacific = Array.from({ length: m }, () => Array(n).fill(false));
6+
const atlantic = Array.from({ length: m }, () => Array(n).fill(false));
7+
const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
8+
9+
const dfs = (r, c, visited, prevHeight) => {
10+
if (r < 0 || r >= m || c < 0 || c >= n || visited[r][c] || heights[r][c] < prevHeight) {
11+
return;
12+
}
13+
visited[r][c] = true;
14+
for (const [dr, dc] of directions) {
15+
dfs(r + dr, c + dc, visited, heights[r][c]);
16+
}
17+
};
18+
19+
for (let i = 0; i < m; i++) {
20+
dfs(i, 0, pacific, heights[i][0]); // Pacific left edge
21+
dfs(i, n - 1, atlantic, heights[i][n - 1]); // Atlantic right edge
22+
}
23+
for (let j = 0; j < n; j++) {
24+
dfs(0, j, pacific, heights[0][j]); // Pacific top edge
25+
dfs(m - 1, j, atlantic, heights[m - 1][j]); // Atlantic bottom edge
26+
}
27+
28+
const result = [];
29+
for (let i = 0; i < m; i++) {
30+
for (let j = 0; j < n; j++) {
31+
if (pacific[i][j] && atlantic[i][j]) {
32+
result.push([i, j]);
33+
}
34+
}
35+
}
36+
return result;
37+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
3+
if not heights:
4+
return []
5+
6+
m, n = len(heights), len(heights[0])
7+
pacific_reachable = [[False] * n for _ in range(m)]
8+
atlantic_reachable = [[False] * n for _ in range(m)]
9+
10+
def dfs(r, c, reachable):
11+
reachable[r][c] = True
12+
for dr, dc in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
13+
nr, nc = r + dr, c + dc
14+
if 0 <= nr < m and 0 <= nc < n and not reachable[nr][nc] and heights[nr][nc] >= heights[r][c]:
15+
dfs(nr, nc, reachable)
16+
17+
for i in range(m):
18+
dfs(i, 0, pacific_reachable)
19+
dfs(i, n - 1, atlantic_reachable)
20+
21+
for j in range(n):
22+
dfs(0, j, pacific_reachable)
23+
dfs(m - 1, j, atlantic_reachable)
24+
25+
result = []
26+
for r in range(m):
27+
for c in range(n):
28+
if pacific_reachable[r][c] and atlantic_reachable[r][c]:
29+
result.append([r, c])
30+
31+
return result

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,4 @@ Each problem includes:
167167
- 2025-10-02 — [Water Bottles II](https://leetcode.com/problems/water-bottles-ii/) (Medium) → `Medium/2025-10-02-3100-Water-Bottles-II`
168168
- 2025-10-03 — [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) (Hard) → `Hard/2025-10-03-407-Trapping-Rain-Water-II`
169169
- 2025-10-04 — [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) (Medium) → `Medium/2025-10-04-11-Container-With-Most-Water`
170+
- 2025-10-05 — [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) (Medium) → `Medium/2025-10-05-417-Pacific-Atlantic-Water-Flow`

0 commit comments

Comments
 (0)