Simulation And Optimisation | Java | C++
Intuition, approach, and complexity dicussed in video solution in detail\nhttps://youtu.be/UJ8fLOavT7k\n# Code\nApproach 1: Java\n\njava\nclass Solution {\n    
# Intuition, approach, and complexity dicussed in video solution in detail\nhttps://youtu.be/UJ8fLOavT7k\n# Code\nApproach 1: Java\n\n```java\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int size = tickets.length;\n        int timePassed = 0, indx = 0;\n        while (tickets[k] > 0) {\n            if (tickets[indx] > 0) {\n               tickets[indx]--;\n               timePassed++;\n            }\n            indx = (indx + 1) % size;\n        }\n        return timePassed;\n    }\n}\n```\n\nApproach 1: C++\n\n```cpp\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int size = tickets.size();\n        int timePassed = 0, indx = 0;\n        while (tickets[k] > 0) {\n            if (tickets[indx] > 0) {\n                tickets[indx]--;\n                timePassed++;\n            }\n            indx = (indx + 1) % size;\n        }\n        return timePassed;\n    }\n};\n```\n\nApproach 2: Java\n\n```java\nclass Solution {\n    public int timeRequiredToBuy(int[] tickets, int k) {\n        int size = tickets.length;\n        int timePassed = 0;\n        for (int indx = 0; indx < size; indx++) {\n            int timeContri = 0;\n            if (indx <= k) {\n                timeContri = Math.min(tickets[indx], tickets[k]);\n            } else {\n                timeContri = Math.min(tickets[indx], tickets[k] - 1);\n            }\n            timePassed += timeContri;\n        }\n        return timePassed;\n    }\n}\n```\n\nApproach 2: C++\n\n```cpp\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& tickets, int k) {\n        int size = tickets.size();\n        int timePassed = 0;\n        for (int indx = 0; indx < size; indx++) {\n            int timeContri = 0;\n            if (indx <= k) {\n                timeContri = min(tickets[indx], tickets[k]);\n            } else {\n                timeContri = min(tickets[indx], tickets[k] - 1);\n            }\n            timePassed += timeContri;\n        }\n        return timePassed;\n    }\n};\n```\n\nFeel free to copy and paste these headings into your text editor.
['Array', 'Queue', 'Simulation', 'C++', 'Java']