20240929 finished.
This commit is contained in:
parent
3bfd62a6a3
commit
cd06c877e2
|
@ -249,3 +249,4 @@ mod p2306_naming_a_company;
|
||||||
mod p2535_difference_between_element_sum_and_digit_sum_of_an_array;
|
mod p2535_difference_between_element_sum_and_digit_sum_of_an_array;
|
||||||
mod p2516_take_k_of_each_character_from_left_and_right;
|
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;
|
54
src/problem/p2073_time_needed_to_buy_tickets.rs
Normal file
54
src/problem/p2073_time_needed_to_buy_tickets.rs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user