力扣每日一题:1466. 重新规划路线(2023-12-07)

力扣每日一题

题目:1466. 重新规划路线

日期:2023-12-07

用时:45 m 36 s

时间:37ms

内存:69.64MB

代码:

java 复制代码
class Solution {
    public int minReorder(int n, int[][] connections) {
        list = new List[n];
        Arrays.setAll(list, k -> new ArrayList<>());
        for (int[] connection : connections) {
            int start = connection[0];
            int end = connection[1];
            list[start].add(new int[] {end, 1});
            list[end].add(new int[] {start, 0});
        }
        return dfs(0, -1);
    }
    
    List<int[]>[] list;

    private int dfs(int index, int target) {
        int ans = 0;
        for (int[] num : list[index]) {
            if (num[0] != target) {
                ans += num[1] + dfs(num[0], index);
            }
        }
        return ans;
    }
}
相关推荐
练习时长一年5 小时前
Leetcode热题100(跳跃游戏 II)
算法·leetcode·游戏
小白菜又菜10 小时前
Leetcode 3432. Count Partitions with Even Sum Difference
算法·leetcode
wuhen_n12 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo12 小时前
leetcode 2483
数据结构·算法·leetcode
Xの哲學12 小时前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
大头流矢13 小时前
归并排序与计数排序详解
数据结构·算法·排序算法
油泼辣子多加13 小时前
【信创】算法开发适配
人工智能·深度学习·算法·机器学习
Aaron158813 小时前
AD9084和Versal RF系列具体应用案例对比分析
嵌入式硬件·算法·fpga开发·硬件架构·硬件工程·信号处理·基带工程
laocooon52385788613 小时前
插入法排序 python
开发语言·python·算法
wuhen_n14 小时前
LeetCode -- 1:两数之和(简单)
javascript·算法·leetcode·职场和发展