力扣每日一题: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;
    }
}
相关推荐
白羊by3 小时前
YOLOv1~v11 全版本核心演进总览
深度学习·算法·yolo
墨尘笔尖4 小时前
最大最小值降采样算法的优化
c++·算法
white-persist6 小时前
【vulhub shiro 漏洞复现】vulhub shiro CVE-2016-4437 Shiro反序列化漏洞复现详细分析解释
运维·服务器·网络·python·算法·安全·web安全
FL16238631297 小时前
基于C#winform部署软前景分割DAViD算法的onnx模型实现前景分割
开发语言·算法·c#
baizhigangqw8 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶
算法·启发式算法·web app
C雨后彩虹8 小时前
最多等和不相交连续子序列
java·数据结构·算法·华为·面试
cpp_25019 小时前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
Hugh-Yu-1301239 小时前
二元一次方程组求解器c++代码
开发语言·c++·算法
编程大师哥9 小时前
C++类和对象
开发语言·c++·算法
加农炮手Jinx10 小时前
LeetCode 146. LRU Cache 题解
算法·leetcode·力扣