Skip to content

Commit f2106b4

Browse files
committed
add 815 java, progress
1 parent b8e52ad commit f2106b4

File tree

6 files changed

+363
-21
lines changed

6 files changed

+363
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@
932932
743|[Network Delay Time](https://leetcode.com/problems/network-delay-time/)| [Python](./leetcode_python/Breadth-First-Search/network-delay-time.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/NetworkDelayTime.java) | _O(n)_ | _O(n)_ | Medium |Dijlstra, `google`| AGAIN **** (2)
933933
752|[Open the Lock](https://leetcode.com/problems/open-the-lock/)| [Python](./leetcode_python/Breadth-First-Search/open-the-lock.py) | _O(k * n^k + d)_ | _O(k * n^k + d)_ | Medium |LC 863 | AGAIN (not start)
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)
935-
815|[Bus Routes](https://leetcode.com/problems/bus-routes/)| [Python](./leetcode_python/Breadth-First-Search/bus-routes.py) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Hard | shortest route, graph, bfs,`amazon`| AGAIN*** (2) (not start)
935+
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)
937937
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)
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)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-12-20
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-12-15
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241220: 815
12
20241215: 1109
23
20241214: 560,523
34
20241208: 304,853,325

data/to_review.txt

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
1+
2025-02-13 -> ['815']
2+
2025-02-08 -> ['1109']
13
2025-02-07 -> ['560,523']
24
2025-02-01 -> ['304,853,325']
3-
2025-01-26 -> ['370(todo),1109(todo)']
5+
2025-01-26 -> ['370(todo)']
46
2025-01-24 -> ['34,767']
7+
2025-01-23 -> ['815']
58
2025-01-20 -> ['722,380']
69
2025-01-19 -> ['33,81']
10+
2025-01-18 -> ['1109']
711
2025-01-17 -> ['560,523', '253']
812
2025-01-16 -> ['776,31']
913
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
1014
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1115
2025-01-11 -> ['304,853,325', '394']
12-
2025-01-10 -> ['833,950']
13-
2025-01-05 -> ['370(todo),1109(todo)']
16+
2025-01-10 -> ['815', '833,950']
17+
2025-01-05 -> ['1109', '370(todo)']
1418
2025-01-04 -> ['560,523', '53,210,207']
1519
2025-01-03 -> ['34,767', '444']
16-
2025-01-02 -> ['1188,130,855(again)']
20+
2025-01-02 -> ['815', '1188,130,855(again)']
1721
2024-12-30 -> ['722,380']
1822
2024-12-29 -> ['304,853,325', '33,81']
19-
2024-12-28 -> ['900']
23+
2024-12-28 -> ['815', '1109', '900']
2024
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2125
2024-12-26 -> ['776,31']
22-
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
26+
2024-12-25 -> ['815', '004(todo),34(todo),162(todo),275(todo)']
2327
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
24-
2024-12-23 -> ['370(todo),1109(todo)']
25-
2024-12-22 -> ['560,523']
26-
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
27-
2024-12-20 -> ['833,950', '932']
28+
2024-12-23 -> ['815', '1109', '370(todo)']
29+
2024-12-22 -> ['815', '560,523']
30+
2024-12-21 -> ['815', '304,853,325', '34,767', '394', '855,846']
31+
2024-12-20 -> ['1109', '833,950', '932']
2832
2024-12-19 -> ['560,523']
29-
2024-12-18 -> ['951,792']
30-
2024-12-17 -> ['560,523', '722,380']
31-
2024-12-16 -> ['560,523', '304,853,325', '33,81']
32-
2024-12-15 -> ['560,523', '370(todo),1109(todo)']
33+
2024-12-18 -> ['1109', '951,792']
34+
2024-12-17 -> ['1109', '560,523', '722,380']
35+
2024-12-16 -> ['1109', '560,523', '304,853,325', '33,81']
36+
2024-12-15 -> ['560,523', '370(todo)']
3337
2024-12-14 -> ['253', '53,210,207', '163,1048']
3438
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
3539
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
3640
2024-12-11 -> ['304,853,325', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
37-
2024-12-10 -> ['304,853,325', '370(todo),1109(todo)']
41+
2024-12-10 -> ['304,853,325', '370(todo)']
3842
2024-12-09 -> ['304,853,325', '722,380']
3943
2024-12-08 -> ['34,767', '33,81', '394', '737']
40-
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
44+
2024-12-07 -> ['370(todo)', '833,950', '900', '686,734,737']
4145
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
42-
2024-12-05 -> ['370(todo),1109(todo)', '34,767', '776,31', '528,334']
43-
2024-12-04 -> ['370(todo),1109(todo)', '722,380', '004(todo),34(todo),162(todo),275(todo)']
44-
2024-12-03 -> ['370(todo),1109(todo)', '34,767', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
46+
2024-12-05 -> ['370(todo)', '34,767', '776,31', '528,334']
47+
2024-12-04 -> ['370(todo)', '722,380', '004(todo),34(todo),162(todo),275(todo)']
48+
2024-12-03 -> ['370(todo)', '34,767', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
4549
2024-12-02 -> ['34,767']
4650
2024-12-01 -> ['34,767', '722,380', '253', '53,210,207']
4751
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
package LeetCodeJava.BFS;
2+
3+
// https://leetcode.com/problems/bus-routes/description/
4+
5+
import java.util.*;
6+
7+
/**
8+
* 815. Bus Routes
9+
* Solved
10+
* Hard
11+
* Topics
12+
* Companies
13+
* You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.
14+
* <p>
15+
* For example, if routes[0] = [1, 5, 7], this means that the 0th bus travels in the sequence 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... forever.
16+
* You will start at the bus stop source (You are not on any bus initially), and you want to go to the bus stop target. You can travel between bus stops by buses only.
17+
* <p>
18+
* Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.
19+
* <p>
20+
* <p>
21+
* <p>
22+
* Example 1:
23+
* <p>
24+
* Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
25+
* Output: 2
26+
* Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
27+
* Example 2:
28+
* <p>
29+
* Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
30+
* Output: -1
31+
* <p>
32+
* <p>
33+
* <p>
34+
* <p>
35+
* Constraints:
36+
* <p>
37+
* 1 <= routes.length <= 500.
38+
* 1 <= routes[i].length <= 105
39+
* All the values of routes[i] are unique.
40+
* sum(routes[i].length) <= 105
41+
* 0 <= routes[i][j] < 106
42+
* 0 <= source, target < 106
43+
*/
44+
public class BusRoutes {
45+
46+
// V0
47+
// TODO : fix below
48+
// IDEA : BFS
49+
// public int numBusesToDestination(int[][] routes, int source, int target) {
50+
//
51+
// // edge case
52+
// if (routes.length == 1) {
53+
// if (source == target) {
54+
// return 0;
55+
// }
56+
// return 1;
57+
// }
58+
// // build graph
59+
// Map<Integer, List<Integer>> graph = new HashMap<>();
60+
// for (int[] x : routes) {
61+
// int start = x[0];
62+
// int end = x[1];
63+
// List<Integer> cur = graph.getOrDefault(start, new ArrayList<>());
64+
// cur.add(end);
65+
// graph.put(start, cur); // double check ???
66+
// }
67+
// // bfs
68+
// int cnt = 0;
69+
// Queue<Integer> queue = new LinkedList<>();
70+
// queue.add(source);
71+
// Set<Integer> visited = new HashSet<>();
72+
// while (!queue.isEmpty()) {
73+
// Integer curr = queue.poll();
74+
// // visit "neighbor"
75+
// if (!graph.keySet().isEmpty()) {
76+
// for (Integer x : graph.get(curr)) {
77+
// if (!visited.contains(x)) {
78+
// if (target == x) {
79+
// return cnt;
80+
// }
81+
// cnt += 1;
82+
// queue.add(x);
83+
// visited.add(x);
84+
// }
85+
// }
86+
// }
87+
// }
88+
//
89+
// //return cnt > 0 ? cnt : -1;
90+
// return -1;
91+
// }
92+
93+
// V1
94+
// IDEA : Breadth-First Search (BFS) with Bus Stops as Nodes
95+
// https://leetcode.com/problems/bus-routes/editorial/
96+
public int numBusesToDestination_1(int[][] routes, int source, int target) {
97+
if (source == target) {
98+
return 0;
99+
}
100+
HashMap<Integer, ArrayList<Integer>> adjList = new HashMap<>();
101+
// Create a map from the bus stop to all the routes that include this stop.
102+
for (int r = 0; r < routes.length; r++) {
103+
for (int stop : routes[r]) {
104+
// Add all the routes that have this stop.
105+
ArrayList<Integer> route = adjList.getOrDefault(
106+
stop,
107+
new ArrayList<>()
108+
);
109+
route.add(r);
110+
adjList.put(stop, route);
111+
}
112+
}
113+
114+
Queue<Integer> q = new LinkedList<>();
115+
Set<Integer> vis = new HashSet<Integer>(routes.length);
116+
// Insert all the routes in the queue that have the source stop.
117+
for (int route : adjList.get(source)) {
118+
q.add(route);
119+
vis.add(route);
120+
}
121+
122+
int busCount = 1;
123+
while (!q.isEmpty()) {
124+
int size = q.size();
125+
126+
for (int i = 0; i < size; i++) {
127+
int route = q.remove();
128+
129+
// Iterate over the stops in the current route.
130+
for (int stop : routes[route]) {
131+
// Return the current count if the target is found.
132+
if (stop == target) {
133+
return busCount;
134+
}
135+
136+
// Iterate over the next possible routes from the current stop.
137+
for (int nextRoute : adjList.get(stop)) {
138+
if (!vis.contains(nextRoute)) {
139+
vis.add(nextRoute);
140+
q.add(nextRoute);
141+
}
142+
}
143+
}
144+
}
145+
busCount++;
146+
}
147+
return -1;
148+
}
149+
150+
// V2
151+
// IDEA : Breadth-First Search (BFS) with Routes as Nodes (test OK)
152+
// https://leetcode.com/problems/bus-routes/editorial/
153+
154+
List<List<Integer>> adjList = new ArrayList();
155+
156+
// Iterate over each pair of routes and add an edge between them if there's a
157+
// common stop.
158+
void createGraph(int[][] routes) {
159+
for (int i = 0; i < routes.length; i++) {
160+
for (int j = i + 1; j < routes.length; j++) {
161+
if (haveCommonNode(routes[i], routes[j])) {
162+
adjList.get(i).add(j);
163+
adjList.get(j).add(i);
164+
}
165+
}
166+
}
167+
}
168+
169+
// Returns true if the provided routes have a common stop, false otherwise.
170+
boolean haveCommonNode(int[] route1, int[] route2) {
171+
int i = 0, j = 0;
172+
while (i < route1.length && j < route2.length) {
173+
if (route1[i] == route2[j]) {
174+
return true;
175+
}
176+
177+
if (route1[i] < route2[j]) {
178+
i++;
179+
} else {
180+
j++;
181+
}
182+
}
183+
return false;
184+
}
185+
186+
// Add all the routes in the queue that have the source as one of the stops.
187+
void addStartingNodes(Queue<Integer> q, int[][] routes, int source) {
188+
for (int i = 0; i < routes.length; i++) {
189+
if (isStopExist(routes[i], source)) {
190+
q.add(i);
191+
}
192+
}
193+
}
194+
195+
// Returns true if the given stop is present in the route, false otherwise.
196+
boolean isStopExist(int[] route, int stop) {
197+
for (int j = 0; j < route.length; j++) {
198+
if (route[j] == stop) {
199+
return true;
200+
}
201+
}
202+
return false;
203+
}
204+
205+
public int numBusesToDestination_2(int[][] routes, int source, int target) {
206+
if (source == target) {
207+
return 0;
208+
}
209+
210+
for (int i = 0; i < routes.length; ++i) {
211+
Arrays.sort(routes[i]);
212+
adjList.add(new ArrayList());
213+
}
214+
215+
createGraph(routes);
216+
217+
Queue<Integer> q = new LinkedList<>();
218+
addStartingNodes(q, routes, source);
219+
220+
Set<Integer> vis = new HashSet<Integer>(routes.length);
221+
int busCount = 1;
222+
while (!q.isEmpty()) {
223+
int sz = q.size();
224+
225+
while (sz-- > 0) {
226+
int node = q.remove();
227+
228+
// Return busCount, if the stop target is present in the current route.
229+
if (isStopExist(routes[node], target)) {
230+
return busCount;
231+
}
232+
233+
// Add the adjacent routes.
234+
for (int adj : adjList.get(node)) {
235+
if (!vis.contains(adj)) {
236+
vis.add(adj);
237+
q.add(adj);
238+
}
239+
}
240+
}
241+
242+
busCount++;
243+
}
244+
245+
return -1;
246+
}
247+
248+
}

0 commit comments

Comments
 (0)