✅97.44%🔥Easy Solution🔥With explanation🔥
Approach\n#### 1. Initialize a variable time to keep track of the total time taken.\n#### 2. While the person at position k hasn\'t bought all their tickets:\n-
# Approach\n#### 1. Initialize a variable `time` to keep track of the total time taken.\n#### 2. While the person at position k hasn\'t bought all their tickets:\n- Iterate through the queue and let each person buy a ticket.\n- Update the queue by decrementing the number of tickets for the person who bought a ticket.\n- Increment the time by 1 second for each pass.\n#### 3. Return the total time taken.\n\n\n# Complexity\n- ## Time complexity:\n#### The algorithm iterates through each person in the queue once, so the time complexity of the loop is $$O(n)$$, where n is the number of people in the queue.\n\n- ## Space complexity:\n#### The algorithm uses only a constant amount of extra space for variables such as `total`, `i`, and `x`.\n#### Therefore, the space complexity is $$O(1)$$.\n----\n# Code\n\n\n```python []\nclass Solution:\n    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:\n        total = 0\n\n        for i, x in enumerate(tickets):\n            if i <= k:\n                total += min(tickets[i], tickets[k])\n            else:\n                total += min(tickets[i], tickets[k] - 1)\n\n        return total\n```\n```C++ []\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int total = 0;\n\n        for (int i = 0; i < tickets.size(); ++i) {\n            if (i <= k) {\n                total += min(tickets[i], tickets[k]);\n            } else {\n                total += min(tickets[i], tickets[k] - 1);\n            }\n        }\n\n        return total;\n    }\n};\n```\n```java []\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int total = 0;\n\n        for (int i = 0; i < tickets.length; i++) {\n            if (i <= k) {\n                total += Math.min(tickets[i], tickets[k]);\n            } else {\n                total += Math.min(tickets[i], tickets[k] - 1);\n            }\n        }\n\n        return total;\n    }\n}\n\n```\n```javascript []\nvar timeRequiredToBuy = function(tickets, k) {\n    let total = 0;\n\n    for (let i = 0; i < tickets.length; i++) {\n        if (i <= k) {\n            total += Math.min(tickets[i], tickets[k]);\n        } else {\n            total += Math.min(tickets[i], tickets[k] - 1);\n        }\n    }\n\n    return total;\n};\n\n```\n```C# []\npublic class Solution {\n    public int TimeRequiredToBuy(int[] tickets, int k) {\n        int total = 0;\n\n        for (int i = 0; i < tickets.Length; i++) {\n            if (i <= k) {\n                total += Math.Min(tickets[i], tickets[k]);\n            } else {\n                total += Math.Min(tickets[i], tickets[k] - 1);\n            }\n        }\n\n        return total;\n    }\n}\n\n```\n---\n\n![Screenshot 2023-08-20 065922.png](https://assets.leetcode.com/users/images/0a81abf2-1b6e-40c8-b139-4eab27d6fce5_1712622014.1692593.png)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
['Array', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']