###### 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 ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up