LeetCode 815. Bus Routes

原题链接在这里:https://leetcode.com/problems/bus-routes/

题目:

You are given an array routes representing bus routes where routes[i] is a bus route that the ith bus repeats forever.

  • 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.

Return the least number of buses you must take to travel from source to target. Return -1 if it is not possible.

Example 1:

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:

Input: routes = [[7,12],[4,5,15],[6],[15,19],[9,12,13]], source = 15, target = 12
Output: -1

Constraints:

  • 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

题解:

The least number of buses you must take indicates that it is BFS question.

Have a map to record station to routes.

In BFS que, put pair of current station and current number of buses taken. At the begining, it is {source, 0}.

After polling the current pair, check if station is the target, if yes, then return corresponding number of buses taken.

Then get the list of routes stop at this station, for each unvisited routes, check its stopped stations. For each unvisited station, add {station, cur[1] + 1} to the queue. Since it must change to a different bus now.

Time Complexity: O(n ). n is the number of nodes. Here the maimum is routes.length * routes[i].length. Each node will only gets into queue once.

Space: O(n).

AC Java:

 1 class Solution {
 2     public int numBusesToDestination(int[][] routes, int source, int target) {
 3         Map<Integer, Set<Integer>> stationToRoute = new HashMap<>();
 4         for(int i = 0; i < routes.length; i++){
 5             for(int j : routes[i]){
 6                 stationToRoute.computeIfAbsent(j, x -> new HashSet<Integer>()).add(i);
 7             }
 8         }
 9 
10         Set<Integer> visitedStation = new HashSet<>();
11         Set<Integer> visitedRoute = new HashSet<>();
12         LinkedList<int[]> que = new LinkedList<>();
13         visitedStation.add(source);
14         que.add(new int[]{source, 0});
15         while(!que.isEmpty()){
16             int[] cur = que.poll();
17             if(cur[0] == target){
18                 return cur[1];
19             }
20 
21             for(int route: stationToRoute.getOrDefault(cur[0], new HashSet<Integer>())){
22                 if(visitedRoute.contains(route)){
23                     continue;
24                 }
25 
26                 visitedRoute.add(route);
27                 for(int next : routes[route]){
28                     if(visitedStation.add(next)){
29                         que.add(new int[]{next, cur[1] + 1});
30                     }
31                 }
32             }
33         }
34 
35         return -1;
36     }
37 }

 

posted @ 2022-07-31 09:41  Dylan_Java_NYC  阅读(46)  评论(0编辑  收藏  举报