Python easy to understand solution with explanation : Tracking and Dynamic programming
Idea 1: Array \n The trick : track the minimum price and the maximum possible profit\n\t \tKeep track of the buy price --> keep comparing the current price with
### Idea 1: Array \n* The trick : track the minimum price and the maximum possible profit\n\t* \tKeep track of the buy price --> keep comparing the current price with prvious buy price and track of the minimum price while iterating through the list\n\t* \tHowever, if the current price is greater than the previous buy price \n\t\t* \tnow check if you sell it now would you get a better profit than the previous one \n\t\t\t* \tKeep track of the maximum profit\n```\n\nclass Solution(object):\n    def maxProfit(self, prices):\n        \n        buy_price = prices[0] # at the begining the minimum price is the first price\n        profit = 0 # at the begining the minimum  profit is zero\n        \n        for i in range(1,len(prices)):\n            #if the current price is less than the previous buy price ; update the buy_price\n            if prices[i] < buy_price:\n                buy_price = prices[i]\n            else: # if not check if you sell with the current price would you get better profit than the previous one\n                profit = max(profit, prices[i]-buy_price) # compare the previous profit with the current profit\n                \n        return profit\n\t\t\n\t\t#Runtime: 916 ms\n\t\t#Memory Usage: 23 MB\n```\n        \n### * Idea 2 : Dynamic programming\n* Track the minimum_price and the maximum profit together using a dp table\n\t* Thus each dp table cell will hold two items --> dp = [[min_price, max_profit], [min_price, max_profit], ........[min_price, max_profit]]\n        \n```\nclass Solution(object):\n    def maxProfit(self, prices):\n        n = len(prices)\n        dp = [0]*n # initializing the dp table\n        dp[0] = [prices[0],0] # filling the the first dp table --> low_price = prices[0] max_profit=0\n        min_price = max_profit = 0\n        # Note that ---> indixing the dp table --> dp[i-1][0] stores minimum price and dp[i-1][1] stores maximum profit\n        for i in range(1,n):\n            min_price = min(dp[i-1][0], prices[i]) # min(previous_min_price, cur_min_price)\n            max_profit = max(dp[i-1][1], prices[i]-dp[i-1][0]) # max(previoius_max_profit, current_profit)\n            dp[i] =[min_price,max_profit]\n                \n        return dp[n-1][1]\n\t\t#Runtime: 1220 ms, \n\t\t#Memory Usage: 32.4 MB,\n```\n### \n### ******* If you found this helpful , upvote and comment as sign of appreciation **********
['Dynamic Programming', 'Python', 'Python3']