DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Time Needed to Buy Tickets

/**
 * @param {number[]} tickets
 * @param {number} k
 * @return {number}
 */
var timeRequiredToBuy = function(tickets, k) {
    let time = 0;

    while (tickets[k] > 0) {
        for (let i = 0; i < tickets.length; i++) {
            if (tickets[k] === 0) break;
            if (tickets[i] <= 0) continue;
            tickets[i]--;
            time++;
        }
    }

    return time;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)