-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathLargest Color Value in a Directed Graph.java
39 lines (39 loc) · 1.3 KB
/
Largest Color Value in a Directed Graph.java
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
class Solution {
public int largestPathValue(String colors, int[][] edges) {
int n = colors.length();
Map<Integer, List<Integer>> graph = new HashMap<>();
int[] indegree = new int[n];
for (int[] edge : edges) {
graph.computeIfAbsent(edge[0], k -> new ArrayList<>())
.add(edge[1]);
indegree[edge[1]]++;
}
int[][] count = new int[n][26];
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < n; i++) {
if (indegree[i] == 0) {
queue.add(i);
}
}
int result = 1;
int seen = 0;
while (!queue.isEmpty()) {
int node = queue.remove();
result = Math.max(result, ++count[node][colors.charAt(node) - 'a']);
seen++;
if (!graph.containsKey(node)) {
continue;
}
for (int neighbor : graph.get(node)) {
for (int i = 0; i < 26; i++) {
count[neighbor][i] = Math.max(count[neighbor][i], count[node][i]);
}
indegree[neighbor]--;
if (indegree[neighbor] == 0) {
queue.add(neighbor);
}
}
}
return seen < n ? -1 : result;
}
}