力扣每日一题: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;
    }
}
相关推荐
wadesir4 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
yugi9878384 小时前
基于MATLAB实现协同过滤电影推荐系统
算法·matlab
TimberWill4 小时前
哈希-02-最长连续序列
算法·leetcode·排序算法
Morwit4 小时前
【力扣hot100】64. 最小路径和
c++·算法·leetcode
leoufung4 小时前
LeetCode 373. Find K Pairs with Smallest Sums:从暴力到堆优化的完整思路与踩坑
java·算法·leetcode
wifi chicken5 小时前
数组遍历求值,行遍历和列遍历谁更快
c语言·数据结构·算法
胡楚昊5 小时前
NSSCTF动调题包通关
开发语言·javascript·算法
Gold_Dino6 小时前
agc011_e 题解
算法
bubiyoushang8886 小时前
基于蚁群算法的直流电机PID参数整定 MATLAB 实现
数据结构·算法·matlab
风筝在晴天搁浅6 小时前
hot100 240.搜索二维矩阵Ⅱ
算法·矩阵