- 
      
 - 
        
Save ImaginaryStargazer/6657eefc58ea10707a4ff4b498c8bd23 to your computer and use it in GitHub Desktop.  
    309. Best Time to Buy and Sell Stock with Cooldown
  
        
  
    
      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 { | |
| func maxProfit(_ prices: [Int]) -> Int { | |
| guard !prices.isEmpty else { | |
| return 0 | |
| } | |
| var hold = [Int](repeating: Int.min, count: prices.count + 1) | |
| var sell = [Int](repeating: 0, count: prices.count + 1) | |
| var cooldown = [Int](repeating: 0, count: prices.count + 1) | |
| for day in 1...prices.count { | |
| hold[day] = max(hold[day-1], cooldown[day-1] - prices[day-1]) | |
| sell[day] = hold[day-1] + prices[day-1] | |
| cooldown[day] = max(cooldown[day-1],sell[day-1]) | |
| } | |
| return max(cooldown[prices.count],hold[prices.count],sell[prices.count]) | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment