2073. Time Needed to Buy Tickets Easy

@problem@discussion
#Array#Queue#Simulation



1/**
2 * [2073] Time Needed to Buy Tickets
3 *
4 * There are n people in a line queuing to buy tickets, where the 0^th person is at the front of the line and the (n - 1)^th person is at the back of the line.
5 * You are given a 0-indexed integer array tickets of length n where the number of tickets that the i^th person would like to buy is tickets[i].
6 * Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
7 * Return the time taken for the person at position k (0-indexed) to finish buying tickets.
8 *  
9 * Example 1:
10 * 
11 * Input: tickets = [2,3,2], k = 2
12 * Output: 6
13 * Explanation: 
14 * - In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
15 * - In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
16 * The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
17 * 
18 * Example 2:
19 * 
20 * Input: tickets = [5,1,1,1], k = 0
21 * Output: 8
22 * Explanation:
23 * - In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
24 * - In the next 4 passes, only the person in position 0 is buying tickets.
25 * The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	n == tickets.length
31 * 	1 <= n <= 100
32 * 	1 <= tickets[i] <= 100
33 * 	0 <= k < n
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/time-needed-to-buy-tickets/
39// discuss: https://leetcode.com/problems/time-needed-to-buy-tickets/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn time_required_to_buy(tickets: Vec<i32>, k: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_2073() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.