From 5553ec7eccc006df866e985fcc176ce6319c0d12 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 20 May 2024 19:04:12 +0800 Subject: [PATCH] update 200 java, progress --- data/progress.txt | 2 +- data/to_review.txt | 18 +++--- .../LeetCodeJava/DFS/NumberOfIslands.java | 59 +++++++++++++++++- .../src/main/java/dev/workspace3.java | 61 +++++++++++++++++++ 4 files changed, 128 insertions(+), 12 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index e7c25b4d..fb9739a1 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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 diff --git a/data/to_review.txt b/data/to_review.txt index f90b8c97..5a3922db 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] @@ -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'] @@ -20,7 +20,7 @@ 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'] @@ -28,19 +28,19 @@ 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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java b/leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java index bcfef718..65e463e6 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java +++ b/leetcode_java/src/main/java/LeetCodeJava/DFS/NumberOfIslands.java @@ -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; } diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index e3238724..a3d6c5eb 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -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 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; + } + }