Day 56 || O(n) time and O(1) space || Easiest Beginner Friendly Sol
Intuition of this Problem:\nThe intuition behind the code is to keep track of the minimum stock value seen so far while traversing the array from left to right.
# Intuition of this Problem:\nThe intuition behind the code is to keep track of the minimum stock value seen so far while traversing the array from left to right. At each step, if the current stock value is greater than the minimum value seen so far, we calculate the profit that can be earned by selling the stock at the current value and buying at the minimum value seen so far, and update the maximum profit seen so far accordingly.\n\nBy keeping track of the minimum stock value and the maximum profit seen so far, we can solve the problem in a single pass over the array, without needing to consider all possible pairs of buy-sell transactions. This makes the code efficient and easy to implement.\n<!-- Describe your first thoughts on how to solve this problem. -->\n**NOTE - PLEASE READ APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Approach for this Problem:\n1. Initialize a variable n to the size of the input vector prices.\n2. Initialize variables maximumProfit and minStockVal to 0 and INT_MAX respectively.\n3. Initialize a loop variable i to 0.\n4. While i is less than n, do the following:\n    - a. Set minStockVal to the minimum value between minStockVal and the value of prices at index i.\n    - b. If minStockVal is less than or equal to the value of prices at index i, set maximumProfit to the maximum value between maximumProfit and the difference between prices at index i and minStockVal.\n    - c. Increment i by 1.\n1. Return maximumProfit.\n<!-- Describe your approach to solving the problem. -->\n\n# Humble Request:\n- If my solution is helpful to you then please **UPVOTE** my solution, your **UPVOTE** motivates me to post such kind of solution.\n- Please let me know in comments if there is need to do any improvement in my approach, code....anything.\n- **Let\'s connect on** https://www.linkedin.com/in/abhinash-singh-1b851b188\n\n![57jfh9.jpg](https://assets.leetcode.com/users/images/c2826b72-fb1c-464c-9f95-d9e578abcaf3_1674104075.4732099.jpeg)\n\n# Code:\n```C++ []\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        int n = prices.size();\n        int maximumProfit = 0, minStockVal = INT_MAX;\n        int i = 0;\n        while (i < n) {\n            minStockVal = min(minStockVal, prices[i]);\n            // whenever the price of current stock is greater then then the stock value which we bought then only we will sell the stock \n            if (prices[i] >= minStockVal)\n                maximumProfit = max(maximumProfit, prices[i] - minStockVal);\n            i++;\n        }\n        return maximumProfit;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int maxProfit(int[] prices) {\n        int n = prices.length;\n        int maximumProfit = 0, minStockVal = Integer.MAX_VALUE;\n        int i = 0;\n        while (i < n) {\n            minStockVal = Math.min(minStockVal, prices[i]);\n            // whenever the price of current stock is greater then then the stock value which we bought then only we will sell the stock \n            if (prices[i] >= minStockVal)\n                maximumProfit = Math.max(maximumProfit, prices[i] - minStockVal);\n            i++;\n        }\n        return maximumProfit;\n    }\n}\n\n```\n```Python []\nclass Solution:\n    def maxProfit(self, prices: List[int]) -> int:\n        n = len(prices)\n        maximumProfit, minStockVal = 0, float(\'inf\')\n        i = 0\n        while i < n:\n            minStockVal = min(minStockVal, prices[i])\n            if prices[i] >= minStockVal:\n                maximumProfit = max(maximumProfit, prices[i] - minStockVal)\n            i += 1\n        return maximumProfit\n\n```\n\n# Time Complexity and Space Complexity:\n- Time complexity: **O(n)**, where n is the size of the input vector prices.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(1)**, as we are only using constant extra space for variables.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->
['Array', 'C++', 'Java', 'Python3']