C++ One Pass
See my latest update in repo LeetCode\n## Solution 1. Brute Force Simulation\n\ncpp\n// OJ: https://leetcode.com/problems/time-needed-to-buy-tickets/\n// Author
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n## Solution 1. Brute Force Simulation\n\n```cpp\n// OJ: https://leetcode.com/problems/time-needed-to-buy-tickets/\n// Author: github.com/lzl124631x\n// Time: O(SUM(A))\n// Space: O(1)\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& A, int k) {\n        int step = 0;\n        while (true) {\n            for (int i = 0; i < A.size(); ++i) {\n                if (A[i] == 0) continue;\n                A[i]--;\n                ++step;\n                if (A[k] == 0) return step;\n            }\n        }\n    }\n};\n```\n\n## Solution 2. One Pass\n\nFor `i <= k`, `A[i]` contributes `min(A[k], A[i])` steps.\n\nFor `i > k`, `A[i]` contributes `min(A[k] - 1, A[i])` steps.\n\n```cpp\n// OJ: https://leetcode.com/problems/time-needed-to-buy-tickets/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1)\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& A, int k) {\n        int ans = 0;\n        for (int i = 0; i < A.size(); ++i) {\n            ans += min(A[k] - (i > k), A[i]);\n        }\n        return ans;\n    }\n};\n```
[]