> For the complete documentation index, see [llms.txt](https://chiragjain.gitbook.io/neetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chiragjain.gitbook.io/neetcode/121-best-time-to-buy-and-sell-stock/code.md).

# CODE

```python
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        min_prefix = prices[0]
        max_profit = 0 
        # To sell at the current price, what could have been the 
        # lowest buy price on the left
        for price in prices:
            max_profit = max(price - min_prefix, max_profit)
            min_prefix = min(min_prefix, price)
        return max_profit
            
        
        
```
