🟢Beats 100.00% of users with Java, Easy Solution with Explanation
Keep growing \uD83D\uDE0A\n\n\n---\n\n# Complexity\n\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n---\n\n# Problem\n\n## Real-world scenario\n\nImagi
##### Keep growing \uD83D\uDE0A\n![image](https://assets.leetcode.com/users/images/5f29d34c-ef28-44b3-8203-6c3605111c9d_1712622012.4852026.png)\n\n---\n\n# Complexity\n\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n---\n\n# Problem\n\n## Real-world scenario\n\nImagine there\'s a long queue at a ticket counter for a popular concert. The queue consists of n people, where the person at the front of the line (0th person) is the first to buy tickets, and the person at the back of the line (n - 1) is the last.\n\nEach person in the queue has a specific number of tickets they want to buy, indicated by the array tickets of length n, where tickets[i] represents the number of tickets the ith person wants to buy.\n\nAt this ticket counter, each person takes exactly 1 second to purchase a single ticket. After purchasing a ticket, the person goes to the end of the line instantaneously to buy more tickets if needed. If a person has bought all the tickets they wanted, they will leave the queue.\n\nYour task is to determine how much time it will take for the person at position k (0-indexed) in the queue to finish buying all their tickets.\n\nSo, in this scenario, you\'re essentially calculating the time it takes for a specific person in the queue to complete purchasing all the tickets they want.\n\n---\n\n# Approach Explanation\n\n### Calculating Time Required to Buy Tickets\n- The function `timeRequiredToBuy` takes in an array `ticketsArray` representing the number of tickets available at each position, and an integer `position` representing the initial position of the person.\n- It aims to calculate the total time required for a person starting from the given position to buy tickets at each position based on the availability of tickets.\n- The function initializes variables `numberOfPeople`, `currentIndex`, and `timeTaken`.\n  - `numberOfPeople` stores the total number of positions in the array.\n  - `currentIndex` starts at the given `position`.\n  - `timeTaken` stores the total time taken to buy tickets.\n- The function uses a while loop to iterate from the given position towards the start of the array.\n  - In each iteration, it adds the minimum of the number of available tickets at the current position and the original position (where the person started) to the `timeTaken`.\n  - This simulates the process of buying tickets while moving towards the start of the array.\n- After completing the loop towards the start, the function adjusts the ticket count at the original position by decrementing it by 1.\n- Then, it uses another while loop to iterate from the position towards the end of the array.\n  - Similar to the previous loop, it adds the minimum of the number of available tickets at the current position and the original position to the `timeTaken`.\n  - This simulates the process of buying tickets while moving towards the end of the array.\n- Finally, the function returns the `timeTaken`, representing the total time required to buy tickets from the given position.\n\n### Complexity Analysis\n- Time Complexity: The algorithm runs in O(n) time, where n is the length of the `ticketsArray`. This is because it iterates through the array twice.\n- Space Complexity: The algorithm uses O(1) additional space, as it only requires a few extra variables regardless of the size of the input array.\n\n---\n\n# Pseudocode\n\n```Javascript []\nStart\nInitialize \'numberOfPeople\' to the length of \'ticketsArray\'\nInitialize \'currentIndex\' to \'position\'\nInitialize \'timeTaken\' to 0\n\nWhile \'currentIndex\' is greater than or equal to 0\n    Increment \'timeTaken\' by the minimum of \'ticketsArray[position]\' and \'ticketsArray[currentIndex]\'\n    Decrement \'currentIndex\' by 1\nEnd\n\nSet \'currentIndex\' to \'position + 1\'\nDecrement the value at index \'position\' in \'ticketsArray\'\n\nWhile \'currentIndex\' is less than \'numberOfPeople\'\n    Increment \'timeTaken\' by the minimum of \'ticketsArray[position]\' and \'ticketsArray[currentIndex]\'\n    Increment \'currentIndex\' by 1\nEnd\n\nReturn \'timeTaken\'\nEnd\n```\n\n# Code\n\n```java []\nclass Solution {\n    public int timeRequiredToBuy(int[] ticketsArray, int position) {\n        int numberOfPeople = ticketsArray.length;\n        int currentIndex = position;\n        int timeTaken = 0;\n        \n        while (currentIndex >= 0) {\n            timeTaken += Math.min(ticketsArray[position], ticketsArray[currentIndex]);\n            --currentIndex;\n        }\n        \n        currentIndex = position + 1;\n        --ticketsArray[position];\n        \n        while (currentIndex < numberOfPeople) {\n            timeTaken += Math.min(ticketsArray[position], ticketsArray[currentIndex]);\n            ++currentIndex;\n        }\n        \n        return timeTaken;\n    }\n}\n```\n----\n> ![VOTE](https://assets.leetcode.com/users/images/8bc25f2b-9256-46ad-a27c-2746a7c529bc_1712670904.6992257.gif)\n# *If you have any question, feel free to ask. If you found my solution helpful, Please UPVOTE !* \uD83D\uDE0A
['Array', 'Queue', 'Simulation', 'Java']