From 5b08535a48f389742aaa9e6f0f11c47318e0cba5 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 9 Apr 2024 22:43:37 +0200 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB2073.=20Time=20Needed=20to=20?= =?UTF-8?q?Buy=20Tickets=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/time-needed-to-buy-tickets.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 rs/time-needed-to-buy-tickets.rs diff --git a/rs/time-needed-to-buy-tickets.rs b/rs/time-needed-to-buy-tickets.rs new file mode 100644 index 0000000..f3f2352 --- /dev/null +++ b/rs/time-needed-to-buy-tickets.rs @@ -0,0 +1,16 @@ +use std::cmp; + +impl Solution { + pub fn time_required_to_buy(tickets: Vec, k: i32) -> i32 { + let threshold = tickets[k as usize]; + + let from_left: i32 = (0..=k as usize) + .map(|i| cmp::min(tickets[i], threshold)) + .sum(); + let from_right: i32 = (k as usize + 1..tickets.len()) + .map(|i| cmp::min(tickets[i], threshold - 1)) + .sum(); + + from_left + from_right + } +}