Java easy solution beats 100%
Approach\n Describe your approach to solving the problem. \nThe idea of this solution is to think about the problem differently. Let\'s reformulate it in anothe
# Approach\n<!-- Describe your approach to solving the problem. -->\nThe idea of this solution is to think about the problem differently. Let\'s reformulate it in another way.\n- The persons who are queued in front of the selected person will at most buy the same ticket as the person in index `k`. If they should buy more tickets, person `k` will buy his tickets before that.\n- The persons behind person `k` will buy at most the `(number of tickets of person k) - 1`.\n- In our implementation we will implement it in a way that we pre-calculate how much tickets each person will buy without simulating the whole process.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \nWe iterate through the tickets array only once: $$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int value = tickets[k];\n        int seconds = 0;\n        for(int i = 0; i<tickets.length; i++) {\n            int max = (i<=k)? value: value-1;\n            seconds+= (tickets[i]>max)? max: tickets[i];\n        }\n        return seconds;\n    }\n}\n```
['Java']