Best Time to Buy and Sell Stock with Cooldown
Posted keepshuatishuati
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Best Time to Buy and Sell Stock with Cooldown相关的知识,希望对你有一定的参考价值。
1 public class Solution { 2 public int maxProfit(int[] prices) { 3 int lastBuy = 0, lastSell = 0, buy = Integer.MIN_VALUE, sell = 0; 4 for (int i = 0; i < prices.length; i++) { 5 lastBuy = buy; 6 buy = Math.max(buy, lastSell - prices[i]); 7 8 lastSell = sell; 9 sell = Math.max(sell, lastBuy + prices[i]); 10 } 11 return sell; 12 } 13 }
lastSell = sell is after buy max. So the buy price is compared with sell[i-2]. Current sell is sell[i-1].
以上是关于Best Time to Buy and Sell Stock with Cooldown的主要内容,如果未能解决你的问题,请参考以下文章
最强解析面试题:best-time-to-buy-and-sell-stock
最强解析面试题:best-time-to-buy-and-sell-stock
121. Best Time to Buy and Sell Stock
122. Best Time to Buy and Sell Stock II