2 interview Approaches with Video Solution ✅ || Brute Force -> Optimal (1 Pass) 🔥
Happy Ugadi, I wish you have wonderful year ahead \uD83D\uDE00\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nUnderstand the ques
*Happy Ugadi, I wish you have wonderful year ahead* \uD83D\uDE00\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUnderstand the question clearly and dry-run few examples then you can implement the solution\n\n***If you learned something from the solution. Please Upvote and subscribe to my youtube channel***\n\n# ***Easy Video Explanation***\n\nhttps://youtu.be/1Ri51bORarI\n    \n\n# Brute Force\n\n\n```C++ []\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& v, int k) {\n        int n=v.size();\n        int t=0;\n        while(true) {\n            for(int i=0;i<n;i++) {\n                if(v[i]>0) {\n                    v[i]=v[i]-1;\n                    t++;\n                }\n                if(i==k && v[i]==0)\n                    return t;\n            }\n        }\n        return 0;\n    }\n};\n```\n```python []\nclass Solution:\n    def timeRequiredToBuy(self, v: List[int], k: int) -> int:\n        n = len(v)\n        t = 0\n        while True:\n            for i in range(n):\n                if v[i] > 0:\n                    v[i] -= 1\n                    t += 1\n                if i == k and v[i] == 0:\n                    return t\n\n```\n```Java []\npublic class Solution {\n    public int timeRequiredToBuy(int[] v, int k) {\n        int n = v.length;\n        int t = 0;\n        while (true) {\n            for (int i = 0; i < n; i++) {\n                if (v[i] > 0) {\n                    v[i] = v[i] - 1;\n                    t++;\n                }\n                if (i == k && v[i] == 0)\n                    return t;\n            }\n        }\n    }\n}\n\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n * m)\n    n: size of array\n    m : max number within the array\n    \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n# Optimal Solution (1 pass)\n\n\n```C++ []\nclass Solution {\npublic:\n    int timeRequiredToBuy(vector<int>& v, int k) {\n        int n=v.size();\n        int value=v[k];\n        int t=0;\n        for(int i=0;i<n;i++) {\n            if(i<k) {\n                t=t+min(v[i],value);\n            } else if(i==k) {\n                t=t+value;\n            } else {\n                if(v[i]<value)\n                    t=t+v[i];\n                else\n                    t=t+value-1;\n            }\n        }\n        return t;\n    }\n};\n```\n```python []\nfrom typing import List\n\nclass Solution:\n    def timeRequiredToBuy(self, v: List[int], k: int) -> int:\n        n = len(v)\n        value = v[k]\n        t = 0\n        for i in range(n):\n            if i < k:\n                t += min(v[i], value)\n            elif i == k:\n                t += value\n            else:\n                if v[i] < value:\n                    t += v[i]\n                else:\n                    t += value - 1\n        return t\n\n```\n```Java []\nimport java.util.List;\n\npublic class Solution {\n    public int timeRequiredToBuy(List<Integer> v, int k) {\n        int n = v.size();\n        int value = v.get(k);\n        int t = 0;\n        for (int i = 0; i < n; i++) {\n            if (i < k) {\n                t += Math.min(v.get(i), value);\n            } else if (i == k) {\n                t += value;\n            } else {\n                if (v.get(i) < value)\n                    t += v.get(i);\n                else\n                    t += value - 1;\n            }\n        }\n        return t;\n    }\n}\n\n\n```\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(N)\n    \n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n\n# ***If you like the solution Please Upvote and subscribe to my youtube channel***\n\n*Thank you* \uD83D\uDE00
['Math', 'Queue', 'Python', 'C++', 'Java']