Created
October 16, 2017 01:46
-
-
Save cixuuz/9cfbf415fa64adbbae34fa4d10df2651 to your computer and use it in GitHub Desktop.
[309. Best Time to Buy and Sell Stock with Cooldown] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
# O(n) O(1) | |
def maxProfit(self, prices): | |
""" | |
:type prices: List[int] | |
:rtype: int | |
""" | |
if len(prices) < 2: | |
return 0 | |
sell, buy, prev_sell, prev_buy = 0, -prices[0], 0, 0 | |
for price in prices: | |
prev_buy = buy | |
buy = max(prev_sell - price, prev_buy) | |
prev_sell = sell | |
sell = max(prev_buy + price, prev_sell) | |
return sell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment