题解 | 队列消数

队列消数

https://www.nowcoder.com/practice/48f6e451ff52440798067b77dc5ea95b

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param tickets int整型一维数组 
# @param k int整型 
# @return int整型
#
class Solution:
    def timeRequiredToBuy(self , tickets: List[int], k: int) -> int:
        # write code here
        queue = []
        n = len(tickets)
        for i in range(n):
            queue.append((i, tickets[i]))
        time = 0
        while queue:
            idx, t = queue.pop(0)
            time += 1            
            if t > 1:
                # 如果票大于1,减1后放回队尾
                queue.append((idx, t - 1))
            # 如果移除的是目标元素,结束循环
            if idx == k and t == 1:
                break
        return time

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务