From 82752746749319995da84efb8b4b1ede9c76e521 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 28 Jul 2023 00:17:18 +0200 Subject: [PATCH] =?UTF-8?q?problem(rs):=20add=20=E2=80=9C2141.=20Maximum?= =?UTF-8?q?=20Running=20Time=20of=20N=20Computers=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- .../rs/maximum-running-time-of-n-computers.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 problems/rs/maximum-running-time-of-n-computers.rs diff --git a/problems/rs/maximum-running-time-of-n-computers.rs b/problems/rs/maximum-running-time-of-n-computers.rs new file mode 100644 index 0000000..84c9971 --- /dev/null +++ b/problems/rs/maximum-running-time-of-n-computers.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn max_run_time(n: i32, mut batteries: Vec) -> i64 { + batteries.sort(); + + let mut total = batteries.iter().map(|x| *x as u64).sum::(); + let mut k: u64 = 0; + + while *batteries.last().unwrap() as u64 > total / (n as u64 - k) { + total -= *batteries.last().unwrap() as u64; + batteries.pop(); + + k += 1; + } + + (total / (n as u64 - k)) as i64 + } +} +