-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3965.cpp
More file actions
22 lines (22 loc) · 784 Bytes
/
Copy path3965.cpp
File metadata and controls
22 lines (22 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
long long dfs(int node, vector<vector<int>>& graph, vector<int>& baseTime) {
if (graph[node].size() == 0) return baseTime[node];
long long minV = LLONG_MAX;
long long maxV = LLONG_MIN;
for (auto& neighbor : graph[node]) {
long long v = dfs(neighbor, graph, baseTime);
minV = min(minV, v);
maxV = max(maxV, v);
}
long long ownDuration = maxV - minV + baseTime[node];
return maxV + ownDuration;
}
long long finishTime(int n, vector<vector<int>>& edges, vector<int>& baseTime) {
vector<vector<int>> graph(n);
for (auto& edge : edges) {
graph[edge[0]].push_back(edge[1]);
}
return dfs(0, graph, baseTime);
}
};