Created
April 9, 2024 19:14
-
-
Save tatsuyax25/8f44002e09b4752bfc197ac297f451d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} tickets | |
* @param {number} k | |
* @return {number} | |
*/ | |
var timeRequiredToBuy = function(tickets, k) { | |
// Initialize a counter to keep track of the total time | |
let count = 0; | |
// Loop through the tickets array in a circular manner | |
// The loop continues until the person at position k has no more tickets to buy | |
for (let i = 0; tickets[k] > 0; i = (i + 1) % tickets.length) { | |
// If the person at the current position has tickets to buy | |
if (tickets[i] > 0) { | |
// The person buys a ticket, decreasing their ticket count by 1 | |
tickets[i]--; | |
// Increase the total counter by 1 | |
count++; | |
} | |
} | |
// Return the total time taken for the person at position k to buy all their tickets | |
return count; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment