Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 16, 2017 01:46
Show Gist options
  • Save cixuuz/9cfbf415fa64adbbae34fa4d10df2651 to your computer and use it in GitHub Desktop.
Save cixuuz/9cfbf415fa64adbbae34fa4d10df2651 to your computer and use it in GitHub Desktop.
[309. Best Time to Buy and Sell Stock with Cooldown] #leetcode
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