mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
rs: add «621. Task Scheduler»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
5e51f2ba08
commit
5549cdd7d8
1 changed files with 21 additions and 0 deletions
21
rs/task-scheduler.rs
Normal file
21
rs/task-scheduler.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use std::cmp;
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
pub fn least_interval(tasks: Vec<char>, n: i32) -> i32 {
|
||||
let mut freqs: HashMap<char, i32> = HashMap::new();
|
||||
let mut found_max = 0;
|
||||
|
||||
for c in &tasks {
|
||||
let mut counter = freqs.entry(*c).or_insert(0);
|
||||
|
||||
*counter += 1;
|
||||
found_max = cmp::max(found_max, *counter);
|
||||
}
|
||||
|
||||
let mut time = (1 + n) * (found_max - 1);
|
||||
time += freqs.values().filter(|&&c| c == found_max).count() as i32;
|
||||
|
||||
cmp::max(tasks.len().try_into().unwrap(), time)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue