目前我觉得最容易理解,最便捷的使用队列来模拟的做法

核心思想是用队列来储存人的索引,遍历索引然后票数减一,如果正好到k时票数也买完了,输出时间

classSolution {

public:

    /**

     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可

     *

     *

     * @param tickets int整型vector

     * @param k int整型

     * @return int整型

     */

inttimeRequiredToBuy(vector<int>&ticketsintk)

    {

        // write code here

int count = 0;

        queue<int> preson;

        for (int i = 0;i < tickets.size(); i++)

        {

            preson.push(i);

        }

        while (!preson.empty())

        {

            int current = preson.front();

            preson.pop();

            count++;

            tickets[current]--;

            if (tickets[current] > 0)

            {

                preson.push(current);

            }

            else if (current == k)

            {

                return count;

            }

        }

        return count;

    }

};