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].

posted on 2016-07-06 05:43  keepshuatishuati  阅读(192)  评论(0编辑  收藏  举报