Skip to content

Commit 1bf4f47

Browse files
committed
add 417
1 parent bcd8265 commit 1bf4f47

File tree

3 files changed

+182
-4
lines changed

3 files changed

+182
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
My personal leetcode answers<br/>
66
This is a **continually updated** open source project<br/>
77
<br/>
8-
Total sovled: **332**<br/>
9-
Auto updated at: **2024-07-05 14:17:19**<br/>
8+
Total sovled: **333**<br/>
9+
Auto updated at: **2024-07-05 15:47:04**<br/>
1010

1111
## 软件/Softwares
1212
- [Anki](https://apps.ankiweb.net/)
@@ -209,17 +209,18 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
209209
| [Lintcode-1534. Convert Binary Search Tree To Sorted Doubly Linked List](https://www.lintcode.com/problem/convert-binary-search-tree-to-sorted-doubly-linked-list) | [c++](./lintcode/1534.convert-binary-search-tree-to-sorted-doubly-linked-list.cpp), [python](./lintcode/1534.convert-binary-search-tree-to-sorted-doubly-linked-list.py) | O\(N\) | O\(Height\) | BST \| Leetcode\-426 | - |
210210

211211
## Breadth-First Search
212-
| Problem(6) | Solution | Time | Space | Note | Ref |
212+
| Problem(7) | Solution | Time | Space | Note | Ref |
213213
| ----- | ----- | ----- | ----- | ----- | ----- |
214214
| [Leetcode-102. Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/) | [c++](./leetcode/102.binary-tree-level-order-traversal.cpp), [python](./leetcode/102.binary-tree-level-order-traversal.py) | O\(N\) | O\(Width\) | Level | - |
215215
| [Leetcode-104. Maximum Depth Of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) | [c++](./leetcode/104.maximum-depth-of-binary-tree.cpp), [python](./leetcode/104.maximum-depth-of-binary-tree.py) | O\(N\) | O\(Height\) | Recursion | - |
216216
| [Leetcode-297. Serialize And Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) | [c++](./leetcode/297.serialize-and-deserialize-binary-tree.cpp), [python](./leetcode/297.serialize-and-deserialize-binary-tree.py) | O\(N\) | O\(N\) | Serialization | - |
217217
| [Leetcode-322. Coin Change](https://leetcode.com/problems/coin-change/description/) | [c++](./leetcode/322.coin-change.cpp), [python](./leetcode/322.coin-change.py) | O\(K \* N\) | O\(N\) | Index | [Video](https://youtu.be/EjMjlFjLRiM) |
218+
| [Leetcode-417. Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | [c++](./leetcode/417.pacific-atlantic-water-flow.cpp), [python](./leetcode/417.pacific-atlantic-water-flow.py) | O\(MN\) | O\(MN\) | \- | - |
218219
| [Leetcode-547. Number Of Provinces](https://leetcode.com/problems/number-of-provinces/description/) | [c++](./leetcode/547.number-of-provinces.cpp), [python](./leetcode/547.number-of-provinces.py) | O\(N^2\) | O\(N\) | \- | - |
219220
| [Leetcode-695. Max Area Of Island](https://leetcode.com/problems/max-area-of-island/description/) | [c++](./leetcode/695.max-area-of-island.cpp), [python](./leetcode/695.max-area-of-island.py) | O\(MN\) | O\(MN\) | \- | - |
220221

221222
## Depth-First Search
222-
| Problem(13) | Solution | Time | Space | Note | Ref |
223+
| Problem(14) | Solution | Time | Space | Note | Ref |
223224
| ----- | ----- | ----- | ----- | ----- | ----- |
224225
| [Leetcode-94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) | [c++](./leetcode/94.binary-tree-inorder-traversal.cpp), [python](./leetcode/94.binary-tree-inorder-traversal.py) | O\(N\) | O\(Height\) | InOrder | - |
225226
| [Leetcode-98. Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/) | [c++](./leetcode/98.validate-binary-search-tree.cpp), [python](./leetcode/98.validate-binary-search-tree.py) | O\(N\) | O\(Height\) | BST | - |
@@ -231,6 +232,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
231232
| [Leetcode-236. Lowest Common Ancestor Of A Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) | [c++](./leetcode/236.lowest-common-ancestor-of-a-binary-tree.cpp), [python](./leetcode/236.lowest-common-ancestor-of-a-binary-tree.py) | O\(N\) | O\(Height\) | Recursion | - |
232233
| [Leetcode-257. Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/description/) | [c++](./leetcode/257.binary-tree-paths.cpp), [python](./leetcode/257.binary-tree-paths.py) | O\(N\) | O\(Height\) | \- | - |
233234
| [Leetcode-297. Serialize And Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/) | [c++](./leetcode/297.serialize-and-deserialize-binary-tree.cpp), [python](./leetcode/297.serialize-and-deserialize-binary-tree.py) | O\(N\) | O\(N\) | Serialization | - |
235+
| [Leetcode-417. Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/description/) | [c++](./leetcode/417.pacific-atlantic-water-flow.cpp), [python](./leetcode/417.pacific-atlantic-water-flow.py) | O\(MN\) | O\(MN\) | \- | - |
234236
| [Leetcode-547. Number Of Provinces](https://leetcode.com/problems/number-of-provinces/description/) | [c++](./leetcode/547.number-of-provinces.cpp), [python](./leetcode/547.number-of-provinces.py) | O\(N^2\) | O\(N\) | \- | - |
235237
| [Leetcode-695. Max Area Of Island](https://leetcode.com/problems/max-area-of-island/description/) | [c++](./leetcode/695.max-area-of-island.cpp), [python](./leetcode/695.max-area-of-island.py) | O\(MN\) | O\(MN\) | \- | - |
236238
| [Lintcode-11. Search Range In Binary Search Tree](https://www.lintcode.com/problem/search-range-in-binary-search-tree) | [c++](./lintcode/11.search-range-in-binary-search-tree.cpp), [python](./lintcode/11.search-range-in-binary-search-tree.py) | O\(N\) | O\(Height\) | InOrder | - |
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Tag: Array, Depth-First Search, Breadth-First Search, Matrix
2+
// Time: O(MN)
3+
// Space: O(MN)
4+
// Ref: -
5+
// Note: -
6+
7+
// There is an m x 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.
8+
// The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).
9+
// 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.
10+
// Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.
11+
//  
12+
// Example 1:
13+
//
14+
//
15+
// 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]]
16+
// Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
17+
// Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
18+
// [0,4]: [0,4] -> Pacific Ocean
19+
//   [0,4] -> Atlantic Ocean
20+
// [1,3]: [1,3] -> [0,3] -> Pacific Ocean
21+
//   [1,3] -> [1,4] -> Atlantic Ocean
22+
// [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
23+
//   [1,4] -> Atlantic Ocean
24+
// [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
25+
//   [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
26+
// [3,0]: [3,0] -> Pacific Ocean
27+
//   [3,0] -> [4,0] -> Atlantic Ocean
28+
// [3,1]: [3,1] -> [3,0] -> Pacific Ocean
29+
//   [3,1] -> [4,1] -> Atlantic Ocean
30+
// [4,0]: [4,0] -> Pacific Ocean
31+
// [4,0] -> Atlantic Ocean
32+
// Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
33+
//
34+
// Example 2:
35+
//
36+
// Input: heights = [[1]]
37+
// Output: [[0,0]]
38+
// Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
39+
//
40+
//  
41+
// Constraints:
42+
//
43+
// m == heights.length
44+
// n == heights[r].length
45+
// 1 <= m, n <= 200
46+
// 0 <= heights[r][c] <= 105
47+
//
48+
//
49+
50+
class Solution {
51+
public:
52+
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
53+
int m = heights.size();
54+
int n = heights[0].size();
55+
56+
vector<vector<bool>> pacific(m, vector(n, false));
57+
vector<vector<bool>> atlantic(m, vector(n, false));
58+
59+
for (int i = 0; i < m; i++) {
60+
dfs(heights, pacific, i, 0);
61+
dfs(heights, atlantic, i, n - 1);
62+
}
63+
64+
for (int j = 0; j < n; j++) {
65+
dfs(heights, pacific, 0, j);
66+
dfs(heights, atlantic, m - 1, j);
67+
}
68+
vector<vector<int>> res;
69+
for (int i = 0; i < m; i++) {
70+
for (int j = 0; j < n; j++) {
71+
if (pacific[i][j] && atlantic[i][j]) {
72+
res.push_back(vector<int>{i, j});
73+
}
74+
}
75+
}
76+
77+
return res;
78+
}
79+
80+
void dfs(vector<vector<int>>& heights, vector<vector<bool>>&visited, int x, int y) {
81+
if (visited[x][y]) {
82+
return;
83+
}
84+
85+
visited[x][y] = true;
86+
int directions[5] = {-1, 0, 1, 0, -1};
87+
for (int i = 0; i < 4; i++) {
88+
int dx = x + directions[i];
89+
int dy = y + directions[i + 1];
90+
91+
if (dx >= 0 && dx < heights.size() && dy >= 0 && dy < heights[dx].size() && heights[dx][dy] >= heights[x][y]) {
92+
dfs(heights, visited, dx, dy);
93+
}
94+
}
95+
96+
}
97+
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Tag: Array, Depth-First Search, Breadth-First Search, Matrix
2+
# Time: O(MN)
3+
# Space: O(MN)
4+
# Ref: -
5+
# Note: -
6+
7+
# There is an m x 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.
8+
# The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).
9+
# 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.
10+
# Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.
11+
#  
12+
# Example 1:
13+
#
14+
#
15+
# 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]]
16+
# Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
17+
# Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
18+
# [0,4]: [0,4] -> Pacific Ocean
19+
#   [0,4] -> Atlantic Ocean
20+
# [1,3]: [1,3] -> [0,3] -> Pacific Ocean
21+
#   [1,3] -> [1,4] -> Atlantic Ocean
22+
# [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
23+
#   [1,4] -> Atlantic Ocean
24+
# [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
25+
#   [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
26+
# [3,0]: [3,0] -> Pacific Ocean
27+
#   [3,0] -> [4,0] -> Atlantic Ocean
28+
# [3,1]: [3,1] -> [3,0] -> Pacific Ocean
29+
#   [3,1] -> [4,1] -> Atlantic Ocean
30+
# [4,0]: [4,0] -> Pacific Ocean
31+
# [4,0] -> Atlantic Ocean
32+
# Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
33+
#
34+
# Example 2:
35+
#
36+
# Input: heights = [[1]]
37+
# Output: [[0,0]]
38+
# Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
39+
#
40+
#  
41+
# Constraints:
42+
#
43+
# m == heights.length
44+
# n == heights[r].length
45+
# 1 <= m, n <= 200
46+
# 0 <= heights[r][c] <= 105
47+
#
48+
#
49+
50+
class Solution:
51+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
52+
pacific = set()
53+
atlantic = set()
54+
55+
m = len(heights)
56+
n = len(heights[0])
57+
for i in range(m):
58+
self.dfs(heights, pacific, i, 0)
59+
self.dfs(heights, atlantic, i, n - 1)
60+
61+
for j in range(n):
62+
self.dfs(heights, pacific, 0, j)
63+
self.dfs(heights, atlantic, m - 1, j)
64+
65+
66+
return list(pacific & atlantic)
67+
68+
def dfs(self, heights: List[List[int]], visited: set, x: int, y: int):
69+
if (x, y) in visited:
70+
return
71+
72+
visited.add((x, y))
73+
directions = [-1, 0, 1, 0, -1]
74+
for i in range(4):
75+
dx = x + directions[i]
76+
dy = y + directions[i + 1]
77+
78+
if 0 <= dx < len(heights) and 0 <= dy < len(heights[dx]) and heights[dx][dy] >= heights[x][y]:
79+
self.dfs(heights, visited, dx, dy)

0 commit comments

Comments
 (0)