#include <queue> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param tickets int整型vector * @param k int整型 * @return int整型 */ int timeRequiredToBuy(vector<int>& tickets, int k) { // write code here queue<int> q; int time = 0; //初始化队列 for(int i = 0; i < tickets.size(); i++) { q.push(i); } while(!q.empty()) { int current = q.front();//当前处理的人 q.pop(); tickets[current]--;//买了一张票,总需要买的票数减少一张 time++;//每次耗时1秒 if(current == k && tickets[current] == 0) { return time; } //如果当前还有人需要买票,重新入队 if(tickets[current] > 0) { q.push(current); } } return time; } };