原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

题目:

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]

题解:

Let buy[i] denotes maximum profit till index i, series of transactions ending with buy.

Let sell[i] denotes maimum profit till index i, series of transactions ending with sell.

There is a cooldown before buy again. buy[i] could happend after sell[i-2], not sell[i-1].

Thus, buy[i] = Math.max(buy[i-1], sell[i-2]-prices[i]).

If wanna sell a stock, must buy stock before. Thus sell[i] is calculated with buy[i-1].

sell[i] = Math.max(sell[i-1], buy[i-1]+prices[i]).

只用到了i-1, i-2两个之前的量,所以可以使用常量来代替数组.

b0 as buy[i], b1 as buy[i-1].

s0 as sell[i], s1 as sell[i-1], s2 as sell[i-2].

Time Complexity: O(n). n = prices.length.

Space: O(1).

AC Java:

 class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length < 2){
return 0;
} int b0 = -prices[0];
int b1 = b0;
int s2 = 0;
int s1 = 0;
int s0 = 0;
for(int i = 1; i<prices.length; i++){
b0 = Math.max(b1, s2-prices[i]);
s0 = Math.max(s1, b1+prices[i]); b1 = b0;
s2 = s1;
s1 = s0;
} return s0;
}
}

LeetCode Best Time to Buy and Sell Stock with Cooldown的更多相关文章

  1. [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  6. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  7. [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费

    Your are given an array of integers prices, for which the i-th element is the price of a given stock ...

  8. Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)

    Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...

  9. [LeetCode] Best Time to Buy and Sell Stock 6道合集【DP】

    1. Best Time to Buy and Sell Stock 2. Best Time to Buy and Sell Stock II 3. Best Time to Buy and Sel ...

随机推荐

  1. 未能加载文件或程序集"Microsoft.Web.Infrastructure 的解决方案

    转载请注明来源: http://www.cnblogs.com/zaiyuzhong/p/Unload-Infrastructure-Solution.html 部署MVC5  项目发布到文件系统 I ...

  2. android 第三方 Im

    1.阿里百川 单聊.群聊.客服能力集成,仅需花费4小时,不收费,0成本接入,让App轻松拥有沟通能力,历经多次双十一考验,消息到达率100%,全年可用性高达99.99%,登录异常提醒,木马钓鱼网站监测 ...

  3. 读取和写入 文件 (NSFIleManger 与 NSFileHandle)

    读取和写入 文件 //传递文件路径方法 -(id)initPath:(NSString *)srcPath targetPath:(NSString *)targetPath { self = [su ...

  4. 【BZOJ】1115: [POI2009]石子游戏Kam

    http://www.lydsy.com/JudgeOnline/problem.php?id=1115 题意:n堆石子,个数是从左到右单增.每一次可以从任意堆取出任意石子,但要保持单增这个性质.问先 ...

  5. Android -- TextView (3)

    1.效果图

  6. url 转码

    //URL解码 //-(NSString *)URLDecodedString:(NSString *)str //{ // NSString *decodedString=(__bridge_tra ...

  7. IOS UINavigationController 导航控制器

    /** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...

  8. ORACLE 查看锁

    SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s W ...

  9. Linux任务调度命令(轻松管理Linux)

    Linux任务调度其实就是让系统在某个时间执行某些命令或者程序,这样可以让管理员更加轻松地管理自己的Linux,当我刚了解到这个方法时,我的内心充满了无尽的欣喜,感觉Linux实在是太强大了. 下面我 ...

  10. [LintCode] Continuous Subarray Sum 连续子数组之和

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...