题解 | 队列消数

队列消数

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

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param tickets int整型vector 
     * @param k int整型 
     * @return int整型
     */
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        // write code here
        queue<int> qu;
        int ti=0;
        for(int i=0;i<tickets.size();i++){
        	qu.push(i);
		}
		while(true){
			int x=qu.front();
			ti++;
			if(tickets[x]>1){
				tickets[x]--;
				qu.pop();qu.push(x);
			}else if(tickets[x]<=1&&k==x){
				return ti;
			}else{
				qu.pop();
			}
		}
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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