3
* @param {number[][]} relations4
* @param {number[]} time7
var minimumTime = function (n, relations, time) {9
Approach: We can create reverse edges for relation.10
Then longest path(by weightage of time for each node) from the node will be the minimum time to finish that course(node)11
Now we can use simple DFS to find the longest path for each node.12
The node containing the longest path will be course to finish the last.We can also use memo to save the longest path from node, so when we reach to this node, we need not to calculate the longest path again. 15
for (let i = 0; i < relations.length; i++) {16
if (edges[relations[i][1]] === undefined) {17
edges[relations[i][1]] = [];19
edges[relations[i][1]].push(relations[i][0]);25
for (let i = 1; i <= n; i++) {26
timeRequired = longestPath(i);27
max = Math.max(max, timeRequired);30
function longestPath(node) {31
if (memo[node] !== undefined) {36
if (edges[node] !== undefined) {37
for (let i = 0; i < edges[node].length; i++) {38
len = longestPath(edges[node][i]);39
max = Math.max(max, len);42
memo[node] = time[node - 1] + max; //use memo to save the longest path from node, so when we reach to this node, we need not to calculate the longest path again