原题链接在这里: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. CSS抗锯齿 font-smoothing 属性介绍

    CSS3里面加入了一个“-webkit-font-smoothing”属性. 这个属性可以使页面上的字体抗锯齿,使用后字体看起来会更清晰舒服. 加上之后就顿时感觉页面小清晰了. 淘宝也在用哦! 它有三 ...

  2. 转自虫师:性能测试的 Check List

    原文地址:http://www.cnblogs.com/jackei/archive/2006/03/24/357372.html 1. 开发人员是否提交了测试申请? 2. 测试对象是否已经明确? 3 ...

  3. Code[VS] 2370 LCA 题解

    Code[VS] 2370 小机房的树 题解 RMQ 树链剖分 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同 ...

  4. HDU 4503 湫湫系列故事——植树节(单色三角形)

    题目链接 #include <cstdio> using namespace std; int main() { int n,sum,a,t,i; scanf("%d" ...

  5. 【CodeVS】1993草地排水

    题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水 ...

  6. 如何更改tableView cell的accessoryView位置,如何让首尾的Separator不显示

    一,如何更改tableView cell的accessoryView位置 1.实则是更改不了的,因此右边总会有一个小边距. 2.可以向 cell 的 contentView 中添加按钮放在右边,与 c ...

  7. Thymeleaf学习内容

    Thymeleaf Page Layouts Spring MVC and Thymeleaf: how to access data from templates thymeleaf-layout- ...

  8. Connect模块解析

    Connect模块背景 Node.js的愿望是成为一个能构建高速,可伸缩的网络应用的平台,它本身具有基于事件,异步,非阻塞,回调等特性,这在前几篇专栏中有过描述. 正是基于这样的一些特性,Node.j ...

  9. Lanterns

    Lanterns 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/B 题目: Description ...

  10. CSS的class、id、css文件名的常用命名规则

    CSS的class.id.css文件名的常用命名规则        (一)常用的CSS命名规则 头:header       内容:content/container       尾:footer   ...