Easy Explained in Java, C++ and Python
Intuition\n1. Go through the line of people one by one. Each person buys one ticket at a time. Keep count of how many tickets are sold until the kth person has 
# Intuition\n1. Go through the line of people one by one. Each person buys one ticket at a time. Keep count of how many tickets are sold until the kth person has bought all the tickets they want.\n2. We will use Queue data structure for this.\n3. Point to be noted, Remember that those who have no more tickets to buy will leave the line, means we won\'t add them into the queue again.\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. **Create a Queue**: The function `timeRequiredToBuy` initializes a queue (FIFO - First In First Out) using `LinkedList`. This queue will hold indices of people waiting in line.\n\n2. **Initialize Time Counter**: A variable `time` is initialized to keep track of the time taken to sell all tickets.\n\n3. **Fill Queue**: The code loops through the `tickets` array and adds the indices of people to the queue. Each index represents a person in line.\n\n4. **Ticket Selling Loop**: The main part of the code is a while loop that runs as long as the queue is not empty. Inside this loop:\n   - It dequeues an index from the queue, indicating the current person being served.\n   - Decrements the number of tickets the current person wants to buy.\n   - Increments the `time` counter to reflect the time taken.\n   - Checks if the current person (`index`) is the same as `k` (the target person we\'re interested in), and if they have bought all their tickets. If so, the function returns the `time` taken.\n   - If the current person still wants to buy more tickets (tickets[index] > 0), it enqueues the same index back into the queue, indicating that the person is still in line to buy more tickets.\n\n5. **Return Time**: If the loop completes without finding the kth person or the queue becomes empty, it returns the total `time` taken.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$ Since we are using the queue data Structure.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        Queue<Integer> queue = new LinkedList<>();\n        int time = 0;\n\n        for(int i=0; i<tickets.length; i++) {\n            queue.add(i);\n        }\n\n        while(!queue.isEmpty()) {\n            int index = queue.poll();\n            tickets[index]--;\n            time++;\n\n            if(tickets[index]==0 && index==k) {\n                return time;\n            }\n            if(tickets[index] > 0) {\n                queue.add(index);\n            }\n        }\n        return time;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        queue<int> queue;\n        int time = 0;\n\n        for(int i = 0; i < tickets.size(); i++) {\n            queue.push(i);\n        }\n\n        while(!queue.empty()) {\n            int index = queue.front();\n            queue.pop();\n            tickets[index]--;\n            time++;\n\n            if(tickets[index] == 0 && index == k) {\n                return time;\n            }\n            if(tickets[index] > 0) {\n                queue.push(index);\n            }\n        }\n        return time;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:\n        queue = deque()\n        time = 0\n\n        for i in range(len(tickets)):\n            queue.append(i)\n\n        while queue:\n            index = queue.popleft()\n            tickets[index] -= 1\n            time += 1\n\n            if tickets[index] == 0 and index == k:\n                return time\n            if tickets[index] > 0:\n                queue.append(index)\n\n        return time\n```\n
['Queue', 'Python', 'C++', 'Java', 'Python3']