Skip to content

Commit 7b83ee0

Browse files
committed
Runtime: 545 ms (Top 77.81%) | Memory: 132.6 MB (Top 61.42%)
1 parent a9e9cbe commit 7b83ee0

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1+
// Runtime: 545 ms (Top 77.81%) | Memory: 132.6 MB (Top 61.42%)
12
class Solution {
23
public:
34
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
45
vector<vector<int>> adjList(n);
56
vector<int> inDegree(n),cTime(n,0);
6-
7-
for(auto &r:relations) { // Create adjacency list and in degree count vectors.
7+
8+
for(auto &r:relations) { // Create adjacency list and in degree count vectors.
89
adjList[r[0]-1].push_back(r[1]-1);
9-
inDegree[r[1]-1]++;
10-
}
10+
inDegree[r[1]-1]++;
11+
}
1112
queue<pair<int,int>> q;
12-
13-
for(int i=0;i<n;i++) // Get all nodes with in-degree=0 and store add them to the queue.
13+
14+
for(int i=0;i<n;i++) // Get all nodes with in-degree=0 and store add them to the queue.
1415
if(!inDegree[i])
1516
q.push({i,0});
1617

1718
while(!q.empty()) {
18-
auto [node,t]=q.front(); // Process node `node`.
19+
auto [node,t]=q.front(); // Process node `node`.
1920
q.pop();
20-
21-
// Completion time of the current node the time when the processing started `t`
22-
// (Max time at which prerequisutes completed) + the time taken to process it `time[node]`.
21+
22+
// Completion time of the current node the time when the processing started `t`
23+
// (Max time at which prerequisutes completed) + the time taken to process it `time[node]`.
2324
int completionTime=t+time[node];
24-
cTime[node]=completionTime; // Store the final completion time of the node `node`.
25-
26-
for(int &n:adjList[node]) {
27-
// Update the intermediate completion time of the child node `n`.
28-
// This means that node `n` would start processing at least at `cTime[n]`.
25+
cTime[node]=completionTime; // Store the final completion time of the node `node`.
26+
27+
for(int &n:adjList[node]) {
28+
// Update the intermediate completion time of the child node `n`.
29+
// This means that node `n` would start processing at least at `cTime[n]`.
2930
cTime[n]=max(cTime[n],completionTime);
30-
31-
if(!--inDegree[n]) // Add the node with in-degree=0 to the queue.
31+
32+
if(!--inDegree[n]) // Add the node with in-degree=0 to the queue.
3233
q.push({n,cTime[n]});
3334
}
3435
}
35-
// Return the maximum time it took for a node/course to complete as our result.
36+
// Return the maximum time it took for a node/course to complete as our result.
3637
return *max_element(cTime.begin(),cTime.end());
3738
}
38-
};
39+
};

0 commit comments

Comments
 (0)