From 0dd1649874fb9adf92ab8f8a9d19367c12e2efde Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sat, 30 Sep 2023 20:48:36 +0800 Subject: [PATCH] add 695 java, update progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +-- .../LeetCodeJava/DFS/MaxAreaOfIsland.java | 144 ++++++++++++++++++ 4 files changed, 155 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/DFS/MaxAreaOfIsland.java diff --git a/README.md b/README.md index 4e28b6d6..c192ce0d 100644 --- a/README.md +++ b/README.md @@ -920,7 +920,7 @@ 638| [Shopping Offers](https://leetcode.com/problems/shopping-offers/) | [Python](./leetcode_python/Depth-First-Search/shopping-offers.py) | _O(n * 2^n)_ | _O(n)_| Medium |`google`| AGAIN (not start*) 690| [Employee Importance](https://leetcode.com/problems/employee-importance/) |[Python](./leetcode_python/Depth-First-Search/employee-importance.py) | _O(n)_ | _O(h)_ | Easy | DFS, BFS,`good basic` ,`UBER`| OK** (3) 694| [Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/) | [Python](./leetcode_python/Depth-First-Search/number-of-distinct-islands.py) | _O(m * n)_ | _O(m * n)_ | Medium |`#200, #711 Number of Islands` good pattern, dfs, 🔒, compare with `# 200 Number of Islands`, `amazon`| AGAIN******** (5) -695| [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Python](./leetcode_python/Depth-First-Search/max-area-of-island.py) | _O(m * n)_ | _O(m * n)_ | Easy |`amazon`, `microsoft`, `linkedin`, `basic`| AGAIN* (3) +695| [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Python](./leetcode_python/Depth-First-Search/max-area-of-island.py), [Java](./LeetCodeJava/DFS/MaxAreaOfIsland.java) | _O(m * n)_ | _O(m * n)_ | Medium |`amazon`, `microsoft`, `linkedin`, `basic`| AGAIN* (3) 711| [Number of Distinct Islands II](https://leetcode.com/problems/number-of-distinct-islands-ii/) | [Python](./leetcode_python/Depth-First-Search/number-of-distinct-islands-ii.py), [Java](./leetcode_java/Depth-First-Search/number-of-distinct-islands-ii.java) | | | Hard |complex, dfs, check `# 200, 694 Number of Distinct Islands`,`amazon`| OK*** (3) 721| [Accounts Merge](https://leetcode.com/problems/accounts-merge/) | [Python ](./leetcode_python/Depth-First-Search/accounts-merge.py) | _O(nlogn)_ | _O(n)_| Medium | dfs, Disjoint Set Union (DSU), `Union Find`,`path compression`, `complex`, `fb`, google, amazon, m$, apple, twitter| AGAIN******* (4) (not start) 733| [Flood Fill](https://leetcode.com/problems/flood-fill/) | [Python](./leetcode_python/Depth-First-Search/flood-fill.py) | _O(m * n)_ | _O(m * n)_ | Easy |`fb`, `amazon`, `good basic`| OK**** (5) diff --git a/data/progress.txt b/data/progress.txt index e06d11d6..037636a1 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20230930: 133 +20230930: 133,695 20230903: 654,106,105 20230830: 200 20230827: 131,17 diff --git a/data/to_review.txt b/data/to_review.txt index 8eadcbb2..e8027366 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,19 +1,19 @@ -2023-11-24 -> ['133'] -2023-11-03 -> ['133'] +2023-11-24 -> ['133,695'] +2023-11-03 -> ['133,695'] 2023-10-28 -> ['654,106,105'] 2023-10-24 -> ['200'] -2023-10-21 -> ['133', '131,17'] +2023-10-21 -> ['133,695', '131,17'] 2023-10-16 -> ['79'] 2023-10-15 -> ['40'] 2023-10-14 -> ['90'] -2023-10-13 -> ['133', '46'] -2023-10-08 -> ['133'] +2023-10-13 -> ['133,695', '46'] +2023-10-08 -> ['133,695'] 2023-10-07 -> ['654,106,105', '78,39'] 2023-10-06 -> ['355'] -2023-10-05 -> ['133', '621'] -2023-10-03 -> ['133', '200', '973,215'] -2023-10-02 -> ['133'] -2023-10-01 -> ['133'] +2023-10-05 -> ['133,695', '621'] +2023-10-03 -> ['133,695', '200', '973,215'] +2023-10-02 -> ['133,695'] +2023-10-01 -> ['133,695'] 2023-09-30 -> ['131,17'] 2023-09-25 -> ['79'] 2023-09-24 -> ['654,106,105', '40'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DFS/MaxAreaOfIsland.java b/leetcode_java/src/main/java/LeetCodeJava/DFS/MaxAreaOfIsland.java new file mode 100644 index 00000000..d799b914 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/DFS/MaxAreaOfIsland.java @@ -0,0 +1,144 @@ +package LeetCodeJava.DFS; + +// https://leetcode.com/problems/max-area-of-island/ + +import java.util.Arrays; +import java.util.List; +import java.util.Stack; + +// +//class Point { +// int x; +// int y; +// +// Point(int x, int y) { +// this.x = x; +// this.y = y; +// } +//} + +public class MaxAreaOfIsland { + + // TODO : fix below +// int ans = 0; +// List toVisit = Arrays.asList(); +// List> collected; +// +// // V0 +// public int maxAreaOfIsland(int[][] grid) { +// +// int len = grid.length; +// int width = grid[0].length; +// +// if (len == 0 && width == 0) { +// return 0; +// } +// +// // collect "1" points +// for (int i = 0; i < len; i++) { +// for (int j = 0; j < width; j++) { +// if (grid[i][j] == 1) { +// toVisit.add(new Point(j, i)); +// } +// } +// } +// +// // dfs +// for (Point point : toVisit) { +// int tmp = this._help(grid, point.y, point.x, Arrays.asList()); +// this.ans = Math.max(this.ans, tmp); +// } +// return this.ans; +// } +// +// private int _help(int[][] grid, int x, int y, List tmp) { +// +// int len = grid.length; +// int width = grid[0].length; +// +// if (grid[y][x] != 1) { +// return 0; +// } +// +// if (x >= width || y >= len) { +// return 0; +// } +// +// // mark as visit +// grid[y][x] = -1; +// // double check?? +// tmp.add(1); +// _help(grid, x + 1, y, tmp); +// _help(grid, x - 1, y, tmp); +// _help(grid, x, y + 1, tmp); +// _help(grid, x, y - 1, tmp); +// +// return tmp.size(); +// } + + // V1 + // IDEA : DFS (recursive) + // https://leetcode.com/problems/max-area-of-island/editorial/ + int[][] grid; + boolean[][] seen; + + public int area(int r, int c) { + if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length || + seen[r][c] || grid[r][c] == 0) + return 0; + seen[r][c] = true; + return (1 + area(r+1, c) + area(r-1, c) + + area(r, c-1) + area(r, c+1)); + } + + public int maxAreaOfIsland_1(int[][] grid) { + this.grid = grid; + seen = new boolean[grid.length][grid[0].length]; + int ans = 0; + for (int r = 0; r < grid.length; r++) { + for (int c = 0; c < grid[0].length; c++) { + ans = Math.max(ans, area(r, c)); + } + } + return ans; + } + + // V2 + // IDEA : DFS (iterative) + // https://leetcode.com/problems/max-area-of-island/editorial/ + public int maxAreaOfIsland_2(int[][] grid) { + boolean[][] seen = new boolean[grid.length][grid[0].length]; + int[] dr = new int[]{1, -1, 0, 0}; + int[] dc = new int[]{0, 0, 1, -1}; + + int ans = 0; + for (int r0 = 0; r0 < grid.length; r0++) { + for (int c0 = 0; c0 < grid[0].length; c0++) { + if (grid[r0][c0] == 1 && !seen[r0][c0]) { + int shape = 0; + Stack stack = new Stack(); + stack.push(new int[]{r0, c0}); + seen[r0][c0] = true; + while (!stack.empty()) { + int[] node = stack.pop(); + int r = node[0], c = node[1]; + shape++; + for (int k = 0; k < 4; k++) { + int nr = r + dr[k]; + int nc = c + dc[k]; + if (0 <= nr && nr < grid.length && + 0 <= nc && nc < grid[0].length && + grid[nr][nc] == 1 && !seen[nr][nc]) { + stack.push(new int[]{nr, nc}); + seen[nr][nc] = true; + } + } + } + ans = Math.max(ans, shape); + } + } + } + return ans; + } + +}