Python 3   ||   5 lines,  w/ explanation and example   ||   T/S: 95% / 85%
Here\'s the plan:\n\nWe iterate throughprices, keeping track of\n1. mn, the minimumprice were we to buy that day,and \n2. mx, the potential maximum profit, were
Here\'s the plan:\n\nWe iterate through`prices`, keeping track of\n1. `mn`, the minimum`price` were we to buy that day,and \n2. `mx`, the potential maximum profit, were we to sell that day.\n\nWe return the profit`mx`after completing the iteration\n\n```\nclass Solution:\n    def maxProfit(self, prices: list[int]) -> int:\n                                            # Example: prices = [7,1,5,3,6,4]\n                                            #\n        mn, mx = prices[0], 0               #   p   mn   mx  \n                                            #  \u2013\u2013\u2013  \u2013\u2013\u2013  \u2013\u2013\u2013\n        for p in prices:                    #        7    0    <--   initial values\n                                            #   1    1    0    <--   1 - 1 = 0, so mx = 0 still\n            if   p < mn     : mn = p        #   5    1    4    <--   5 - 1 = 4, so mx = 4 now\n            elif p > mx + mn: mx = p - mn   #   3    1    4    <--   3 - 1 = 2, so mx = 4 still\n                                            #   6    1    4    <--   6 - 1 = 5, so mx = 5 now\n        return mx                           #   4    1    5    <--   4 - 1 = 3, so mx = 5 still\n                                            #\n                                            #                         return 5\n\n```\n[https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/1292506689/](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/1292506689/)\n\nI could be wrong, but I think that time complexity is *O*(*N*) and space complexity is *O*(1), in which *N* ~ `len(prices)`.
['Python3']