Class Solution
java.lang.Object
g2001_2100.s2073_time_needed_to_buy_tickets.Solution
2073 - Time Needed to Buy Tickets.<p>Easy</p>
<p>There are <code>n</code> people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the <strong>front</strong> of the line and the <code>(n - 1)<sup>th</sup></code> person is at the <strong>back</strong> of the line.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>tickets</code> of length <code>n</code> where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is <code>tickets[i]</code>.</p>
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong> ) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave</strong> the line.</p>
<p>Return <em>the <strong>time taken</strong> for the person at position</em> <code>k</code><em><strong>(0-indexed)</strong> to finish buying tickets</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> tickets = [2,3,2], k = 2</p>
<p><strong>Output:</strong> 6</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>
<p>In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].</p>
</li>
<li>
<p>In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].</p>
</li>
</ul>
<p>The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> tickets = [5,1,1,1], k = 0</p>
<p><strong>Output:</strong> 8</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>
<p>In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].</p>
</li>
<li>
<p>In the next 4 passes, only the person in position 0 is buying tickets.</p>
</li>
</ul>
<p>The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == tickets.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= tickets[i] <= 100</code></li>
<li><code>0 <= k < n</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
timeRequiredToBuy
public int timeRequiredToBuy(int[] tickets, int k)
-