Skip to content

Commit

Permalink
add 130 java, update progres
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 3, 2023
1 parent ab414c3 commit d5899dd
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@
103| [Binary Tree Zigzag Level Order Traversal](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| [Python](./leetcode_python/Breadth-First-Search/binary-tree-zigzag-level-order-traversal.py) | _O(n)_| _O(n)_| Medium |`amazon`,`fb`| OK
126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)| [Python](./leetcode_python/Breadth-First-Search/word-ladder-ii.py) ||| Hard |complex, trick, dfs, bfs, dfs+bfs, check `# 127 Word Ladder`,`amazon` | AGAIN*** (3)
127| [Word Ladder](https://leetcode.com/problems/word-ladder/)| [Python](./leetcode_python/Breadth-First-Search/word-ladder.py) | _O(n * d)_ | _O(d)_ | Hard/Medium |good basic, check #126 Word Ladder II, `bfs`, `UBER`, `amazon`, `M$`, `fb`| AGAIN************** (9)
130| [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [Python](./leetcode_python/Breadth-First-Search/surrounded-regions.py) | _O(m + n)_ | | Medium |`bfs`, `dfs`,`union find`,good basic, `amazon`| AGAIN*********** (5)
130| [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [Python](./leetcode_python/Breadth-First-Search/surrounded-regions.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java) | _O(m + n)_ | | Medium |`bfs`, `dfs`,`union find`,good basic, `amazon`| AGAIN*********** (5)
133| [Clone Graph](https://leetcode.com/problems/clone-graph/)| [Python](./leetcode_python/Breadth-First-Search/clone-graph.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java) | _O(n)_ | _O(n)_ | Medium |Curated Top 75, good trick, `check #138 Copy List with Random Pointer `,`graph`,`dfs`,`bfs`, `UBER`, `google`,`amazon`,`fb`| AGAIN**************** (9) (MUST)
207| [Course Schedule](https://leetcode.com/problems/course-schedule/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Curated Top 75, Topological Sort, backtrack, `good trick`,`dfs`, `bfs` , `amazon`,`fb`| AGAIN**************** (12) (MUST)
210| [Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)| [Python](./leetcode_python/Breadth-First-Search/course-schedule-ii.py) | _O(\|V\| + \|E\|)_ | _O(\|E\|)_ | Medium |Topological Sort,check `# 207 Course Schedule ` first, `dfs`, `bfs` ,`amazon` ,`fb` | AGAIN********* (9) (again)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20231002: 130
20231001: 417
20230930: 133,695
20230903: 654,106,105
Expand Down
14 changes: 7 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@
2020-07-30 -> ['694']
2020-07-29 -> ['663']
2020-07-28 -> ['298', '661,662']
2020-07-27 -> ['066,271', '130']
2020-07-27 -> ['130', '066,271']
2020-07-26 -> ['346,686', '701,450']
2020-07-25 -> ['640,645']
2020-07-24 -> ['163', '482,127,102,107', '606,459']
Expand All @@ -517,26 +517,26 @@
2020-07-18 -> ['734,737', '771,775', '451,414,189']
2020-07-16 -> ['163', '762', '725,538,089']
2020-07-15 -> ['298', '742', '517,535,529']
2020-07-14 -> ['066,271', '130', '711,046,126,127']
2020-07-14 -> ['130', '066,271', '711,046,126,127']
2020-07-13 -> ['346,686', '738', '063,064,120,0146']
2020-07-12 -> ['210,261', '396', '675,297,138']
2020-07-11 -> ['066,271', '163', '361,393,133,207', '482,127,102,107']
2020-07-10 -> ['734,737', '388', '836,860,863']
2020-07-09 -> ['066,271', '694']
2020-07-08 -> ['066,271', '163', '646', '663']
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
2020-07-06 -> ['163', '361,393,133,207', '130', '669,682,739,763']
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
2020-07-01 -> ['269', '210,261', '361,393,133,207', '130', '322,380,394']
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
2020-06-28 -> ['734,737', '130', '482,127,102,107', '355,119,536', '231,232,240']
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
2020-06-26 -> ['298', '130', '694', '155,165,167,179']
2020-06-25 -> ['298', '130', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
Expand Down
210 changes: 210 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package LeetCodeJava.BFS;

// https://leetcode.com/problems/surrounded-regions/

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class SurroundedRegions {

// TODO : fix below
// V0
// int[][] points = new int[][]{};
// public void solve(char[][] board) {
//
// if (board.length == 1 && board[0].length == 1){
// return;
// }
//
// int len = board.length;
// int width = board[0].length;
//
// // dfs
// // make points to "X"
// List<List<Integer>> tmp_points = _collect(board, 0, 0, new ArrayList<>());
// if (tmp_points != null && _is_border(tmp_points)){
// flip(tmp_points, board);
// }
// return;
// }
//
// private List<List<Integer>> _collect(char[][] board, int x, int y, List<List<Integer>> points){
//
// int len = board.length;
// int width = board[0].length;
//
// if (x < 0 || x >= width || y < 0 || y >= len || board[y][x] == 'X'){
// return null;
// }
//
// if (board[y][x] == 'O'){
// List<Integer> cur = new ArrayList<>();
// cur.add(x);
// cur.add(y);
// points.add(cur);
// }
//
// _collect(board, x+1, y, points);
// _collect(board, x-1, y, points);
// _collect(board, x, y+1, points);
// _collect(board, x, y-1, points);
//
// return points;
// }
//
// private Boolean _is_border(List<List<Integer>> points){
// for (List<Integer> x : points){
// if (x.get(0) == 0 || x.get(1) == 0){
// return false;
// }
// }
// return true;
// }
//
// private void flip(List<List<Integer>> points, char[][] board){
// for (List<Integer> x : points){
// board[x.get(0)][x.get(1)] = 'X';
// }
// }

// V1
// IDEA : DFS
// https://leetcode.com/problems/surrounded-regions/editorial/
protected Integer ROWS = 0;
protected Integer COLS = 0;

public void solve_1(char[][] board) {
if (board == null || board.length == 0) {
return;
}
this.ROWS = board.length;
this.COLS = board[0].length;

List<Pair<Integer, Integer>> borders = new LinkedList<Pair<Integer, Integer>>();
// Step 1). construct the list of border cells
for (int r = 0; r < this.ROWS; ++r) {
borders.add(new Pair(r, 0));
borders.add(new Pair(r, this.COLS - 1));
}
for (int c = 0; c < this.COLS; ++c) {
borders.add(new Pair(0, c));
borders.add(new Pair(this.ROWS - 1, c));
}

// Step 2). mark the escaped cells
for (Pair<Integer, Integer> pair : borders) {
this.DFS(board, pair.first, pair.second);
}

// Step 3). flip the cells to their correct final states
for (int r = 0; r < this.ROWS; ++r) {
for (int c = 0; c < this.COLS; ++c) {
if (board[r][c] == 'O')
board[r][c] = 'X';
if (board[r][c] == 'E')
board[r][c] = 'O';
}
}
}

protected void DFS(char[][] board, int row, int col) {
if (board[row][col] != 'O')
return;

board[row][col] = 'E';
if (col < this.COLS - 1)
this.DFS(board, row, col + 1);
if (row < this.ROWS - 1)
this.DFS(board, row + 1, col);
if (col > 0)
this.DFS(board, row, col - 1);
if (row > 0)
this.DFS(board, row - 1, col);
}
}

class Pair<U, V> {
public U first;
public V second;

public Pair(U first, V second) {
this.first = first;
this.second = second;
}


// V2
// IDEA : BFS
// https://leetcode.com/problems/surrounded-regions/editorial/
protected Integer ROWS = 0;
protected Integer COLS = 0;

public void solve_2(char[][] board) {
if (board == null || board.length == 0) {
return;
}
this.ROWS = board.length;
this.COLS = board[0].length;

List<Pair<Integer, Integer>> borders = new LinkedList<Pair<Integer, Integer>>();
// Step 1). construct the list of border cells
for (int r = 0; r < this.ROWS; ++r) {
borders.add(new Pair(r, 0));
borders.add(new Pair(r, this.COLS - 1));
}
for (int c = 0; c < this.COLS; ++c) {
borders.add(new Pair(0, c));
borders.add(new Pair(this.ROWS - 1, c));
}

// Step 2). mark the escaped cells
for (Pair<Integer, Integer> pair : borders) {
this.BFS(board, pair.first, pair.second);
}

// Step 3). flip the cells to their correct final states
for (int r = 0; r < this.ROWS; ++r) {
for (int c = 0; c < this.COLS; ++c) {
if (board[r][c] == 'O')
board[r][c] = 'X';
if (board[r][c] == 'E')
board[r][c] = 'O';
}
}
}

protected void BFS(char[][] board, int r, int c) {
LinkedList<Pair2<Integer, Integer>> queue = new LinkedList<Pair2<Integer, Integer>>();
queue.offer(new Pair2<>(r, c));

while (!queue.isEmpty()) {
Pair2<Integer, Integer> pair = queue.pollFirst();
int row = pair.first, col = pair.second;
if (board[row][col] != 'O')
continue;

board[row][col] = 'E';
if (col < this.COLS - 1)
queue.offer(new Pair2<>(row, col + 1));
if (row < this.ROWS - 1)
queue.offer(new Pair2<>(row + 1, col));
if (col > 0)
queue.offer(new Pair2<>(row, col - 1));
if (row > 0)
queue.offer(new Pair2<>(row - 1, col));
}
}
}


class Pair2<U, V> {
public U first;
public V second;

public Pair2(U first, V second) {
this.first = first;
this.second = second;
}

}

0 comments on commit d5899dd

Please sign in to comment.