Beats 100% 🔥 || C++ Solution with clean explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n- The problem essentially boils down to simulating the process of people buying tickets
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem essentially boils down to simulating the process of people buying tickets in a queue. We need to find the time taken for a specific person, indexed by k, to finish buying their tickets.\n\n- The key insight is that if a person is behind the person at position k, they will have to wait for the person at position k to finish buying their tickets before they can start buying theirs. Additionally, if a person is ahead of the person at position k, they will continue buying tickets until they reach the same number of tickets as the person at position k, and then they will buy one less ticket in each subsequent cycle.\n\n- With this insight, we can iterate through the line of people, keeping track of the time taken for each person to buy tickets based on their position relative to the person at position k. Finally, we sum up the time taken by each person to find the total time taken for the person at position k to finish buying tickets.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- To solve this problem efficiently, we can simulate the process of people buying tickets. We iterate through the line of people, keeping track of the number of tickets each person wants to buy and the time taken for each person to finish buying tickets.\n\n- We start by initializing a variable ans to 0, which will represent the total time taken. We also initialize a variable t to store the number of tickets the person at position k wants to buy. Then, we iterate through each person in the line:\n\n  - If the current person is before or at position k, we add the minimum of their ticket requirement and t to ans, as they have to buy tickets up to the point where the person at position k finishes.\n\n  - If the current person is after position k, we add the minimum of their ticket requirement and t-1 to ans, as they have to buy tickets up to the point where the person at position k finishes, and the person at position k buys one ticket less than their requirement.\n\n- Finally, we return the value of ans, which represents the time taken for the person at position k to finish buying tickets.\n\n# Complexity\n**Time complexity:**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- $$O(n)$$, where n is the number of people in the line. We iterate through the line of people once.\n\n**Space complexity:**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- $$O(1)$$ the program uses only a constant amount of extra space.\n\n\n![_9bb04da7-cab5-454b-87e7-d5340cdeb5f8.jpeg](https://assets.leetcode.com/users/images/ded73ab5-f160-4430-9cc9-93d79c276468_1712640663.7642262.jpeg)\n\n# Code\n```\n//Upvote karle bhai\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int ans=0,t=tickets[k],n=tickets.size();\n        for(int i=0;i<n;i++){\n            if(i<=k){\n                ans+=min(tickets[i],t);\n            }\n            else{\n                ans+=min(tickets[i],t-1);\n            }\n        }\n        return ans;\n    }\n};\n```
['Array', 'Queue', 'Simulation', 'C++']