1
+ // Runtime: 545 ms (Top 77.81%) | Memory: 132.6 MB (Top 61.42%)
1
2
class Solution {
2
3
public:
3
4
int minimumTime (int n, vector<vector<int >>& relations, vector<int >& time) {
4
5
vector<vector<int >> adjList (n);
5
6
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.
8
9
adjList[r[0 ]-1 ].push_back (r[1 ]-1 );
9
- inDegree[r[1 ]-1 ]++;
10
- }
10
+ inDegree[r[1 ]-1 ]++;
11
+ }
11
12
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.
14
15
if (!inDegree[i])
15
16
q.push ({i,0 });
16
17
17
18
while (!q.empty ()) {
18
- auto [node,t]=q.front (); // Process node `node`.
19
+ auto [node,t]=q.front (); // Process node `node`.
19
20
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]`.
23
24
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]`.
29
30
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.
32
33
q.push ({n,cTime[n]});
33
34
}
34
35
}
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.
36
37
return *max_element (cTime.begin (),cTime.end ());
37
38
}
38
- };
39
+ };
0 commit comments