Skip to content

Commit

Permalink
add 417 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 1, 2023
1 parent bdce593 commit 7e72222
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@
364| [Nested List Weight Sum II](https://leetcode.com/problems/nested-list-weight-sum-ii/) | [Python](./leetcode_python/Depth-First-Search/nested-list-weight-sum-ii.py) | _O(n)_ | _O(h)_ | Medium |🔒, `linkedin`| AGAIN*
366| [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) | [Python](./leetcode_python/Depth-First-Search/find-leaves-of-binary-tree.py) | _O(n)_ | _O(h)_ | Medium |🔒, `linkedin`| AGAIN*
399| [Evaluate Division](https://leetcode.com/problems/evaluate-division/) | [Python](./leetcode_python/Depth-First-Search/evaluate-division.py) | _O(q * \|V\|!)_ | _O(e)_ | Medium |`union find`, `dfs`, `AGAIN`, `good trick` `google`, `fb`| AGAIN*********** (6)
417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) | [Python](./leetcode_python/Depth-First-Search/pacific-atlantic-water-flow.py) | _O(m * n)_ | _O(m * n)_ | Medium |Curated Top 75, 2 direction dfs, dfs, matrix, `m$`,`google`| AGAIN** (3)
417 | [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) | [Python](./leetcode_python/Depth-First-Search/pacific-atlantic-water-flow.py), [Java](./LeetCodeJava/DFS/PacificAtlanticWaterFlow.java) | _O(m * n)_ | _O(m * n)_ | Medium |Curated Top 75, 2 direction dfs, dfs, matrix, `m$`,`google`| AGAIN** (3)
464| [Can I Win](https://leetcode.com/problems/can-i-win/) | [Python](./leetcode_python/Depth-First-Search/can-i-win.py) | _O(n!)_ | _O(n)_ | Medium |`DP`, `linkedin`| AGAIN* (not start)
491| [Increasing Subsequences](https://leetcode.com/problems/increasing-subsequences/) | [Python](./leetcode_python/Depth-First-Search/increasing_subsequences.py) | | | Medium |`trick`,`dfs`,`dfs good trick`, `dp`,`yahoo` ,`fb`| AGAIN***** (2)
515| [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [Python](./leetcode_python/Depth-First-Search/find-largest-value-in-each-tree-row.py) | _O(n)_ | _O(h)_ | Medium |`good basic`,`linkedin`| AGAIN* (3)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20231001: 417
20230930: 133,695
20230903: 654,106,105
20230830: 200
Expand Down
13 changes: 9 additions & 4 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
2023-11-25 -> ['417']
2023-11-24 -> ['133,695']
2023-11-04 -> ['417']
2023-11-03 -> ['133,695']
2023-10-28 -> ['654,106,105']
2023-10-24 -> ['200']
2023-10-22 -> ['417']
2023-10-21 -> ['133,695', '131,17']
2023-10-16 -> ['79']
2023-10-15 -> ['40']
2023-10-14 -> ['90']
2023-10-14 -> ['417', '90']
2023-10-13 -> ['133,695', '46']
2023-10-09 -> ['417']
2023-10-08 -> ['133,695']
2023-10-07 -> ['654,106,105', '78,39']
2023-10-06 -> ['355']
2023-10-06 -> ['417', '355']
2023-10-05 -> ['133,695', '621']
2023-10-03 -> ['133,695', '200', '973,215']
2023-10-02 -> ['133,695']
2023-10-04 -> ['417']
2023-10-03 -> ['417', '133,695', '200', '973,215']
2023-10-02 -> ['417', '133,695']
2023-10-01 -> ['133,695']
2023-09-30 -> ['131,17']
2023-09-25 -> ['79']
Expand Down
7 changes: 7 additions & 0 deletions doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
# 1) Basic op


### 0-0)
- [Arrays.asList vs new ArrayList()](https://www.baeldung.com/java-arrays-aslist-vs-new-arraylist#:~:text=asList%20method%20returns%20a%20type,the%20add%20and%20remove%20methods.)

- Conclusion:
- Arrays.asList : only wrap existing array, it NOT implement "add", "remove" methods (but has modify method)
- new ArrayList : implement "add", "remove" and "modify" methods, not affect original array

### 1-0) String to Char array
```java
// java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package LeetCodeJava.DFS;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class PacificAtlanticWaterFlow {

// List<List<Integer>> res = new ArrayList<>();
// //boolean[][] seen;
//
// // V0
// public List<List<Integer>> pacificAtlantic(int[][] heights) {
//
// // double check ??
// if (heights.length == 1 && heights[0].length == 1) {
// List<Integer> _ans = new ArrayList<>();
// _ans.add(0);
// _ans.add(0);
// this.res.add(_ans);
// return this.res;
// }
//
// int len = heights.length;
// int width = heights[0].length;
//
// for (int x = 0; x < width; x++) {
// for (int y = 0; y < len; y++) {
//
// boolean[][] seen = new boolean[len][width];
// String status = _help(heights, x, y, 0, 0, seen, null);
// System.out.println(">>> status = " + status + " (x,y) = " + x + ", " + y);
//
// if (status != null && status.contains("a") && status.contains("p")) {
// List<Integer> _cur = new ArrayList<>();
// _cur.add(x);
// _cur.add(y);
// this.res.add(_cur);
// }
// }
// }
//
// return this.res;
// }
//
// private String _help(int[][] heights, int x, int y, int lastX, int lastY, boolean[][] seen, String status) {
//
// int len = heights.length;
// int width = heights[0].length;
//
// if (y == 0 || x == 0){
// status += "-p";
// }
//
// if (y == len-1 || x == width-1){
// status += "-a";
// }
//
// seen[y][x] = true;
//
// if (x >= width || x < 0 || y >= len || y < 0 || seen[y][x] == true || heights[lastY][lastX] < heights[y][x]) {
// return status;
// }
//
// return _help(heights, x+1, y, x, y, seen, status) +
// _help(heights, x-1, y, x, y, seen, status)+
// _help(heights, x, y+1, x, y, seen, status)+
// _help(heights, x, y-1, x, y, seen, status);
// }

// V1
// IDEA : DFS
// https://leetcode.com/problems/pacific-atlantic-water-flow/editorial/
private static final int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
private int numRows;
private int numCols;
private int[][] landHeights;

public List<List<Integer>> pacificAtlantic_1(int[][] matrix) {
// Check if input is empty
if (matrix.length == 0 || matrix[0].length == 0) {
return new ArrayList<>();
}

// Save initial values to parameters
numRows = matrix.length;
numCols = matrix[0].length;
landHeights = matrix;
boolean[][] pacificReachable = new boolean[numRows][numCols];
boolean[][] atlanticReachable = new boolean[numRows][numCols];

// Loop through each cell adjacent to the oceans and start a DFS
for (int i = 0; i < numRows; i++) {
dfs(i, 0, pacificReachable);
dfs(i, numCols - 1, atlanticReachable);
}
for (int i = 0; i < numCols; i++) {
dfs(0, i, pacificReachable);
dfs(numRows - 1, i, atlanticReachable);
}

// Find all cells that can reach both oceans
List<List<Integer>> commonCells = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (pacificReachable[i][j] && atlanticReachable[i][j]) {
// TODO : fix below
//commonCells.add(List.of(i, j));
}
}
}
return commonCells;
}

private void dfs(int row, int col, boolean[][] reachable) {
// This cell is reachable, so mark it
reachable[row][col] = true;
for (int[] dir : DIRECTIONS) { // Check all 4 directions
int newRow = row + dir[0];
int newCol = col + dir[1];
// Check if new cell is within bounds
if (newRow < 0 || newRow >= numRows || newCol < 0 || newCol >= numCols) {
continue;
}
// Check that the new cell hasn't already been visited
if (reachable[newRow][newCol]) {
continue;
}
// Check that the new cell has a higher or equal height,
// So that water can flow from the new cell to the old cell
if (landHeights[newRow][newCol] < landHeights[row][col]) {
continue;
}
// If we've gotten this far, that means the new cell is reachable
dfs(newRow, newCol, reachable);
}
}

// V2
// IDEA : BFS
// https://leetcode.com/problems/pacific-atlantic-water-flow/editorial/
private static final int[][] DIRECTIONS_ = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
private int numRows_;
private int numCols_;
private int[][] landHeights_;

public List<List<Integer>> pacificAtlantic_2(int[][] matrix) {
// Check if input is empty
if (matrix.length == 0 || matrix[0].length == 0) {
return new ArrayList<>();
}

// Save initial values to parameters
numRows_ = matrix.length;
numCols_ = matrix[0].length;
landHeights_ = matrix;

// Setup each queue with cells adjacent to their respective ocean
Queue<int[]> pacificQueue = new LinkedList<>();
Queue<int[]> atlanticQueue = new LinkedList<>();
for (int i = 0; i < numRows_; i++) {
pacificQueue.offer(new int[]{i, 0});
atlanticQueue.offer(new int[]{i, numCols_ - 1});
}
for (int i = 0; i < numCols_; i++) {
pacificQueue.offer(new int[]{0, i});
atlanticQueue.offer(new int[]{numRows_ - 1, i});
}

// Perform a BFS for each ocean to find all cells accessible by each ocean
boolean[][] pacificReachable = bfs(pacificQueue);
boolean[][] atlanticReachable = bfs(atlanticQueue);

// Find all cells that can reach both oceans
List<List<Integer>> commonCells = new ArrayList<>();
for (int i = 0; i < numRows_; i++) {
for (int j = 0; j < numCols_; j++) {
if (pacificReachable[i][j] && atlanticReachable[i][j]) {
// TODO : fix below
//commonCells.add(List.of(i, j));
}
}
}
return commonCells;
}

private boolean[][] bfs(Queue<int[]> queue) {
boolean[][] reachable = new boolean[numRows_][numCols_];
while (!queue.isEmpty()) {
int[] cell = queue.poll();
// This cell is reachable, so mark it
reachable[cell[0]][cell[1]] = true;
for (int[] dir : DIRECTIONS_) { // Check all 4 directions
int newRow = cell[0] + dir[0];
int newCol = cell[1] + dir[1];
// Check if new cell is within bounds
if (newRow < 0 || newRow >= numRows_ || newCol < 0 || newCol >= numCols_) {
continue;
}
// Check that the new cell hasn't already been visited
if (reachable[newRow][newCol]) {
continue;
}
// Check that the new cell has a higher or equal height,
// So that water can flow from the new cell to the old cell
if (landHeights_[newRow][newCol] < landHeights_[cell[0]][cell[1]]) {
continue;
}
// If we've gotten this far, that means the new cell is reachable
queue.offer(new int[]{newRow, newCol});
}
}
return reachable;
}

}

0 comments on commit 7e72222

Please sign in to comment.