【Video】Keep minimum price solution
Intuition\nKeep minimum price.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/NG4ZDYD4a2o\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my chann
# Intuition\nKeep minimum price.\n\n---\n\n# Solution Video\n\nhttps://youtu.be/NG4ZDYD4a2o\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nSubscribers: 4,197\nThank you for your support!\n\n---\n\n# Approach\n\nSimply, when we find low price of stock, we should buy it so that we can get maximum profit in a later day.\n\n```\nInput: prices = [7,1,5,3,6,4]\n```\nInitialize\n```\nbuy_price = 7\nprofit = 0\n\n7 is the first price in input array\n```\nWe start from index `1`.\n\n```\n[7,1,5,3,6,4]\n   p\n\nbuy_price = 7\nprofit = 0\n```\nWe find `price 1`. Let\'s think about whether we should update `buy_price` or not.\n\nAs I told you, we should buy the stock when we find low price because there is possiblity that we can get maximum profit in a later day.\n\nCaculation of profit is very simple\n\n```\nprofit = current price - buy price\n```\nAt day 1, we find `price 1`, so we should buy it at day 1. Update `buy_price` with `price 1`\n```\nbuy_price = 1\n```\nThen calclate profit.\n```\nprofit = max(profit, current price - buy price)\n0 = max(0, 1 - 1)\n```\nNext, I\'ll speed up.\n```\n[7,1,5,3,6,4]\n     p\n\nbuy_price = 1\nprofit = 0\n```\nUpdate `buy_price`? \u2192 No, because previous `buy_price` is lower than current price.\n\nCalculate profit.\n```\nprofit = max(0, 5 - 1)\n= 4\n```\nNext,\n```\n[7,1,5,3,6,4]\n       p\n\nbuy_price = 1\nprofit = 4\n```\nUpdate `buy_price`? \u2192 No\nCalculate profit.\n```\nprofit = max(4, 3 - 1)\n= 4\n```\nNext,\n```\n[7,1,5,3,6,4]\n         p\n\nbuy_price = 1\nprofit = 4\n```\nUpdate `buy_price`? \u2192 No\nCalculate profit.\n```\nprofit = max(4, 6 - 1)\n= 5\n```\nNext,\n```\n[7,1,5,3,6,4]\n           p\n\nbuy_price = 1\nprofit = 5\n```\nUpdate `buy_price`? \u2192 No\nCalculate profit.\n```\nprofit = max(5, 4 - 1)\n= 5\n```\nFinish iteration.\n\n```\nreturn 5\n```\n\nEasy!\uD83D\uDE04\nLet\'s see solution codes and step by step algorithm!\n\n---\n\n\u2B50\uFE0F I recently created a video on how I\'ve been using LeetCode to learn.\n\nhttps://youtu.be/bU_dXCOWHls\n\n---\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n```python []\nclass Solution:\n    def maxProfit(self, prices: List[int]) -> int:\n        buy_price = prices[0]\n        profit = 0\n\n        for p in prices[1:]:\n            if buy_price > p:\n                buy_price = p\n            \n            profit = max(profit, p - buy_price)\n        \n        return profit\n```\n```javascript []\nvar maxProfit = function(prices) {\n    let buyPrice = prices[0];\n    let profit = 0;\n\n    for (let i = 1; i < prices.length; i++) {\n        if (buyPrice > prices[i]) {\n            buyPrice = prices[i];\n        }\n\n        profit = Math.max(profit, prices[i] - buyPrice);\n    }\n\n    return profit;    \n};\n```\n```java []\nclass Solution {\n    public int maxProfit(int[] prices) {\n        int buyPrice = prices[0];\n        int profit = 0;\n\n        for (int i = 1; i < prices.length; i++) {\n            if (buyPrice > prices[i]) {\n                buyPrice = prices[i];\n            }\n\n            profit = Math.max(profit, prices[i] - buyPrice);\n        }\n\n        return profit;        \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int maxProfit(vector<int>& prices) {\n        int buyPrice = prices[0];\n        int profit = 0;\n\n        for (int i = 1; i < prices.size(); i++) {\n            if (buyPrice > prices[i]) {\n                buyPrice = prices[i];\n            }\n\n            profit = max(profit, prices[i] - buyPrice);\n        }\n\n        return profit;        \n    }\n};\n```\n\n## Step by Step Algorithm\n\n1. **Initialize Variables**:\nInitialize `buy_price` to the first price in the list and `profit` to 0.\n\n```python\nbuy_price = prices[0]\nprofit = 0\n```\n\n2. **Iterate Over Prices**\nLoop through each price starting from the second price (`prices[1:]`).\n\n```python\nfor p in prices[1:]:\n```\n\n3. **Update Buy Price**\nIf the current price (`p`) is less than the previously set `buy_price`, update `buy_price` to the current price. This ensures that we buy at the lowest price possible.\n\n```python\nif buy_price > p:\n    buy_price = p\n```\n\n4. **Calculate Profit**\nCalculate the profit by subtracting the `buy_price` from the current price (`p`). Update `profit` to the maximum value between the current profit and the calculated profit. This ensures we keep track of the maximum profit throughout the iteration.\n\n```python\nprofit = max(profit, p - buy_price)\n```\n\n5. **Return Maximum Profit**\nAfter the loop, return the final value of `profit`, which represents the maximum profit achievable from buying and selling stocks.\n\n```python\nreturn profit\n```\n\n---\n\nThank you for reading my post.\nPlease upvote it and don\'t forget to subscribe to my channel!\n\n# \u2B50\uFE0F Subscribe URL\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\n# \u2B50\uFE0F Twitter\nhttps://twitter.com/CodingNinjaAZ\n\n# \u2B50\uFE0F My previous post and video\n#392 Is Subsequence\n\npost\nhttps://leetcode.com/problems/is-subsequence/solutions/4934555/video-two-pointer-solution/\n\nvideo\nhttps://youtu.be/2FYJxatZnxE\n
['Array', 'Math', 'Dynamic Programming', 'C++', 'Java', 'Python3', 'JavaScript']