We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接: https://leetcode.cn/problems/time-needed-to-buy-tickets/
解题思路:
tickets[k] > 0
tickets[k]
tickets
1
/** * @param {number[]} tickets * @param {number} k * @return {number} */ var timeRequiredToBuy = function(tickets, k) { let result = 0 // 存储结果 // 不断循环直到tickets[k]为0,表示购买到所需数量的票 while (tickets[k]) { // 每次循环tickets for (let i = 0; i < tickets.length; i++) { // 如果当前是第k个人,他只剩一次购票机会,购票之后就退出循环 if (i === k && tickets[k] === 1) { // 购票时间加1 result++ // 购票数量减1 tickets[k]-- break } // 如果tickets[i]大于0,表示还可购票 if (tickets[i]) { // 购票时间加1 result++ // 购票数量减1 tickets[i]-- } } } return result };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:
https://leetcode.cn/problems/time-needed-to-buy-tickets/
解题思路:
tickets[k] > 0
,就可以不断排队购票tickets[k]
为0停止tickets
中的每个人购票次数减1,同时将购票时间加1tickets[k]
为1
,则进行最后一次购票,并返回结果The text was updated successfully, but these errors were encountered: