---
# System prepended metadata

title: 121.Best Time to Buy and Sell Stock
tags: [LeetCode]

---

###### tags: `LeetCode`
121.Best Time to Buy and Sell Stock
===
想法: 
把Max先縮到最小，min放到最大，然後去跑一個迴圈，減去之後若比本來的最大值大，就輸出。

作法:
max跟min令值，然後替代最小值，找每一個數值減去最小值，來找取最大max。
最後回傳最大MAX value


```
class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        max_profit, min_price = 0,float("inf") 

        for price in prices:
            min_price = min(min_price, price)
            max_profit = max(max_profit, price-min_price)
        
        return max_profit
```