Skip to content

Commit

Permalink
update 200 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 20, 2024
1 parent cdd3b2e commit 5553ec7
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 12 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240520: 73
20240520: 73,200
20240519: 207,79,206,213,198
20240518: 212(todo),211,338,208(again)
20240517: 347,253(todo),91(todo),217
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-07-14 -> ['73']
2024-07-14 -> ['73,200']
2024-07-13 -> ['207,79,206,213,198']
2024-07-12 -> ['212(todo),211,338,208(again)']
2024-07-11 -> ['347,253(todo),91(todo),217']
Expand All @@ -9,7 +9,7 @@
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-23 -> ['73']
2024-06-23 -> ['73,200']
2024-06-22 -> ['207,79,206,213,198']
2024-06-21 -> ['212(todo),211,338,208(again)']
2024-06-20 -> ['347,253(todo),91(todo),217']
Expand All @@ -20,27 +20,27 @@
2024-06-15 -> ['371']
2024-06-14 -> ['121,252']
2024-06-13 -> ['125']
2024-06-10 -> ['73']
2024-06-10 -> ['73,200']
2024-06-09 -> ['207,79,206,213,198']
2024-06-08 -> ['212(todo),211,338,208(again)']
2024-06-07 -> ['347,253(todo),91(todo),217']
2024-06-06 -> ['226,98,253(todo)']
2024-06-05 -> ['104,230,102,100']
2024-06-04 -> ['105,106']
2024-06-03 -> ['242,235']
2024-06-02 -> ['73', '371']
2024-06-02 -> ['73,200', '371']
2024-06-01 -> ['207,79,206,213,198', '121,252']
2024-05-31 -> ['212(todo),211,338,208(again)', '125']
2024-05-30 -> ['347,253(todo),91(todo),217']
2024-05-29 -> ['226,98,253(todo)']
2024-05-28 -> ['73', '104,230,102,100']
2024-05-28 -> ['73,200', '104,230,102,100']
2024-05-27 -> ['207,79,206,213,198', '105,106']
2024-05-26 -> ['212(todo),211,338,208(again)', '242,235']
2024-05-25 -> ['73', '347,253(todo),91(todo),217', '371']
2024-05-25 -> ['73,200', '347,253(todo),91(todo),217', '371']
2024-05-24 -> ['207,79,206,213,198', '226,98,253(todo)', '121,252']
2024-05-23 -> ['73', '212(todo),211,338,208(again)', '104,230,102,100', '125']
2024-05-22 -> ['73', '207,79,206,213,198', '347,253(todo),91(todo),217', '105,106']
2024-05-21 -> ['73', '207,79,206,213,198', '212(todo),211,338,208(again)', '226,98,253(todo)', '242,235']
2024-05-23 -> ['73,200', '212(todo),211,338,208(again)', '104,230,102,100', '125']
2024-05-22 -> ['73,200', '207,79,206,213,198', '347,253(todo),91(todo),217', '105,106']
2024-05-21 -> ['73,200', '207,79,206,213,198', '212(todo),211,338,208(again)', '226,98,253(todo)', '242,235']
2024-05-20 -> ['207,79,206,213,198', '212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '104,230,102,100', '371']
2024-05-19 -> ['212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252']
2024-05-18 -> ['347,253(todo),91(todo),217', '226,98,253(todo)', '104,230,102,100', '242,235', '125']
Expand Down
59 changes: 57 additions & 2 deletions leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,67 @@ private boolean _is_island(char[][] grid, int x, int y, boolean[][] seen){
return true;
}

// V0
// V0'
// IDEA: DFS (with looping)
public int numIslands_0(char[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}

int l = grid.length;
int w = grid[0].length;
int res = 0;

for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
/**
* NOTE !!!
*
* if grid[i][j] == '1', no need to collect the coordinate (x,y),
* -> just add res with 1,
* -> and call dfs function
*/
if (grid[i][j] == '1') {
res += 1;
dfs(grid, i, j);
}
}
}

return res;
}

/** NOTE !!!
*
* NO NEED to return boolean val on this helper function (dfs),
* since we mark point as "visited" in place with traversing,
* so no response (void) is OK
*/
private void dfs(char[][] grid, int y, int x) {

int l = grid.length;
int w = grid[0].length;

if (y < 0 || y >= l || x < 0 || x >= w || grid[y][x] != '1') {
return;
}

grid[y][x] = '#'; // Mark the cell as visited

int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for (int[] dir : dirs) {
int newY = y + dir[0];
int newX = x + dir[1];
dfs(grid, newY, newX);
}
}

// V0'
// IDEA : DFS (with looping) (modified by GPT)
int num_island_2 = 0;
boolean[][] _seen_2;

public int numIslands_0(char[][] grid) {
public int numIslands_0_1(char[][] grid) {
if (grid.length == 1 && grid[0].length == 1) {
return grid[0][0] == '1' ? 1 : 0;
}
Expand Down
61 changes: 61 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -1126,4 +1126,65 @@ public void setZeroes(int[][] matrix) {
return;
}

// LC 200
// dfs
public int numIslands(char[][] grid) {

if (grid.length == 0 && grid[0].length == 0){
return 0;
}

int l = grid.length;
int w = grid[0].length;

// get "1"
List<int[]> collected = new ArrayList<>();
for (int i = 0; i < w; i++){
for (int j = 0; j < l; j++){
if (grid[j][i] == '1'){
collected.add(new int[]{i,j});
}
}
}

System.out.println("--> collected = ");
for (int[] point : collected){
System.out.println("x = " + point[0] + " y = " + point[1]);
}

int res = 0;
// dfs
for (int[] point : collected){
if (this.dfs_help(grid, point[0], point[1])){
res += 1;
}
}

return res;
}

public boolean dfs_help(char[][] grid, int x, int y){

int l = grid.length;
int w = grid[0].length;

// dirs
int[][] dirs = new int[][]{{0,1}, {0,-1}, {1,0}, {-1,0} };

if (x < 0 || x >= w || y < 0 || y >= l || grid[y][x] != '1'){
return false;
}

for (int[] dir: dirs){

int x_ = x + dir[0];
int y_ = y + dir[1];

// mark as visited
grid[y_][x_] = '#'; // NOTE !!! we setup char via single quote ('#')
this.dfs_help(grid, x_, y_);
}
return true;
}

}

0 comments on commit 5553ec7

Please sign in to comment.