首页 > 其他 > 详细

Best Time to Buy and Sell Stock with Cooldown

时间:2016-07-06 09:52:55      阅读:153      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!