✅C++ solution || Simple || Optimized Solution || TC -> O(n)
Explanation: Optimized Solution || T.C -> O(n)\n\n Take maxProf to store maximum profit.\n Take minPrice to store the minimal price to the left and initialize i
**Explanation: Optimized Solution || T.C -> O(n)**\n\n* Take `maxProf` to store maximum profit.\n* Take `minPrice` to store the minimal price to the left and initialize it with `INT_MAX` or `1e9`.\n* Now linearly traverse from left to right and update the `minPrice` at every stage and after that try to sell it where profit will be price at that instance minus minimal price.\n* Then we compare the current profit with the maximum profit and update the `maxProf`.\n* After the traversal is over, we return the `maxProf`.\n\n```\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        int maxProf = 0;\n        int minPrice = INT_MAX;\n        \n        for(int i = 0; i<prices.size(); i++)\n        {\n            minPrice = min(minPrice,prices[i]);\n            maxProf = max(maxProf, prices[i] - minPrice);\n        }\n        \n        return maxProf;\n    }\n};\n```\n\n**Please upvote if you find the solution useful, means a lot.**\n\n\n\n
['C', 'C++']