You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
long ans = 0;
public long minimumFuelCost(int[][] roads, int seats) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= roads.length; ++i) {
graph.add(new ArrayList<>());
}
// build graph
for (int[] road : roads) {
graph.get(road[0]).add(road[1]);
graph.get(road[1]).add(road[0]);
}
// dfs
dfs(graph, 0, 0, seats);
return ans;
}
private int dfs(List<List<Integer>> graph, int i, int prev, int seats) {
int people = 1;
for (int j : graph.get(i)) {
if (j != prev) {
people += dfs(graph, j, i, seats);
}
}
if (i != 0) {
ans += Math.ceil(people * 1.0 / seats);
}
return people;
}
}
这道题我并没有什么思路,想到car数量不够会怎样,如何一次性convey所有人,如何分配car,如何计算fuel。
但要知道,graph,tree类型的问题,solution都是整治法化为一个个小问题,然后积累解决。
正解:
The text was updated successfully, but these errors were encountered: