From 5549cdd7d8521f62187ccc229d3d76dcece982a4 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 20 Mar 2024 00:16:00 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB621.=20Task=20Scheduler=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/task-scheduler.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rs/task-scheduler.rs diff --git a/rs/task-scheduler.rs b/rs/task-scheduler.rs new file mode 100644 index 0000000..11da439 --- /dev/null +++ b/rs/task-scheduler.rs @@ -0,0 +1,21 @@ +use std::cmp; +use std::collections::HashMap; + +impl Solution { + pub fn least_interval(tasks: Vec, n: i32) -> i32 { + let mut freqs: HashMap = 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) + } +}