力扣每日一题: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;
    }
}
相关推荐
luckys.one11 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
~|Bernard|13 小时前
在 PyCharm 里怎么“点鼠标”完成指令同样的运行操作
算法·conda
战术摸鱼大师13 小时前
电机控制(四)-级联PID控制器与参数整定(MATLAB&Simulink)
算法·matlab·运动控制·电机控制
Christo313 小时前
TFS-2018《On the convergence of the sparse possibilistic c-means algorithm》
人工智能·算法·机器学习·数据挖掘
好家伙VCC14 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
liulilittle15 小时前
IP校验和算法:从网络协议到SIMD深度优化
网络·c++·网络协议·tcp/ip·算法·ip·通信
bkspiderx17 小时前
C++经典的数据结构与算法之经典算法思想:贪心算法(Greedy)
数据结构·c++·算法·贪心算法
中华小当家呐18 小时前
算法之常见八大排序
数据结构·算法·排序算法
沐怡旸19 小时前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
一只懒洋洋19 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类