loop with sum  min(buy, tickets[i])->1 liner||0ms Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nSuppose $x=tickets[k]$. When $i\leq k$ person i has to buy at most x tickets; when $i>k
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSuppose $x=tickets[k]$. When $i\\leq k$ person i has to buy at most x tickets; when $i>k$ peson i has to buy at most x-1 tickets.\n- The simulation using queue will take O(nx) time.\n- All codes here provided are O(n)-time solutions with constant space.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n[Please turn on English subtitles if necessary]\n[https://youtu.be/ek9a0iZCsIg?si=TupccfCCHpIe8Z5K](https://youtu.be/ek9a0iZCsIg?si=TupccfCCHpIe8Z5K)\nThe answer is \n$time=\\sum_i \\min(buy, tikets[i])$ where `buy=(i>k)?x-1:x`\n2nd approach uses a sized-2 array to drop off the if-branch. 3x python codes, one of them is 1-liner.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n# Code||C++ 0ms Beats 100%\n```\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int n=tickets.size();\n        int x=tickets[k];\n        int time=0;\n        for(int i=0; i<n; i++){\n            int buy=(i>k)?x-1:x;\n            time+=min(buy, tickets[i]);\n        }\n        return time;           \n    }\n};\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n# Python code in film\n```\nclass Solution:\n    def timeRequiredToBuy(self, t: List[int], k: int) -> int:\n        #loop with sum min(buy, tickets[i])\n        n=len(t)\n        x=t[k]\n        time=0\n        for i, y in enumerate(t):\n            buy=x\n            if i>k: buy=x-1\n            time+=min(buy, y)\n        return time\n        \n```\nAfter solving this question obtained the following Badge!\n![2024_100days.png](https://assets.leetcode.com/users/images/437aedb2-b3b9-437c-b854-0a2f7955ae36_1712621855.0650465.png)\n# C/C++ without if-branches||0ms beats 100%\n```C++ []\n#pragma GCC optimize("O3", "unroll-loops")\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int n=tickets.size();\n        int x=tickets[k];\n        int time=0;\n        int buy[2]={x, x-1};\n        for(int i=0; i<n; i++){\n            time+=min(buy[i>k], tickets[i]);\n        }\n        return time;           \n    }\n};\n\n```\n```C []\n#pragma GCC optimize("O3", "unroll-loops")\nint timeRequiredToBuy(int* tickets, int n, int k) {\n    int x=tickets[k];\n    int time=0;\n    int buy[2]={x, x-1};\n    for(register int i=0; i<n; i++){\n        int b=buy[i>k], y=tickets[i];\n        time+=(b<y)?b:y;\n    }\n    return time;\n}\n```\n\n# 2nd Python using sum function\n```\nclass Solution:\n    def timeRequiredToBuy(self, t: List[int], k: int) -> int:\n        n=len(t)\n        x=t[k]\n        time=sum(min(y,x) for y in t[:k+1])\n        time+=sum(min(y, x-1) for y in t[k+1:])\n        return time\n        \n```\n# Python 1-liner\n```\nclass Solution:\n    def timeRequiredToBuy(self, t: List[int], k: int) -> int:\n        return sum(min(y,x:=t[k]) for y in t[:k+1])+sum(min(y, x-1) for y in t[k+1:])\n        \n```\n
['Array', 'C', 'C++', 'Python3']