Skip to content

Commit

Permalink
update lc 695 java, cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 2, 2023
1 parent b6d90b5 commit 5dcb97f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 73 deletions.
22 changes: 22 additions & 0 deletions doc/cheatsheet/java_trick.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,28 @@ for(int i = k - 1; i >= 0; --i) {

```

### 1-1-2) Init M X N boolean matrix
```java
// java
// LC 695
public static void main(String[] args) {

// ex1
Boolean[][] x = new Boolean[3][4];
System.out.println(x);
System.out.println(x[0][0]); // null

// ex2
boolean[][] y = new boolean[3][4];
System.out.println(y);
System.out.println(y[0][0]); // false

// ex3
boolean[][] seen;
seen = new boolean[3][4];
}
```

### 1-2) Char array to String

```java
Expand Down
129 changes: 56 additions & 73 deletions leetcode_java/src/main/java/LeetCodeJava/DFS/MaxAreaOfIsland.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,81 @@

// 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();
// }
// V0
// IDEA : DFS
int maxArea = 0;
// NOTE !!! we NEED to use boolean instead of BOOLEAN,
// since boolean' default value is "false", BOOLEAN 's default value is "null"
boolean[][] seen;

public int maxAreaOfIsland(int[][] grid) {

int len = grid.length;
int width = grid[0].length;

// edge case
if (grid.length == 1 && grid[0].length == 1){
if (grid[0][0] == 0){
return 0;
}
return 1;
}

// NOTE !!! we use below to init a M X N matrix
this.seen = new boolean[grid.length][grid[0].length];

for (int i = 0; i < len; i++){
for (int j = 0; j < width; j++){
//System.out.println("i = " + i + " j = " + j);
if (grid[i][j] == 1){
int _area = _getArea(grid, this.seen, j, i);
//System.out.println("_area = " + _area);
this.maxArea = Math.max(this.maxArea, _area);
}
}
}
return this.maxArea;
}

private int _getArea(int[][] grid, boolean[][] seen, int x, int y){

int len = grid.length;
int width = grid[0].length;

if (x < 0 || x >= width || y < 0 || y >= len || seen[y][x] == true || grid[y][x] == 0){
return 0;
}

seen[y][x] = true;

return 1 + _getArea(grid, seen, x+1, y) +
_getArea(grid, seen, x-1, y) +
_getArea(grid, seen, x, y+1) +
_getArea(grid, seen, x, y-1);
}

// V1
// IDEA : DFS (recursive)
// https://leetcode.com/problems/max-area-of-island/editorial/
int[][] grid;
boolean[][] seen;
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)
_seen[r][c] || grid[r][c] == 0)
return 0;
seen[r][c] = true;
_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];
_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++) {
Expand Down
52 changes: 52 additions & 0 deletions leetcode_java/src/main/java/dev/workSpace1.java
Original file line number Diff line number Diff line change
Expand Up @@ -870,5 +870,57 @@ public static void main(String[] args) {
System.out.println(my_str.charAt(2));
}

// https://leetcode.com/problems/max-area-of-island/
int maxArea = 0;
boolean[][] seen;
public int maxAreaOfIsland(int[][] grid) {

int len = grid.length;
int width = grid[0].length;

// edge case
if (grid.length == 1 && grid[0].length == 1){
if (grid[0][0] == 0){
return 0;
}
return 1;
}

// dfs
// private static final int[][] DIRECTIONS = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}}
//Boolean[][] seen = new Boolean[len][width];
//boolean[][] seen;
this.seen = new boolean[grid.length][grid[0].length];

for (int i = 0; i < len; i++){
for (int j = 0; j < width; j++){
System.out.println("i = " + i + " j = " + j);
if (grid[i][j] == 1){
int _area = _getArea(grid, this.seen, j, i);
System.out.println("_area = " + _area);
this.maxArea = Math.max(this.maxArea, _area);
}
}
}
return this.maxArea;
}

private int _getArea(int[][] grid, boolean[][] seen, int x, int y){

int len = grid.length;
int width = grid[0].length;

if (x < 0 || x >= width || y < 0 || y >= len || seen[y][x] == true || grid[y][x] == 0){
return 0;
}

// ??
seen[y][x] = true;

return 1 + _getArea(grid, seen, x+1, y) +
_getArea(grid, seen, x-1, y) +
_getArea(grid, seen, x, y+1) +
_getArea(grid, seen, x, y-1);
}

}

0 comments on commit 5dcb97f

Please sign in to comment.