力扣每日一题
题目:1466. 重新规划路线
日期:2023-12-07
用时:45 m 36 s
时间:37ms
内存:69.64MB
代码:
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;
}
}