-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest-color-value-in-a-directed-graph.js
53 lines (41 loc) · 1.35 KB
/
largest-color-value-in-a-directed-graph.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
// https://leetcode.com/problems/largest-color-value-in-a-directed-graph/
const largestPathValue = (colors, edges) => {
const adjacencyList = createAdjacencyList(edges);
return largestColorValue(colors, adjacencyList);
}
const createAdjacencyList = (edges) => {
const adjacencyList = {};
for (let [start, end] of edges) {
adjacencyList[start] ||= [];
adjacencyList[start].push(end);
}
return adjacencyList;
}
const largestColorValue = (colors, adjacencyList) => {
const explored = new Set();
let result = 0;
for (let node = 0; node < colors.length; node++) {
const stack = [{ node, parents: new Set(), numColors: {}}];
while (stack.length) {
const { node, parents, numColors } = stack.pop();
if (parents.has(node)) {
return -1;
}
parents.add(node);
if (explored.has(node)) {
continue;
}
explored.add(node);
const color = colors[node];
numColors[color] ||= 0;
numColors[color]++;
const neighbours = adjacencyList[node];
if (neighbours) {
neighbours.forEach(n => stack.push({ node: n, parents: new Set(parents), numColors: { ...numColors } }));
}
const max = Object.keys(numColors).reduce((maxValue, currentColor) => Math.max(maxValue, numColors[currentColor]), 0);
result = Math.max(result, max);
}
}
return result;
}