标签:
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
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/5645594.html