Skip to content

Commit 9cdedcb

Browse files
committed
add 934 java
1 parent a1870b3 commit 9cdedcb

File tree

5 files changed

+312
-11
lines changed

5 files changed

+312
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@
934934
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)
935935
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)
936936
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)
937-
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)
937+
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)
938938
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)
939939
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)
940940
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)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241229: 498
1+
20241229: 498(again),934
22
20241228: 379,173,855(todo)
33
20241227: 079,212(todo),362,849
44
20241222: 369,311

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
2025-02-22 -> ['498']
1+
2025-02-22 -> ['498(again),934']
22
2025-02-21 -> ['379,173,855(todo)']
33
2025-02-20 -> ['079,212(todo),362,849']
44
2025-02-15 -> ['369,311']
55
2025-02-14 -> ['370']
66
2025-02-13 -> ['815,871,593,1109']
77
2025-02-07 -> ['560,523']
8-
2025-02-01 -> ['498', '304,853,325']
8+
2025-02-01 -> ['498(again),934', '304,853,325']
99
2025-01-31 -> ['379,173,855(todo)']
1010
2025-01-30 -> ['079,212(todo),362,849']
1111
2025-01-26 -> ['370(todo)']
1212
2025-01-25 -> ['369,311']
1313
2025-01-24 -> ['370', '34,767']
1414
2025-01-23 -> ['815,871,593,1109']
1515
2025-01-20 -> ['722,380']
16-
2025-01-19 -> ['498', '33,81']
16+
2025-01-19 -> ['498(again),934', '33,81']
1717
2025-01-18 -> ['379,173,855(todo)']
1818
2025-01-17 -> ['079,212(todo),362,849', '560,523', '253']
1919
2025-01-16 -> ['776,31']
2020
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2121
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2222
2025-01-12 -> ['369,311']
23-
2025-01-11 -> ['498', '370', '304,853,325', '394']
23+
2025-01-11 -> ['498(again),934', '370', '304,853,325', '394']
2424
2025-01-10 -> ['379,173,855(todo)', '815,871,593,1109', '833,950']
2525
2025-01-09 -> ['079,212(todo),362,849']
26-
2025-01-06 -> ['498']
26+
2025-01-06 -> ['498(again),934']
2727
2025-01-05 -> ['379,173,855(todo)', '370(todo)']
2828
2025-01-04 -> ['079,212(todo),362,849', '369,311', '560,523', '53,210,207']
29-
2025-01-03 -> ['498', '370', '34,767', '444']
29+
2025-01-03 -> ['498(again),934', '370', '34,767', '444']
3030
2025-01-02 -> ['379,173,855(todo)', '815,871,593,1109', '1188,130,855(again)']
31-
2025-01-01 -> ['498', '079,212(todo),362,849']
32-
2024-12-31 -> ['498', '379,173,855(todo)']
33-
2024-12-30 -> ['498', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
31+
2025-01-01 -> ['498(again),934', '079,212(todo),362,849']
32+
2024-12-31 -> ['498(again),934', '379,173,855(todo)']
33+
2024-12-30 -> ['498(again),934', '379,173,855(todo)', '079,212(todo),362,849', '369,311', '722,380']
3434
2024-12-29 -> ['379,173,855(todo)', '079,212(todo),362,849', '370', '304,853,325', '33,81']
3535
2024-12-28 -> ['079,212(todo),362,849', '815,871,593,1109', '900']
3636
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package LeetCodeJava.BFS;
2+
3+
// https://leetcode.com/problems/shortest-bridge/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* 934. Shortest Bridge
10+
* Medium
11+
* Topics
12+
* Companies
13+
* You are given an n x n binary matrix grid where 1 represents land and 0 represents water.
14+
*
15+
* 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.
16+
*
17+
* You may change 0's to 1's to connect the two islands to form one island.
18+
*
19+
* Return the smallest number of 0's you must flip to connect the two islands.
20+
*
21+
*
22+
*
23+
* Example 1:
24+
*
25+
* Input: grid = [[0,1],[1,0]]
26+
* Output: 1
27+
* Example 2:
28+
*
29+
* Input: grid = [[0,1,0],[0,0,0],[0,0,1]]
30+
* Output: 2
31+
* Example 3:
32+
*
33+
* 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]]
34+
* Output: 1
35+
*
36+
*
37+
* Constraints:
38+
*
39+
* n == grid.length == grid[i].length
40+
* 2 <= n <= 100
41+
* grid[i][j] is either 0 or 1.
42+
* There are exactly two islands in grid.
43+
*/
44+
public class ShortestBridge {
45+
// V0
46+
// public int shortestBridge(int[][] grid) {
47+
//
48+
// }
49+
50+
// V1-1
51+
// https://leetcode.com/problems/shortest-bridge/editorial/
52+
// IDEA: Depth-First-Search + Breadth-First-Search
53+
private List<int[]> bfsQueue;
54+
55+
// Recursively check the neighboring land cell of current cell grid[x][y] and
56+
// add all
57+
// land cells of island A to bfsQueue.
58+
private void dfs(int[][] grid, int x, int y, int n) {
59+
grid[x][y] = 2;
60+
bfsQueue.add(new int[] { x, y });
61+
for (int[] pair : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
62+
int curX = pair[0], curY = pair[1];
63+
if (0 <= curX && curX < n && 0 <= curY && curY < n && grid[curX][curY] == 1) {
64+
dfs(grid, curX, curY, n);
65+
}
66+
}
67+
}
68+
69+
// Find any land cell, and we treat it as a cell of island A.
70+
public int shortestBridge_1_1(int[][] grid) {
71+
int n = grid.length;
72+
int firstX = -1, firstY = -1;
73+
for (int i = 0; i < n; i++) {
74+
for (int j = 0; j < n; j++) {
75+
if (grid[i][j] == 1) {
76+
firstX = i;
77+
firstY = j;
78+
break;
79+
}
80+
}
81+
}
82+
83+
// Add all land cells of island A to bfsQueue.
84+
bfsQueue = new ArrayList<>();
85+
dfs(grid, firstX, firstY, n);
86+
87+
int distance = 0;
88+
while (!bfsQueue.isEmpty()) {
89+
List<int[]> newBfs = new ArrayList<>();
90+
for (int[] pair : bfsQueue) {
91+
int x = pair[0], y = pair[1];
92+
for (int[] nextPair : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
93+
int curX = nextPair[0], curY = nextPair[1];
94+
if (0 <= curX && curX < n && 0 <= curY && curY < n) {
95+
if (grid[curX][curY] == 1) {
96+
return distance;
97+
} else if (grid[curX][curY] == 0) {
98+
newBfs.add(nextPair);
99+
grid[curX][curY] = -1;
100+
}
101+
}
102+
}
103+
}
104+
105+
// Once we finish one round without finding land cells of island B, we will
106+
// start the next round on all water cells that are 1 cell further away from
107+
// island A and increment the distance by 1.
108+
bfsQueue = newBfs;
109+
distance++;
110+
}
111+
112+
return distance;
113+
}
114+
115+
// V1-2
116+
// https://leetcode.com/problems/shortest-bridge/editorial/
117+
// IDEA: BFS
118+
public int shortestBridge_1_2(int[][] grid) {
119+
int n = grid.length;
120+
int firstX = -1, firstY = -1;
121+
122+
// Find any land cell, and we treat it as a cell of island A.
123+
for (int i = 0; i < n; i++) {
124+
for (int j = 0; j < n; j++) {
125+
if (grid[i][j] == 1) {
126+
firstX = i;
127+
firstY = j;
128+
break;
129+
}
130+
}
131+
}
132+
133+
// bfsQueue for BFS on land cells of island A; secondBfsQueue for BFS on water
134+
// cells.
135+
List<int[]> bfsQueue = new ArrayList<>();
136+
List<int[]> secondBfsQueue = new ArrayList<>();
137+
bfsQueue.add(new int[] { firstX, firstY });
138+
secondBfsQueue.add(new int[] { firstX, firstY });
139+
grid[firstX][firstY] = 2;
140+
141+
// BFS for all land cells of island A and add them to secondBfsQueue.
142+
while (!bfsQueue.isEmpty()) {
143+
List<int[]> newBfs = new ArrayList<>();
144+
for (int[] cell : bfsQueue) {
145+
int x = cell[0];
146+
int y = cell[1];
147+
for (int[] next : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
148+
int curX = next[0];
149+
int curY = next[1];
150+
if (curX >= 0 && curX < n && curY >= 0 && curY < n && grid[curX][curY] == 1) {
151+
newBfs.add(new int[] { curX, curY });
152+
secondBfsQueue.add(new int[] { curX, curY });
153+
grid[curX][curY] = 2;
154+
}
155+
}
156+
}
157+
bfsQueue = newBfs;
158+
}
159+
160+
int distance = 0;
161+
while (!secondBfsQueue.isEmpty()) {
162+
List<int[]> newBfs = new ArrayList<>();
163+
for (int[] cell : secondBfsQueue) {
164+
int x = cell[0];
165+
int y = cell[1];
166+
for (int[] next : new int[][] { { x + 1, y }, { x - 1, y }, { x, y + 1 }, { x, y - 1 } }) {
167+
int curX = next[0];
168+
int curY = next[1];
169+
if (curX >= 0 && curX < n && curY >= 0 && curY < n) {
170+
if (grid[curX][curY] == 1) {
171+
return distance;
172+
} else if (grid[curX][curY] == 0) {
173+
newBfs.add(new int[] { curX, curY });
174+
grid[curX][curY] = -1;
175+
}
176+
}
177+
}
178+
}
179+
180+
// Once we finish one round without finding land cells of island B, we will
181+
// start the next round on all water cells that are 1 cell further away from
182+
// island A and increment the distance by 1.
183+
secondBfsQueue = newBfs;
184+
distance++;
185+
}
186+
return distance;
187+
}
188+
189+
// V2
190+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,4 +1020,115 @@ public int[] findDiagonalOrder(int[][] mat) {
10201020
return res;
10211021
}
10221022

1023+
// LC 934
1024+
// https://leetcode.com/problems/shortest-bridge/
1025+
// 5.04 - 5.20 PM
1026+
/**
1027+
*
1028+
* An island is a 4-directionally connected group of 1's
1029+
* not connected to any other 1's.
1030+
* `There are exactly two islands in grid.`
1031+
*
1032+
*
1033+
* Return the smallest number
1034+
* of 0's you must flip to connect the two islands.
1035+
*
1036+
*
1037+
* 1 represents land and 0 represents water.
1038+
*
1039+
*
1040+
* Exp 1
1041+
*
1042+
* Input: grid = [[0,1],
1043+
* [1,0]]
1044+
* Output: 1
1045+
*
1046+
*
1047+
* EXP 2
1048+
*
1049+
* Input: grid = [[0,1,0],
1050+
* [0,0,0],
1051+
* [0,0,1]]
1052+
*
1053+
*
1054+
* Output: 2
1055+
*
1056+
*
1057+
* EXP 3
1058+
*
1059+
*
1060+
* Input: grid = [[1,1,1,1,1],
1061+
* [1,0,0,0,1],
1062+
* [1,0,1,0,1],
1063+
* [1,0,0,0,1],
1064+
* [1,1,1,1,1]]
1065+
*
1066+
* Output: 1
1067+
*/
1068+
/**
1069+
* IDEA: BFS (??
1070+
*
1071+
*
1072+
* step 1) get the coordination of 2 islands ("1" collections split by "0")
1073+
* step 2) start from "smaller" island, find the min dist (get the "flip cnt" as well) between it and the other island (??
1074+
*
1075+
*/
1076+
public int shortestBridge(int[][] grid) {
1077+
1078+
// edge
1079+
if(grid.length == 2 && grid[0].length == 2){
1080+
return 1;
1081+
}
1082+
1083+
// get small, big island coordination
1084+
List<List<Integer>> smallIsland = new ArrayList<>();
1085+
List<List<Integer>> bigIsland = new ArrayList<>();
1086+
1087+
Set<List<Integer>> visited = new HashSet<>();
1088+
1089+
int l = grid.length;
1090+
int w = grid[0].length;
1091+
1092+
for(int i = 0; i < l; i++){
1093+
for(int j = 0; j < w; j++){
1094+
getIsland(grid, j, i, smallIsland, visited);
1095+
}
1096+
}
1097+
1098+
// bfs
1099+
1100+
return 0;
1101+
}
1102+
1103+
private List<List<Integer>> getIsland(int[][] grid, int x, int y, List<List<Integer>> cur, Set<List<Integer>> visited){
1104+
if(grid.length == 0 || grid[0].length == 0){
1105+
return null;
1106+
}
1107+
int l = grid.length;
1108+
int w = grid[0].length;
1109+
1110+
int[][] moves = new int[][]{ {0,1}, {0,-1}, {1,0}, {-1,0} };
1111+
1112+
if(grid[y][w] == 1){
1113+
List<Integer> tmp = new ArrayList<>();
1114+
tmp.add(x);
1115+
tmp.add(y);
1116+
cur.add(tmp);
1117+
}
1118+
1119+
for(int[] move: moves){
1120+
int x_ = x + move[0];
1121+
int y_ = y + move[1];
1122+
List<Integer> tmp2 = new ArrayList<>();
1123+
tmp2.add(x_);
1124+
tmp2.add(y_);
1125+
if(x_ >= 0 && x_ <= w && y_ >= 0 && y_ <= l && !visited.contains(tmp2)){
1126+
visited.add(tmp2);
1127+
getIsland(grid, x_, y_, cur, visited);
1128+
}
1129+
}
1130+
1131+
return cur; // ?
1132+
}
1133+
10231134
}

0 commit comments

Comments
 (0)