力扣每日一题: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;
    }
}
相关推荐
天赐学c语言5 小时前
1.6 - 复制IP地址 && vector和list的区别
c++·算法·leecode
多米Domi0115 小时前
0x3f 第23天 黑马web (前端三件套,maven,web入门、mysql)黑马反射注解 hot100普通数组
java·python·mysql·算法·leetcode·maven
上海锟联科技5 小时前
DAS-U1000 极致版解调卡
数据结构·算法·嵌入式实时数据库
Swift社区13 小时前
LeetCode 465 最优账单平衡
算法·leetcode·职场和发展
聆风吟º13 小时前
【数据结构手札】空间复杂度详解:概念 | 习题
java·数据结构·算法
weixin_4450547214 小时前
力扣热题51
c++·python·算法·leetcode
地平线开发者14 小时前
linux 常见稳定性问题分析方法
算法·自动驾驶
s砚山s14 小时前
代码随想录刷题——二叉树篇(九)
算法
地平线开发者14 小时前
大模型常见量化方法简介
算法·自动驾驶
smj2302_7968265217 小时前
解决leetcode第3801题合并有序列表的最小成本
数据结构·python·算法·leetcode