力扣(leetcode)每日一题 815 公交路线 (图的宽度优先遍历变种)

815. 公交路线 - 力扣(LeetCode)

题干

给你一个数组 routes ,表示一系列公交线路,其中每个 routesi 表示一条公交线路,第 i 辆公交车将会在上面循环行驶。

例如,路线 routes0 = 1, 5, 7 表示第 0 辆公交车会一直按序列 1 -> 5 -> 7 -> 1 -> 5 -> 7 -> 1 -> ... 这样的车站路线行驶。

现在从 source 车站出发(初始时不在公交车上),要前往 target 车站。 期间仅可乘坐公交车。

求出 最少乘坐的公交车数量 。如果不可能到达终点车站,返回 -1 。

示例 1:

输入:routes = \[1,2,7,3,6,7], source = 1, target = 6

输出:2

解释:最优策略是先乘坐第一辆公交车到达车站 7 , 然后换乘第二辆公交车到车站 6 。

示例 2:

输入:routes = \[7,12,4,5,15,6,15,19,9,12,13], source = 15, target = 12

输出:-1

解法

首先,你需要一个车站对应公交车列表的hashmap

然后,你得到出发点车站。将对应的公交车列表都取出来,对应公交车列表对应的所有站台都拿出来。然后阶段距离。 一次类推。

也就是原来是点和点之间的连接,这里多了一层中介,就是公交车站台。

java 复制代码
    public static int numBusesToDestination(int[][] routes, int source, int target) {
        if (source == target) { // 目的地和出发点重合,且公交车不经过改地点
            return 0;
        }
        // routes  公交车对应的是目车站列表
        // 数值和车站
        HashMap<Integer, List<Integer>> map = new HashMap<>(); // 车站对应的公交车列表
        for (int i = 0; i < routes.length; i++) {
            int[] route = routes[i];
            for (int key : route) {
                List<Integer> list = map.getOrDefault(key, new ArrayList<>());
                list.add(i);
                map.put(key, list);
            }
        }
        if (map.get(source) == null || map.get(target) == null) {
            return -1;
        }

        HashSet<Integer> set = new HashSet<>(); // 这里记录

        HashMap<Integer, Integer> dict = new HashMap<>();
        // ArrayDeque
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        dict.put(source, 0);
        deque.add(source); // 车站出发点
        while (!deque.isEmpty()) {
            Integer pointx = deque.poll();  // 弹出车站
            Integer distance = dict.get(pointx);
            List<Integer> list = map.get(pointx);  // 得到公交车列表
            for (int i = 0; i < list.size(); i++) {
                Integer car = list.get(i); // 获取公交车
                if (!set.contains(car)) {  // 这个公交车没有用过
                    int[] route = routes[car];   // 得到所有对应的车子
                    for (int j = 0; j < route.length; j++) {
                        int pointy = route[j];
                        if (!dict.containsKey(pointy)) {
                            dict.put(pointy, distance + 1);
                            deque.add(pointy);
                        }
                    }
                    set.add(car);
                }
            }
        }
        if (dict.get(target) != null) {
            return dict.get(target);
        }
        return -1;
    }
相关推荐
LuminousCPP18 小时前
栈和队列专题(四):LeetCode 232. 用栈实现队列|双栈分工 + 按需迁移 + 摊还 O(1)
c语言·数据结构·笔记·算法·leetcode
青 春 记 忆19 小时前
LeetCode 155. 最小栈|Python 解法详解
开发语言·python·leetcode
鹿角片ljp1 天前
LeetCode 46. 全排列|吃透回溯
算法·leetcode·职场和发展
.道阻且长.1 天前
11.LeetCode算法习题讲解--滑动窗口--将x减到0的最小操作数
算法·leetcode·职场和发展
wenyq71 天前
LeetCode 2460. Apply Operations to an Array
算法·leetcode
青 春 记 忆1 天前
LeetCode 142. 环形链表 II|Python 解法详解
python·leetcode·链表
小欣加油1 天前
leetcode3069 将元素分配到两个数组中I
数据结构·c++·算法·leetcode·职场和发展
从琳开始9892 天前
优选算法——双指针(算法原理+力扣题)
算法·leetcode·职场和发展
从琳开始9892 天前
优选算法——滑动窗口(概念+解题模板+LeetCode例题讲解)
算法·leetcode·职场和发展
Nil2082 天前
leetcode 94二叉树的中序遍历
算法·leetcode·职场和发展