815. Bus Routes

Description

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

BFS, O(n ^ 2), S(n*L + edges)

很有趣的题,很容易被障眼法迷惑住,如果一旦将每个站点作为Graph Node,感觉这题就没救儿了。。

正确的做法是,将Bus作为Graph Node!然后根据Bus之间是否有公共站点,来build graph。最后直接BFS就行了。

class Solution {
    public int numBusesToDestination(int[][] routes, int s, int t) {
        if (s == t) {
            return 0;
        }
        
        int n = routes.length;
        // each bus is a node with id (routes index)
        Map<Integer, List<Integer>> graph = new HashMap<>();
        // stops(i) is just a copy of routes[i] for fast accessing
        Map<Integer, Set<Integer>> stops = new HashMap<>();
        
        for (int i = 0; i < n; ++i) {
            graph.put(i, new ArrayList<>());
            Set<Integer> path = new HashSet<>();
            for (int j : routes[i]) {
                path.add(j);
            }
            stops.put(i, path);
        }
        // build graph, just add edge to buses that have intersection
        for (int i = 0; i < n - 1; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (hasIntersect(stops.get(i), stops.get(j))) {
                    graph.get(i).add(j);
                    graph.get(j).add(i);
                }
            }
        }
        // normal BFS
        Queue<int[]> queue = new LinkedList<>();
        Set<Integer> used = new HashSet<>();
        for (int i = 0; i < n; ++i) {
            if (stops.get(i).contains(s)) {
                queue.offer(new int[] {i, 1});
                used.add(i);
            }
        }
        
        while (!queue.isEmpty()) {
            int[] arr = queue.poll();
            int i = arr[0];
            int depth = arr[1];
            
            if (stops.get(i).contains(t)) {
                return depth;
            }
            
            for (int next : graph.get(i)) {
                if (used.add(next)) {
                    queue.offer(new int[] {next, depth + 1});
                }
            }
        }
        
        return -1;
    }
    
    private boolean hasIntersect(Set<Integer> set1, Set<Integer> set2) {
        Iterator<Integer> iter1 = set1.iterator();
        while (iter1.hasNext()) {
            if (set2.contains(iter1.next())) {
                return true;
            }
        }
        
        return false;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,871评论 0 10
  • “女人是用来被爱的,不是用来被理解的。”王尔德如是说。 “艺术并非模仿生活,而是生活模仿艺术”王尔德又说。 ...
    木字铭阅读 326评论 0 0
  • 如意黄四阅读 101评论 0 0
  • 罗胖添了两个宝贝女儿,找樊登来代班,樊登讲了《联盟》这本书,让我虎躯为之一震。立马买了《联盟》并订阅了一年樊登读书...
    路叔叔阅读 1,008评论 0 2
  • 随笔1 回程感叹 妈妈来看我了,我回去了,没见着。想到这里鼻子酸酸的。妈吃好午饭从工地上来,因为没有车,...
    笨熊懒猪阅读 222评论 0 0

友情链接更多精彩内容