From cd06c877e21082eec07cbc51c2c8b063e6cf5c23 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 29 Sep 2024 14:18:47 +0800 Subject: [PATCH] 20240929 finished. --- src/problem/mod.rs | 3 +- .../p2073_time_needed_to_buy_tickets.rs | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/problem/p2073_time_needed_to_buy_tickets.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index ff08760..83f2c7f 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -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; \ No newline at end of file +mod p2286_booking_concert_tickets_in_groups; +mod p2073_time_needed_to_buy_tickets; \ No newline at end of file diff --git a/src/problem/p2073_time_needed_to_buy_tickets.rs b/src/problem/p2073_time_needed_to_buy_tickets.rs new file mode 100644 index 0000000..2d056a9 --- /dev/null +++ b/src/problem/p2073_time_needed_to_buy_tickets.rs @@ -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, 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)); + } +}