Time Needed to Buy Tickets - Time and Space Complexity Optimized
\n\n\n# Approach\n Describe your approach to solving the problem. \n1. Intialize a Variable \'ans\' for storing the time needed for buying the tickets\n\n2. Tra
\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1. Intialize a Variable \'ans\' for storing the time needed for buying the tickets**\n\n**2. Traverse through the entire array ans update the ans with the following conditions**\n     \n    if(tickets[i]>=tickets[k]){ans+=tickets[k];}\n\nAs we wont take more than tickets[k] rounds to buy tickets as the desired target is tickets[k]\n\nAlso if the k is in the middle of the array we won\'t require the people\'s turn after the tickets[k] in the kth round, so we add a condition as\n    \n    if(tickets[i]>=tickets[k]){ans+=tickets[k]; if(i>k) ans--;}\n\nby this we can find the optimal time needed\n\n**Example**\n\nLet\'s take the case [2,3,2,4] and k=1\n\nNow by our solution we traverse from 0 to tickets.length-1\n\nIn i=0 -> **tickets[i]=2** which is less than **tickets[k]=3** so the ans is incremented by **tickets[i]=2** so ans becomes **ans=2**\n\nIn i=1 -> **tickets[i]=3** which is equal to **tickets[k]=3** so the ans is incremented by **tickets[k]=3** so ans becomes **ans=5** since the i is not greater than k the ans is left unchanged\n\nIn i=2 -> **tickets[i]=2** which is less than **tickets[k]=3** so the ans is incremented by **tickets[i]=2** so ans becomes **ans=7**\n\nIn i=3 -> **tickets[i]=4** which is greater than **tickets[k]=3** so the ans is incremented by **tickets[k]=3** so ans becomes **ans=10** but the k is greater than i so the ans is decremented by 1\n\n    in the case of 2 3 2 4\n    1st round -> 1 2 1 3\n    2nd round -> 0 1 0 2\n    3rd round -> 0 0 | 0 1\n\nas the need of the greater elements after the index k is not needed in the last round so the number of greater elements is decremented to get the optimal time.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n\n```java []\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int ans=0;\n        for(int i=0;i<tickets.length;i++){\n            if(tickets[i]>=tickets[k]){ans+=tickets[k];if(i>k) ans--;}\n            else ans+=tickets[i];\n        }\n        return ans;\n    }\n}\n```\n\n```C++ []\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int ans=0;\n        for(int i=0;i<tickets.size();i++){\n            if (tickets[i]>=tickets[k]){ans+=tickets[k]; if(i>k) ans--;} \n            else ans += tickets[i];\n        }\n        return ans;\n    }\n};\n\n```\n```python []\nclass Solution:\n    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:\n        ans=0\n        for i in range(len(tickets)):\n            if tickets[i]>=tickets[k]:\n                ans+=tickets[k]\n                if i>k:\n                    ans-=1\n            else:\n                ans+=tickets[i]\n        return ans\n```\n
['Python', 'C++', 'Java', 'Python3']