forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1857-largest-color-value-in-a-directed-graph.rs
51 lines (43 loc) · 1.48 KB
/
1857-largest-color-value-in-a-directed-graph.rs
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
use std::collections::VecDeque;
impl Solution {
pub fn largest_path_value(colors: String, edges: Vec<Vec<i32>>) -> i32 {
let n = colors.len();
let mut graph = vec![vec![]; n];
let mut indegree = vec![0; n];
for edge in edges {
let u = edge[0] as usize;
let v = edge[1] as usize;
graph[u].push(v);
indegree[v] += 1;
}
let mut queue = VecDeque::new();
for i in 0..n {
if indegree[i] == 0 {
queue.push_back(i);
}
}
let mut color_count = vec![vec![0; 26]; n];
let mut visited = 0;
let colors = colors.as_bytes();
let mut max_color_value = 0;
while let Some(node) = queue.pop_front() {
visited += 1;
let color_index = (colors[node] - b'a') as usize;
color_count[node][color_index] += 1;
max_color_value = max_color_value.max(color_count[node][color_index]);
for &neigh in &graph[node] {
for i in 0..26 {
color_count[neigh][i] = color_count[neigh][i].max(color_count[node][i]);
}
indegree[neigh] -= 1;
if indegree[neigh] == 0 {
queue.push_back(neigh);
}
}
}
if visited != n as i32 {
return -1;
}
max_color_value
}
}