其他分享
首页 > 其他分享> > LeetCode 815. Bus Routes

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.

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:

题解:

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>> stationToRoutes = new HashMap<>();
 4         for(int i = 0; i < routes.length; i++){
 5             for(int j : routes[i]){
 6                 stationToRoutes.putIfAbsent(j, new HashSet<Integer>());
 7                 stationToRoutes.get(j).add(i);
 8             }
 9         }
10         
11         LinkedList<int []> que = new LinkedList<>();
12         HashSet<Integer> visitedStation = new HashSet<>();
13         boolean[] visitedRoute = new boolean[routes.length];
14         que.add(new int[]{source, 0});
15         visitedStation.add(source);
16         while(!que.isEmpty()){
17             int [] cur = que.poll();
18             if(cur[0] == target){
19                 return cur[1];
20             }
21             
22             for(int route : stationToRoutes.get(cur[0])){
23                 if(visitedRoute[route]){
24                     continue;
25                 }
26                 
27                 for(int next : routes[route]){
28                     if(!visitedStation.contains(next)){
29                         que.add(new int[]{next, cur[1] + 1});
30                         visitedStation.add(next);
31                     }
32                 }
33                 
34                 visitedRoute[route] = true;
35             }
36         }
37         
38         return -1;
39     }
40 }

 

标签:target,source,int,Bus,routes,bus,Routes,new,LeetCode
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16536485.html