✅ Beats 100% | How to think about approch | O(n) time✅
In this i will explain what my thought process was and how i arrived to the solution\n# Intuition\n1. Lets start with the easiest case if only one person in que
In this i will explain what my thought process was and how i arrived to the solution\n# Intuition\n1. Lets start with the easiest case if only one person in queue then answer is tickets[0].\n2. No what if all people are stading infront lets take case where k = tickets.size()-1 i.e., standing int last of queue\n            -You can solve this manually once on paper and realize that answer will be sum of min(tickets person in front wants, tickets K wants). \n        -Another way of thinking about this is it number of times people in front k will visit is either tickets[k] times since if the value is more than that also k will be gone from the queue and if the value is less than tickets[k] then they will be gone from the queue and visit again\n3. Now what about people behind k. This is simple everyone behind k will come infront of him once k tickets his first tickets so then applying the logic applied before it become sum of min(tickets person in front wants, tickets K wants - 1).\n\n# Approach\ncode is pretty easy so wont explain that\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& arr, int k) {\n        int ans = 0;\n        for(int i = 0; i < k + 1; i++)\n            ans += min(arr[i], arr[k]);\n        for(int i = k + 1; i < arr.size(); i++)\n            ans += min(arr[i], arr[k]-1);\n            \n        return ans;\n    }\n};\n```
['C++']