From d5899dda19e72bb656028baedb74c22636ac17cf Mon Sep 17 00:00:00 2001 From: yennanliu Date: Tue, 3 Oct 2023 19:37:24 +0800 Subject: [PATCH] add 130 java, update progres --- README.md | 2 +- data/progress.txt | 1 + data/to_review.txt | 14 +- .../LeetCodeJava/BFS/SurroundedRegions.java | 210 ++++++++++++++++++ 4 files changed, 219 insertions(+), 8 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java diff --git a/README.md b/README.md index a6671592..7c7f0583 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 03567835..bfa45a38 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20231002: 130 20231001: 417 20230930: 133,695 20230903: 654,106,105 diff --git a/data/to_review.txt b/data/to_review.txt index 3e2293b1..61d0e4e5 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] @@ -517,7 +517,7 @@ 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'] @@ -525,18 +525,18 @@ 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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java b/leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java new file mode 100644 index 00000000..0f036f78 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/BFS/SurroundedRegions.java @@ -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> tmp_points = _collect(board, 0, 0, new ArrayList<>()); +// if (tmp_points != null && _is_border(tmp_points)){ +// flip(tmp_points, board); +// } +// return; +// } +// +// private List> _collect(char[][] board, int x, int y, List> 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 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> points){ +// for (List x : points){ +// if (x.get(0) == 0 || x.get(1) == 0){ +// return false; +// } +// } +// return true; +// } +// +// private void flip(List> points, char[][] board){ +// for (List 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> borders = new LinkedList>(); + // 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 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 { + 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> borders = new LinkedList>(); + // 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 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> queue = new LinkedList>(); + queue.offer(new Pair2<>(r, c)); + + while (!queue.isEmpty()) { + Pair2 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 { + public U first; + public V second; + + public Pair2(U first, V second) { + this.first = first; + this.second = second; + } + +}