rs: add «2073. Time Needed to Buy Tickets»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
9baf18d1a6
commit
5b08535a48
1 changed files with 16 additions and 0 deletions
16
rs/time-needed-to-buy-tickets.rs
Normal file
16
rs/time-needed-to-buy-tickets.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
use std::cmp;
|
||||
|
||||
impl Solution {
|
||||
pub fn time_required_to_buy(tickets: Vec<i32>, 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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue