Skip to content

Commit

Permalink
Sync LeetCode submission - Parallel Courses III (rust)
Browse files Browse the repository at this point in the history
  • Loading branch information
hucancode committed Oct 18, 2023
1 parent 75363dd commit 79e645a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions leetcode/problems/parallel_courses_iii/solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::cmp::max;
impl Solution {
pub fn minimum_time(n: i32, relations: Vec<Vec<i32>>, time: Vec<i32>) -> i32 {
let n = n as usize;
let mut f = time.clone();
let mut in_degree = vec![0;n];
let mut adj = vec![Vec::new();n];
for rel in relations {
let u = rel[0] as usize - 1;
let v = rel[1] as usize - 1;
adj[u].push(v);
in_degree[v] += 1;
}
let mut q: Vec<usize> = in_degree.iter()
.enumerate()
.filter_map(|(u,&pu)| if pu == 0 {Some(u)} else {None})
.collect();
while let Some(u) = q.pop() {
for &v in adj[u].iter() {
in_degree[v] -= 1;
f[v] = max(f[v], f[u]+time[v]);
if(in_degree[v] == 0) {
q.push(v);
}
}
}
return f.into_iter().fold(0, |acc,x| max(acc,x));
}
}

0 comments on commit 79e645a

Please sign in to comment.