Loading

LeetCode problem#2073

Time Needed to Buy Tickets

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

18 lines
1class Solution {
2    public int timeRequiredToBuy(int[] tickets, int k) {
3        int i = 0;
4        int count = 0;
5        while (tickets[k] > 0) {
6            if (tickets[i] > 0) {
7                tickets[i]--;
8                count++;
9            };
10            if (i + 1 >= tickets.length) {
11                i = 0;
12            } else {
13                i++;
14            }
15        }
16        return count;
17    }
18}

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.