We have a list of bus routes. Each
routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.
We start at bus stop
S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.Example: Input: routes = [[1, 2, 7], [3, 6, 7]] S = 1 T = 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.
Note:
1 <= routes.length <= 500.1 <= routes[i].length <= 500.0 <= routes[i][j] < 10 ^ 6.
Solution #1 以bus route为⼀个node, 构建graph O(n * n * l) time, n为route的数量量,l为route的平均长度
class Solution {
public int numBusesToDestination(int[][] routes, int s, int t) {
if (s == t) return 0;
Map<Integer, Cluster> graph = new HashMap<>();
Queue<Integer> source = mergeRoutes(graph, routes, s);
Set<Integer> visited = new HashSet<>();
int len = 1;
while (!source.isEmpty()) {
int size = source.size();
while (size > 0) {
int top = source.poll();
size--;
if (visited.contains(top)) continue;
visited.add(top);
Cluster topC = graph.get(top);
if (topC.elems.contains(t)) return len;
for (int c : topC.neighbours) {
source.add(c);
}
}
len++;
}
return -1;
}
private Queue<Integer> mergeRoutes(Map<Integer, Cluster> graph, int[][] routes, int s) {
Queue<Integer> source = new LinkedList<>();
for (int i = 0; i < routes.length; i++) {
Cluster c = new Cluster(i, routes[i]);
graph.put(i, c);
for (int elem : routes[i]) {
if (elem == s) source.add(i);
for (Map.Entry<Integer, Cluster> entry : graph.entrySet()) {
if (entry.getValue().elems.contains(elem)) {
entry.getValue().neighbours.add(i);
c.neighbours.add(entry.getValue().index);
}
}
}
}
return source;
}
class Cluster{
public int index;
public Set<Integer> elems;
public Set<Integer> neighbours;
public Cluster(int index, int[] route) {
this.index = index;
elems = new HashSet<>();
for (int i : route) {
elems.add(i);
}
neighbours = new HashSet<>();
}
}
}
Solution#2, 可以用stop当作node构建graph,与上面是相同复杂度
ref: https://leetcode.com/problems/bus-routes/discuss/122712/Simple-Java-Solution-using-BFS?page=1
Node是stop,neighbour是bus routes,visited里面存的也是bus route
class Solution {
public int numBusesToDestination(int[][] routes, int s, int t) {
if (s == t) return 0;
Map<Integer, List<Integer>> stopToBus = new HashMap<>();
Set<Integer> visited = new HashSet<>();
for (int i = 0; i < routes.length; i++) {
for (int j = 0; j < routes[i].length; j++) {
int stop = routes[i][j];
List<Integer> ro = stopToBus.getOrDefault(stop, new ArrayList<Integer>());
ro.add(i);
stopToBus.put(stop, ro);
}
}
int r = 0;
Queue<Integer> que = new LinkedList<>();
que.add(s);
while (!que.isEmpty()) {
int len = que.size();
r++;
for (int i = 0; i < len; i++) {
int cur = que.poll();
for (int bus : stopToBus.get(cur)) {
if (visited.contains(bus)) continue;
visited.add(bus);
for (int stop : routes[bus]) {
if (stop == t) return r;
que.add(stop);
}
}
}
}
return -1;
}
}
No comments:
Post a Comment