码迷,mamicode.com
首页 > 其他好文 > 详细

Best Time to Buy and Sell Stock with Cooldown

时间:2016-07-06 09:55:58      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!