🔥 8 lines of code beat 100% users 🚀 fully explained with example ✅ || Easy to understand 👍
Intuition\n# Python, Python3 and C++ :\n1. Initialization :\n    1. Initialize a variable \'c\' to keep track of the total number of tickets bought.\n    2. Sta
# Intuition\n# Python, Python3 and C++ :\n1. **Initialization :**\n    1. Initialize a variable **\'c\'** to keep track of the total number of tickets bought.\n    2. Start an infinite loop **(while True)** It checks until the person at position k finishes buying his tickets.\n2. **if tickets[k] == 0:   break   :** If the person at position **\'k\'** has bought all his tickets, than exit the while loop.\n3. **for i in range(len(tickets)) :** Iterate through each person in the line.\n4. **if tickets[k] == 0:  break :** If the person at position k has bought all his tickets, than exit the for loop.\n**Note :** I have use **\'if tickets[k] == 0:  break\'** this condition 2 times cause after we get exit from the **for** loop than at the same time we have to take exit from **while infinte loop** otherwise we will get stuck in while loop so we exit both loop at the same time hence we get the value of **\'c\'**.\n5. **if tickets[i]>0 :** Here we check from the start of the array If the person has tickets left to buy and waits until his turn while other peoples in the queue buys the ticket. and we does not count that tickets values which becomes zero thats why we use condition **tickets[i]>0** and if this condition is true we decrease the value of **tickets[i]** by 1 that means 1 ticket is sold(Buys one ticket) and at the same time we increase the value of **\'c\'** by 1 it tells us the exact count aur time taken by the **\'kth\'** person for buying all his tickets.\n6. **return c :** Return the total time taken for the person at position **\'k\'** to finish buying tickets.\n\n**Lets see the example for better understanding :**\n**Example 1 : tickets = [2,3,2], k = 2**\nlets enter the while loop and check the first condition **if tickets[k]==0 :** and **tickets[2]=2** and its not zero so we enter the **for** loop and check the same condition **if tickets[k]==0** it helps to take exit from the for loop but this time this condition is not true so we go to next condition **if tickets[i]>0** and start decreasing the tickets[i] by 1 and increase the **\'c\'** by 1 so after first pass our array becomes **[1,2,1]** and **c=3** as we are inside the while loop so we again check the condition **if tickets[k]==0** means tickets[2]=1 so we enter the for loop again and check same condition and enter the next condition **if tickets[i]>0** now array becomes **[0,1,0]** and **c=6** as soon as the **kth** element becomes 0 we does not have to check further on other elements and our kth element becomes zero and **if tickets[k]==0:** condition becomes true so we take exit from the for loop and for taking exit from the while loop we check the same condition and take exit from the while loop and retrun the value of **c=6**\n\n**I hope you understand the code \uD83D\uDE4F and if you have any query let me know in the comment.**\n**Thanks you all for visiting here.**\n\n# Approach : Brute Force\n\n# Complexity\n- Time complexity: O(n * max(tickets))  n = number of tickets\n![Screenshot (12).png](https://assets.leetcode.com/users/images/de9f19a9-acb5-45fe-9cd6-27477c627900_1712640138.287326.png)\n\n- Space complexity: O(n)\n\n# Code\n``` Python3 []\nclass Solution:\n    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:\n        c=0\n        while True:\n            if tickets[k]==0:    break\n            for i in range(len(tickets)):\n                if tickets[k]==0:    break\n                if tickets[i]>0:\n                    tickets[i]-=1\n                    c+=1\n        return c\n```\n``` Python []\nclass Solution(object):\n    def timeRequiredToBuy(self, tickets, k):\n        """\n        :type tickets: List[int]\n        :type k: int\n        :rtype: int\n        """\n        c=0\n        while True:\n            if tickets[k]==0:    break\n            for i in range(len(tickets)):\n                if tickets[k]==0:    break\n                if tickets[i]>0:\n                    tickets[i]-=1\n                    c+=1\n        return c\n```\n``` C++ []\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int c=0;\n        while (true){\n            if(tickets[k]==0)   \n                break;\n            for(int i=0;i<tickets.size();i++){\n                if(tickets[k]==0)\n                    break;\n                if(tickets[i]>0){\n                    tickets[i]-=1;\n                    c+=1;\n                }\n            }\n        }\n        return c;\n    }\n};\n```
['Array', 'Queue', 'Simulation', 'Python', 'C++', 'Python3']