Skip to content

Commit

Permalink
add 695 java, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 30, 2023
1 parent 1fb7804 commit 0dd1649
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20230930: 133
20230930: 133,695
20230903: 654,106,105
20230830: 200
20230827: 131,17
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
144 changes: 144 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/DFS/MaxAreaOfIsland.java
Original file line number Diff line number Diff line change
@@ -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<Point> toVisit = Arrays.asList();
// List<List<Integer>> 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<Integer> 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<int[]> 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;
}

}

0 comments on commit 0dd1649

Please sign in to comment.