forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
39 lines (39 loc) · 1.18 KB
/
Solution.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();
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
int[] indeg = new int[n];
for (int[] e : edges) {
int a = e[0], b = e[1];
g[a].add(b);
++indeg[b];
}
Deque<Integer> q = new ArrayDeque<>();
int[][] dp = new int[n][26];
for (int i = 0; i < n; ++i) {
if (indeg[i] == 0) {
q.offer(i);
int c = colors.charAt(i) - 'a';
++dp[i][c];
}
}
int cnt = 0;
int ans = 1;
while (!q.isEmpty()) {
int i = q.pollFirst();
++cnt;
for (int j : g[i]) {
if (--indeg[j] == 0) {
q.offer(j);
}
int c = colors.charAt(j) - 'a';
for (int k = 0; k < 26; ++k) {
dp[j][k] = Math.max(dp[j][k], dp[i][k] + (c == k ? 1 : 0));
ans = Math.max(ans, dp[j][k]);
}
}
}
return cnt == n ? ans : -1;
}
}