Skip to content

Commit

Permalink
add 815 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 20, 2024
1 parent b8e52ad commit f2106b4
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@
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)
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)
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) | _O(\|E\| * log\|V\|)_ | _O(\|E\|)_ | Hard | shortest route, graph, bfs,`amazon`| AGAIN*** (2) (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)
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)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-12-20
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-12-15
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

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 @@
20241220: 815
20241215: 1109
20241214: 560,523
20241208: 304,853,325
Expand Down
42 changes: 23 additions & 19 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,51 @@
2025-02-13 -> ['815']
2025-02-08 -> ['1109']
2025-02-07 -> ['560,523']
2025-02-01 -> ['304,853,325']
2025-01-26 -> ['370(todo),1109(todo)']
2025-01-26 -> ['370(todo)']
2025-01-24 -> ['34,767']
2025-01-23 -> ['815']
2025-01-20 -> ['722,380']
2025-01-19 -> ['33,81']
2025-01-18 -> ['1109']
2025-01-17 -> ['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-11 -> ['304,853,325', '394']
2025-01-10 -> ['833,950']
2025-01-05 -> ['370(todo),1109(todo)']
2025-01-10 -> ['815', '833,950']
2025-01-05 -> ['1109', '370(todo)']
2025-01-04 -> ['560,523', '53,210,207']
2025-01-03 -> ['34,767', '444']
2025-01-02 -> ['1188,130,855(again)']
2025-01-02 -> ['815', '1188,130,855(again)']
2024-12-30 -> ['722,380']
2024-12-29 -> ['304,853,325', '33,81']
2024-12-28 -> ['900']
2024-12-28 -> ['815', '1109', '900']
2024-12-27 -> ['560,523', '253', '26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
2024-12-25 -> ['815', '004(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-23 -> ['370(todo),1109(todo)']
2024-12-22 -> ['560,523']
2024-12-21 -> ['304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-23 -> ['815', '1109', '370(todo)']
2024-12-22 -> ['815', '560,523']
2024-12-21 -> ['815', '304,853,325', '34,767', '394', '855,846']
2024-12-20 -> ['1109', '833,950', '932']
2024-12-19 -> ['560,523']
2024-12-18 -> ['951,792']
2024-12-17 -> ['560,523', '722,380']
2024-12-16 -> ['560,523', '304,853,325', '33,81']
2024-12-15 -> ['560,523', '370(todo),1109(todo)']
2024-12-18 -> ['1109', '951,792']
2024-12-17 -> ['1109', '560,523', '722,380']
2024-12-16 -> ['1109', '560,523', '304,853,325', '33,81']
2024-12-15 -> ['560,523', '370(todo)']
2024-12-14 -> ['253', '53,210,207', '163,1048']
2024-12-13 -> ['304,853,325', '34,767', '776,31', '444', '298,729']
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
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)']
2024-12-10 -> ['304,853,325', '370(todo),1109(todo)']
2024-12-10 -> ['304,853,325', '370(todo)']
2024-12-09 -> ['304,853,325', '722,380']
2024-12-08 -> ['34,767', '33,81', '394', '737']
2024-12-07 -> ['370(todo),1109(todo)', '833,950', '900', '686,734,737']
2024-12-07 -> ['370(todo)', '833,950', '900', '686,734,737']
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
2024-12-05 -> ['370(todo),1109(todo)', '34,767', '776,31', '528,334']
2024-12-04 -> ['370(todo),1109(todo)', '722,380', '004(todo),34(todo),162(todo),275(todo)']
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']
2024-12-05 -> ['370(todo)', '34,767', '776,31', '528,334']
2024-12-04 -> ['370(todo)', '722,380', '004(todo),34(todo),162(todo),275(todo)']
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']
2024-12-02 -> ['34,767']
2024-12-01 -> ['34,767', '722,380', '253', '53,210,207']
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
Expand Down
248 changes: 248 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/BFS/BusRoutes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package LeetCodeJava.BFS;

// https://leetcode.com/problems/bus-routes/description/

import java.util.*;

/**
* 815. Bus Routes
* Solved
* Hard
* Topics
* Companies
* You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.
* <p>
* 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.
* 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.
* <p>
* Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* Input: routes = [[1,2,7],[3,6,7]], source = 1, target = 6
* Output: 2
* Explanation: The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.
* Example 2:
* <p>
* Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
* Output: -1
* <p>
* <p>
* <p>
* <p>
* Constraints:
* <p>
* 1 <= routes.length <= 500.
* 1 <= routes[i].length <= 105
* All the values of routes[i] are unique.
* sum(routes[i].length) <= 105
* 0 <= routes[i][j] < 106
* 0 <= source, target < 106
*/
public class BusRoutes {

// V0
// TODO : fix below
// IDEA : BFS
// public int numBusesToDestination(int[][] routes, int source, int target) {
//
// // edge case
// if (routes.length == 1) {
// if (source == target) {
// return 0;
// }
// return 1;
// }
// // build graph
// Map<Integer, List<Integer>> graph = new HashMap<>();
// for (int[] x : routes) {
// int start = x[0];
// int end = x[1];
// List<Integer> cur = graph.getOrDefault(start, new ArrayList<>());
// cur.add(end);
// graph.put(start, cur); // double check ???
// }
// // bfs
// int cnt = 0;
// Queue<Integer> queue = new LinkedList<>();
// queue.add(source);
// Set<Integer> visited = new HashSet<>();
// while (!queue.isEmpty()) {
// Integer curr = queue.poll();
// // visit "neighbor"
// if (!graph.keySet().isEmpty()) {
// for (Integer x : graph.get(curr)) {
// if (!visited.contains(x)) {
// if (target == x) {
// return cnt;
// }
// cnt += 1;
// queue.add(x);
// visited.add(x);
// }
// }
// }
// }
//
// //return cnt > 0 ? cnt : -1;
// return -1;
// }

// V1
// IDEA : Breadth-First Search (BFS) with Bus Stops as Nodes
// https://leetcode.com/problems/bus-routes/editorial/
public int numBusesToDestination_1(int[][] routes, int source, int target) {
if (source == target) {
return 0;
}
HashMap<Integer, ArrayList<Integer>> adjList = new HashMap<>();
// Create a map from the bus stop to all the routes that include this stop.
for (int r = 0; r < routes.length; r++) {
for (int stop : routes[r]) {
// Add all the routes that have this stop.
ArrayList<Integer> route = adjList.getOrDefault(
stop,
new ArrayList<>()
);
route.add(r);
adjList.put(stop, route);
}
}

Queue<Integer> q = new LinkedList<>();
Set<Integer> vis = new HashSet<Integer>(routes.length);
// Insert all the routes in the queue that have the source stop.
for (int route : adjList.get(source)) {
q.add(route);
vis.add(route);
}

int busCount = 1;
while (!q.isEmpty()) {
int size = q.size();

for (int i = 0; i < size; i++) {
int route = q.remove();

// Iterate over the stops in the current route.
for (int stop : routes[route]) {
// Return the current count if the target is found.
if (stop == target) {
return busCount;
}

// Iterate over the next possible routes from the current stop.
for (int nextRoute : adjList.get(stop)) {
if (!vis.contains(nextRoute)) {
vis.add(nextRoute);
q.add(nextRoute);
}
}
}
}
busCount++;
}
return -1;
}

// V2
// IDEA : Breadth-First Search (BFS) with Routes as Nodes (test OK)
// https://leetcode.com/problems/bus-routes/editorial/

List<List<Integer>> adjList = new ArrayList();

// Iterate over each pair of routes and add an edge between them if there's a
// common stop.
void createGraph(int[][] routes) {
for (int i = 0; i < routes.length; i++) {
for (int j = i + 1; j < routes.length; j++) {
if (haveCommonNode(routes[i], routes[j])) {
adjList.get(i).add(j);
adjList.get(j).add(i);
}
}
}
}

// Returns true if the provided routes have a common stop, false otherwise.
boolean haveCommonNode(int[] route1, int[] route2) {
int i = 0, j = 0;
while (i < route1.length && j < route2.length) {
if (route1[i] == route2[j]) {
return true;
}

if (route1[i] < route2[j]) {
i++;
} else {
j++;
}
}
return false;
}

// Add all the routes in the queue that have the source as one of the stops.
void addStartingNodes(Queue<Integer> q, int[][] routes, int source) {
for (int i = 0; i < routes.length; i++) {
if (isStopExist(routes[i], source)) {
q.add(i);
}
}
}

// Returns true if the given stop is present in the route, false otherwise.
boolean isStopExist(int[] route, int stop) {
for (int j = 0; j < route.length; j++) {
if (route[j] == stop) {
return true;
}
}
return false;
}

public int numBusesToDestination_2(int[][] routes, int source, int target) {
if (source == target) {
return 0;
}

for (int i = 0; i < routes.length; ++i) {
Arrays.sort(routes[i]);
adjList.add(new ArrayList());
}

createGraph(routes);

Queue<Integer> q = new LinkedList<>();
addStartingNodes(q, routes, source);

Set<Integer> vis = new HashSet<Integer>(routes.length);
int busCount = 1;
while (!q.isEmpty()) {
int sz = q.size();

while (sz-- > 0) {
int node = q.remove();

// Return busCount, if the stop target is present in the current route.
if (isStopExist(routes[node], target)) {
return busCount;
}

// Add the adjacent routes.
for (int adj : adjList.get(node)) {
if (!vis.contains(adj)) {
vis.add(adj);
q.add(adj);
}
}
}

busCount++;
}

return -1;
}

}
Loading

0 comments on commit f2106b4

Please sign in to comment.