Skip to content

Commit

Permalink
update cheatsheet, code comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 1, 2023
1 parent b3e2176 commit b6d90b5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
7 changes: 7 additions & 0 deletions doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ boolean isPalindrome(String s, int low, int high) {
}
```

### 1-17) init 2D array
```java
// java
// LC 417
private static final int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
```

# 2) Other tricks

### 2-1) Init var, modify it in another method, and use it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

public class PacificAtlanticWaterFlow {

// TODO : fix below
// List<List<Integer>> res = new ArrayList<>();
// //boolean[][] seen;
//
Expand Down
45 changes: 34 additions & 11 deletions leetcode_python/Depth-First-Search/pacific-atlantic-water-flow.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@
"""
You are given an m x n integer matrix heights representing the height of each unit cell in a continent.
The Pacific ocean touches the continent's left and top edges,
and the Atlantic ocean touches the continent's right and bottom edges.
Water can only flow in four directions: up, down, left, and right.
Water flows from a cell to an adjacent one with an equal or lower height.
Return a list of grid coordinates where water can flow to both the Pacific and Atlantic oceans.
417. Pacific Atlantic Water Flow
Medium
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.
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).
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 result[i] = [ri, ci] denotes that rain water can flow from cell (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 = [[2,1],[1,2]]
Output: [[0,0],[0,1],[1,0],[1,1]]
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[i].length
n == heights[r].length
1 <= m, n <= 200
1 <= heights[i][j] <= 105
0 <= heights[r][c] <= 105
"""
# V0
Expand Down

0 comments on commit b6d90b5

Please sign in to comment.