-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-065-semestersRequired.js
145 lines (110 loc) · 3.03 KB
/
structy-065-semestersRequired.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// https://structy.net/problems/premium/semesters-required
// p: num+ & list
// r: num (min of semesters)
// dfs recursion
const semestersRequired = (numCourses, prereqs) => {
if (prereqs.length === 0) return 1;
const graph = graphConverter(prereqs);
const memo = {};
let max = -Infinity;
for (let node in graph) {
max = Math.max(max, explore(graph, node, memo));
}
return max;
};
const explore = (graph, node, memo) => {
if (graph[node].length === 0) return 1;
if (node in memo) return memo[node];
let max = -Infinity;
for (let neighbor of graph[node]) {
count += explore(graph, neighbor, memo);
max = Math.max(count, max);
}
memo[node] = max;
return max;
};
//{ '0': [], '1': [ 0, 2 ], '2': [], '3': [ 4, 2 ], '4': [] }
// // dfs recursion WRONG
// const semestersRequired = (numCourses, prereqs) => {
// if (prereqs.length === 0) return 1;
// const graph = graphConverter(prereqs);
// let max = -Infinity;
// for (const node in graph) {
// max = explore(graph, node, max);
// }
// return max;
// };
// const explore = (graph, node, max) => {
// if (graph[node].length === 0) return 1;
// let count = 1;
// for (const neighbor of graph[node]) {
// count += explore(graph, neighbor, max);
// // console.log(count);
// max = Math.max(max, count);
// }
// return max;
// };
// { '0': [ 5 ], '1': [ 2 ], '2': [ 4 ], '3': [ 5 ], '4': [], '5': [] }
// // bfs iteration
// const semestersRequired = (numCourses, prereqs) => {
// if (prereqs.length === 0) return 1;
// const graph = graphConverter(prereqs);
// // const visited = new Set();
// let max = -Infinity;
// for (const node in graph) {
// const stack = [[node, 1]];
// while (stack.length > 0) {
// const [neighbor, sem] = stack.pop();
// max = Math.max(max, sem);
// for (const neighbor of graph[neighbor]) {
// const curToNei = neighbor + "," + neighbor;
// // if (visited.has(curToNei)) continue;
// // visited.add(curToNei);
// stack.push([neighbor, sem + 1]);
// console.log(neighbor, neighbor, sem);
// }
// }
// console.log("---");
// }
// return max;
// };
const graphConverter = (edges) => {
const graph = {};
for (const edge of edges) {
const [a, b] = edge;
!(a in graph) && (graph[a] = []);
!(b in graph) && (graph[b] = []);
graph[a].push(b);
}
return graph;
};
// const numCourses = 6;
// const prereqs = [
// [1, 2],
// [2, 4],
// [3, 5],
// [0, 5],
// ]; // -> 3
// const numCourses = 3;
// const prereqs = [
// [0, 2],
// [0, 1],
// [1, 2],
// ]; // -> 3
// const numCourses = 7;
// const prereqs = [
// [4, 3],
// [3, 2],
// [2, 1],
// [1, 0],
// [5, 2],
// [5, 6],
// ]; // -> 5
const numCourses = 5;
const prereqs = [
[1, 0],
[3, 4],
[1, 2],
[3, 2],
]; // -> 2
console.log(semestersRequired(numCourses, prereqs)); // -> 3