20240929 finished.

This commit is contained in:
jackfiled 2024-09-29 14:18:47 +08:00
parent 3bfd62a6a3
commit cd06c877e2
2 changed files with 56 additions and 1 deletions

View File

@ -248,4 +248,5 @@ mod p2207_maximize_number_of_subsequences_in_a_string;
mod p2306_naming_a_company;
mod p2535_difference_between_element_sum_and_digit_sum_of_an_array;
mod p2516_take_k_of_each_character_from_left_and_right;
mod p2286_booking_concert_tickets_in_groups;
mod p2286_booking_concert_tickets_in_groups;
mod p2073_time_needed_to_buy_tickets;

View File

@ -0,0 +1,54 @@
/**
* [2073] Time Needed to Buy Tickets
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn time_required_to_buy(tickets: Vec<i32>, k: i32) -> i32 {
use std::collections::VecDeque;
let k = k as usize;
let mut queue = VecDeque::with_capacity(tickets.len());
for (i, &v) in tickets.iter().enumerate() {
queue.push_back(if i == k {
(v, true)
} else {
(v, false)
});
}
let mut second = 0;
loop {
let head = queue.pop_front().unwrap();
second += 1;
if head.0 == 1 {
// 卖完了
if head.1 {
break;
}
} else {
queue.push_back((head.0 - 1, head.1));
}
}
second
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2073() {
assert_eq!(6, Solution::time_required_to_buy(vec![2,3,2], 2));
assert_eq!(8, Solution::time_required_to_buy(vec![5,1,1,1], 0));
}
}