✅ 🔥 0 ms Runtime Beats 100% User 🔥|| Step By Steps Solution ✅ ||  Easy to Understand  ✅🔥 ||
\u2705  IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END \u2705 :\n\n### Intuition :\nWe need to find the maximum profit from buying and selling on two diffe
\u2705  IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END \u2705 :\n\n### Intuition :\nWe need to find the maximum profit from buying and selling on two different days. To maximize profit, we want to buy at the lowest price seen so far and sell at the highest possible price after buying. This can be done by tracking the minimum price as we traverse the list and calculating the potential profit each day by selling at the current price.\n\n### Approach :\n1. Initialize two variables:\n   - `min_price` to keep track of the lowest price encountered so far.\n   - `max_profit` to keep track of the maximum profit achieved so far.\n2. Traverse through the `prices` list:\n   - Update `min_price` if the current price is lower.\n   - Calculate profit by subtracting `min_price` from the current price.\n   - Update `max_profit` if the calculated profit is greater.\n3. Return `max_profit` as the result.\n\n### Solving Steps :\n1. Set `min_price` to a high value (or maximum integer value).\n2. Initialize `max_profit` to 0.\n3. Loop through each price in `prices`:\n   - Update `min_price` if the current price is lower than `min_price`.\n   - Calculate the profit as `price - min_price`.\n   - Update `max_profit` if the calculated profit is greater than `max_profit`.\n4. After the loop, `max_profit` contains the maximum possible profit. Return `max_profit`.\n\n### Code : \n\n#### Python 3 :\n```python\nfrom typing import List\n\nclass Solution:\n    def maxProfit(self, prices: List[int]) -> int:\n        min_price = float(\'inf\')\n        max_profit = 0\n        for price in prices:\n            min_price = min(min_price, price)\n            max_profit = max(max_profit, price - min_price)\n        return max_profit\n\n```\n\n#### Java :\n```java \npublic class Solution {\n    public int maxProfit(int[] prices) {\n        int min_price = Integer.MAX_VALUE;\n        int max_profit = 0;\n        for (int price : prices) {\n            min_price = Math.min(min_price, price);\n            max_profit = Math.max(max_profit, price - min_price);\n        }\n        return max_profit;\n    }\n}\n```\n\n#### C++ :\n```cpp\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        int min_price = INT_MAX;\n        int max_profit = 0;\n        for (int price : prices) {\n            min_price = min(min_price, price);\n            max_profit = max(max_profit, price - min_price);\n        }\n        return max_profit;\n    }\n};\n```\n![WhatsApp Image 2024-10-25 at 11.38.29.jpeg](https://assets.leetcode.com/users/images/d57012f0-bc6e-4b29-bee5-934f48730fb6_1729910773.136187.jpeg)\n
['Array', 'Dynamic Programming', 'C++', 'Java', 'Python3']