Skip to content

Commit

Permalink
add 934 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 29, 2024
1 parent a1870b3 commit 9cdedcb
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@
787|[Cheapest Flights Within K Stops](https://leetcode.com/problems/cheapest-flights-within-k-stops/)| [Python](./leetcode_python/Breadth-First-Search/cheapest-flights-within-k-stops.py) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Medium | `Dijkstra's algorithm`, dfs, bfs, graph, priority queue,`amazon`, apple, google, airbnb | AGAIN****** (3) (not start)
815|[Bus Routes](https://leetcode.com/problems/bus-routes/)| [Python](./leetcode_python/Breadth-First-Search/bus-routes.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/BusRoutes.java) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Hard | shortest route, graph, bfs,`amazon`, google| AGAIN**** (3)
886|[Possible Bipartition](https://leetcode.com/problems/possible-bipartition/)| [Python](./leetcode_python/Breadth-First-Search/possible-bipartition.py) | _O(\|V\| + \|E\|)_ | _O(\|V\| + \|E\|)_| Medium |check `# 785 Is Graph Bipartite?`,`graph`, `AGAIN`,`union find` ,`fb` | AGAIN********** (6)
934|[Shortest Bridge](https://leetcode.com/problems/shortest-bridge/)| [Python](./leetcode_python/Breadth-First-Search/shortest-bridge.py) | _O(n^2)_ | _O(n^2)_ | Medium | BFS, DFS, `complex`| AGAIN (not start)
934|[Shortest Bridge](https://leetcode.com/problems/shortest-bridge/)| [Python](./leetcode_python/Breadth-First-Search/shortest-bridge.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/ShortestBridge.java) | _O(n^2)_ | _O(n^2)_ | Medium | BFS, DFS, `complex`, dfs,bfs,google| AGAIN (1)
967|[Numbers With Same Consecutive Differences](https://leetcode.com/problems/numbers-with-same-consecutive-differences/)| [Python](./leetcode_python/Breadth-First-Search/numbers-with-same-consecutive-differences.py) | _O(2^n)_ | _O(2^n)_ | Medium |`good trick` | AGAIN** (3)
675|[Cut Off Trees for Golf Event](https://leetcode.com/problems/cut-off-trees-for-golf-event/)| [Python](./leetcode_python/Breadth-First-Search/cut-off-trees-for-golf-event.py), [Java](./leetcode_java/Breadth-First-Search/cut-off-trees-for-golf-event.java) ||| Hard |tree, BFS, complex, `amazon` | AGAIN (not start)
864|[Shortest Path to Get All Keys](https://leetcode.com/problems/shortest-path-to-get-all-keys/)| [Python](./leetcode_python/Breadth-First-Search/shortest-path-to-get-all-keys.py) ||| Hard |BFS, `amazon` | AGAIN (not start)
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 @@
20241229: 498
20241229: 498(again),934
20241228: 379,173,855(todo)
20241227: 079,212(todo),362,849
20241222: 369,311
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
2025-02-22 -> ['498']
2025-02-22 -> ['498(again),934']
2025-02-21 -> ['379,173,855(todo)']
2025-02-20 -> ['079,212(todo),362,849']
2025-02-15 -> ['369,311']
2025-02-14 -> ['370']
2025-02-13 -> ['815,871,593,1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['498', '304,853,325']
2025-02-01 -> ['498(again),934', '304,853,325']
2025-01-31 -> ['379,173,855(todo)']
2025-01-30 -> ['079,212(todo),362,849']
2025-01-26 -> ['370(todo)']
2025-01-25 -> ['369,311']
2025-01-24 -> ['370', '34,767']
2025-01-23 -> ['815,871,593,1109']
2025-01-20 -> ['722,380']
2025-01-19 -> ['498', '33,81']
2025-01-19 -> ['498(again),934', '33,81']
2025-01-18 -> ['379,173,855(todo)']
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-12 -> ['369,311']
2025-01-11 -> ['498', '370', '304,853,325', '394']
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
2025-01-10 -> ['379,173,855(todo)', '815,871,593,1109', '833,950']
2025-01-09 -> ['079,212(todo),362,849']
2025-01-06 -> ['498']
2025-01-06 -> ['498(again),934']
2025-01-05 -> ['379,173,855(todo)', '370(todo)']
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
2025-01-03 -> ['498', '370', '34,767', '444']
2025-01-03 -> ['498(again),934', '370', '34,767', '444']
2025-01-02 -> ['379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
2025-01-01 -> ['498', '079,212(todo),362,849']
2024-12-31 -> ['498', '379,173,855(todo)']
2024-12-30 -> ['498', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
2025-01-01 -> ['498(again),934', '079,212(todo),362,849']
2024-12-31 -> ['498(again),934', '379,173,855(todo)']
2024-12-30 -> ['498(again),934', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
2024-12-29 -> ['379,173,855(todo)', '079,212(todo),362,849', '370', '304,853,325', '33,81']
2024-12-28 -> ['079,212(todo),362,849', '815,871,593,1109', '900']
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
Expand Down
190 changes: 190 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/BFS/ShortestBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package LeetCodeJava.BFS;

// https://leetcode.com/problems/shortest-bridge/description/

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

/**
* 934. Shortest Bridge
* Medium
* Topics
* Companies
* You are given an n x n binary matrix grid where 1 represents land and 0 represents water.
*
* An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.
*
* You may change 0's to 1's to connect the two islands to form one island.
*
* Return the smallest number of 0's you must flip to connect the two islands.
*
*
*
* Example 1:
*
* Input: grid = [[0,1],[1,0]]
* Output: 1
* Example 2:
*
* Input: grid = [[0,1,0],[0,0,0],[0,0,1]]
* Output: 2
* Example 3:
*
* Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
* Output: 1
*
*
* Constraints:
*
* n == grid.length == grid[i].length
* 2 <= n <= 100
* grid[i][j] is either 0 or 1.
* There are exactly two islands in grid.
*/
public class ShortestBridge {
// V0
// public int shortestBridge(int[][] grid) {
//
// }

// V1-1
// https://leetcode.com/problems/shortest-bridge/editorial/
// IDEA: Depth-First-Search + Breadth-First-Search
private List<int[]> bfsQueue;

// Recursively check the neighboring land cell of current cell grid[x][y] and
// add all
// land cells of island A to bfsQueue.
private void dfs(int[][] grid, int x, int y, int n) {
grid[x][y] = 2;
bfsQueue.add(new int[] { x, y });
for (int[] pair : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
int curX = pair[0], curY = pair[1];
if (0 <= curX && curX < n && 0 <= curY && curY < n && grid[curX][curY] == 1) {
dfs(grid, curX, curY, n);
}
}
}

// Find any land cell, and we treat it as a cell of island A.
public int shortestBridge_1_1(int[][] grid) {
int n = grid.length;
int firstX = -1, firstY = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
firstX = i;
firstY = j;
break;
}
}
}

// Add all land cells of island A to bfsQueue.
bfsQueue = new ArrayList<>();
dfs(grid, firstX, firstY, n);

int distance = 0;
while (!bfsQueue.isEmpty()) {
List<int[]> newBfs = new ArrayList<>();
for (int[] pair : bfsQueue) {
int x = pair[0], y = pair[1];
for (int[] nextPair : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
int curX = nextPair[0], curY = nextPair[1];
if (0 <= curX && curX < n && 0 <= curY && curY < n) {
if (grid[curX][curY] == 1) {
return distance;
} else if (grid[curX][curY] == 0) {
newBfs.add(nextPair);
grid[curX][curY] = -1;
}
}
}
}

// Once we finish one round without finding land cells of island B, we will
// start the next round on all water cells that are 1 cell further away from
// island A and increment the distance by 1.
bfsQueue = newBfs;
distance++;
}

return distance;
}

// V1-2
// https://leetcode.com/problems/shortest-bridge/editorial/
// IDEA: BFS
public int shortestBridge_1_2(int[][] grid) {
int n = grid.length;
int firstX = -1, firstY = -1;

// Find any land cell, and we treat it as a cell of island A.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
firstX = i;
firstY = j;
break;
}
}
}

// bfsQueue for BFS on land cells of island A; secondBfsQueue for BFS on water
// cells.
List<int[]> bfsQueue = new ArrayList<>();
List<int[]> secondBfsQueue = new ArrayList<>();
bfsQueue.add(new int[] { firstX, firstY });
secondBfsQueue.add(new int[] { firstX, firstY });
grid[firstX][firstY] = 2;

// BFS for all land cells of island A and add them to secondBfsQueue.
while (!bfsQueue.isEmpty()) {
List<int[]> newBfs = new ArrayList<>();
for (int[] cell : bfsQueue) {
int x = cell[0];
int y = cell[1];
for (int[] next : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
int curX = next[0];
int curY = next[1];
if (curX >= 0 && curX < n && curY >= 0 && curY < n && grid[curX][curY] == 1) {
newBfs.add(new int[] { curX, curY });
secondBfsQueue.add(new int[] { curX, curY });
grid[curX][curY] = 2;
}
}
}
bfsQueue = newBfs;
}

int distance = 0;
while (!secondBfsQueue.isEmpty()) {
List<int[]> newBfs = new ArrayList<>();
for (int[] cell : secondBfsQueue) {
int x = cell[0];
int y = cell[1];
for (int[] next : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
int curX = next[0];
int curY = next[1];
if (curX >= 0 && curX < n && curY >= 0 && curY < n) {
if (grid[curX][curY] == 1) {
return distance;
} else if (grid[curX][curY] == 0) {
newBfs.add(new int[] { curX, curY });
grid[curX][curY] = -1;
}
}
}
}

// Once we finish one round without finding land cells of island B, we will
// start the next round on all water cells that are 1 cell further away from
// island A and increment the distance by 1.
secondBfsQueue = newBfs;
distance++;
}
return distance;
}

// V2
}
111 changes: 111 additions & 0 deletions leetcode_java/src/main/java/dev/workspace6.java
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,115 @@ public int[] findDiagonalOrder(int[][] mat) {
return res;
}

// LC 934
// https://leetcode.com/problems/shortest-bridge/
// 5.04 - 5.20 PM
/**
*
* An island is a 4-directionally connected group of 1's
* not connected to any other 1's.
* `There are exactly two islands in grid.`
*
*
* Return the smallest number
* of 0's you must flip to connect the two islands.
*
*
* 1 represents land and 0 represents water.
*
*
* Exp 1
*
* Input: grid = [[0,1],
* [1,0]]
* Output: 1
*
*
* EXP 2
*
* Input: grid = [[0,1,0],
* [0,0,0],
* [0,0,1]]
*
*
* Output: 2
*
*
* EXP 3
*
*
* Input: grid = [[1,1,1,1,1],
* [1,0,0,0,1],
* [1,0,1,0,1],
* [1,0,0,0,1],
* [1,1,1,1,1]]
*
* Output: 1
*/
/**
* IDEA: BFS (??
*
*
* step 1) get the coordination of 2 islands ("1" collections split by "0")
* step 2) start from "smaller" island, find the min dist (get the "flip cnt" as well) between it and the other island (??
*
*/
public int shortestBridge(int[][] grid) {

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

// get small, big island coordination
List<List<Integer>> smallIsland = new ArrayList<>();
List<List<Integer>> bigIsland = new ArrayList<>();

Set<List<Integer>> visited = new HashSet<>();

int l = grid.length;
int w = grid[0].length;

for(int i = 0; i < l; i++){
for(int j = 0; j < w; j++){
getIsland(grid, j, i, smallIsland, visited);
}
}

// bfs

return 0;
}

private List<List<Integer>> getIsland(int[][] grid, int x, int y, List<List<Integer>> cur, Set<List<Integer>> visited){
if(grid.length == 0 || grid[0].length == 0){
return null;
}
int l = grid.length;
int w = grid[0].length;

int[][] moves = new int[][]{ {0,1}, {0,-1}, {1,0}, {-1,0} };

if(grid[y][w] == 1){
List<Integer> tmp = new ArrayList<>();
tmp.add(x);
tmp.add(y);
cur.add(tmp);
}

for(int[] move: moves){
int x_ = x + move[0];
int y_ = y + move[1];
List<Integer> tmp2 = new ArrayList<>();
tmp2.add(x_);
tmp2.add(y_);
if(x_ >= 0 && x_ <= w && y_ >= 0 && y_ <= l && !visited.contains(tmp2)){
visited.add(tmp2);
getIsland(grid, x_, y_, cur, visited);
}
}

return cur; // ?
}

}

0 comments on commit 9cdedcb

Please sign in to comment.