题目

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: Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

解题思路:

第一种方案, 假设数组长度为n, dp[i][j]为从i到j所能达到的最大收益,那么本题即求dp[0][n - 1],

对于dp[i][j], 其可能的cooldown位置有 I, i + 1, ..., j - 1, j,

所以存在递推关系

dp[i][j] = max{ dp[i][k - 1] + dp[k + 1][j]} k = i, i + 1, ... , j - 1, j

当k == i 时, dp[i][k - 1] 不存在,即只有dp[k + 1][j], 同理

当k == j 时, dp[k + 1][j] 不存在,即只有dp[i][k - 1]

prices[j] - prices[I] 为dp[I][j]的初始值

所以最终dp[i][j] = max(prices[j] - prices[I], max{dp[i][k - 1] + dp[k + 1][j]})

而题目希望求解的是dp[0][n - 1]. 所以i 从n-1往0求解,j从0往n-1求解

时间复杂度O(n^3) 空间复杂度O(n^2)

代码如下

class Solution {
public:
//suppose cooldown at k
//dp[i][j] = max{dp[i][k - 1] + dp[k + 1][j]} k = i ... j
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (0 == n) return 0;
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
dp[i][j] = prices[j] - prices[i];
}
} for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
//cout<<"i="<<i<<" j="<<j<<" "<<dp[i][j]<<endl;
for (int k = i; k < j; k++) {
int tmp = 0;
if (k - 1 >= i) {
tmp += dp[i][k - 1];
} if (k + 1 <= j) {
tmp += dp[k + 1][j];
} dp[i][j] = max(dp[i][j], tmp);
}
}
} return dp[0][n - 1];
}
};

第二种方案:顺序DP

常规的DP的类型主要有三类,矩阵dp,一个一维数组的dp,两个一维数组的dp

矩阵dp 构造f[i][j], 一维dp构造f[i], 两个一维dp构造f[i][j]

本题恰好可以使用顺序dp,而且是一维的数组

解题思路:

每一天股票的持有状态可能有三种情况

cool down-->buy-->sell-->cool down-->buy-->sell-->cool down

状态转换的关系如上, leetcode讨论区有人画了状态图,非常容易理解, 参考链接

https://leetcode.com/explore/interview/card/top-interview-questions-hard/121/dynamic-programming/862/discuss/75928/Share-my-DP-solution-(By-State-Machine-Thinking)

也就是说

buy的状态 可能是从前一个buy 或者前一个cool down过来

sell的状态 只能是从前一个buy过来

cool down的状态 可能是从前一个cool down或者前一个sell的状态过来

这里需要搞清楚

1)sell 和 cool down的区别, sell状态只有 卖出的那个时刻状态是保持的, 卖完第二天状态就是cool down了.

2)buy 到 sell 之间的这段时间,按题意并不算cool down,而全是buy状态

3)sell 到 cool down之间的这段时间,全是cool down状态

由此可以得出

buy[i] = max(buy[i - 1], rest[i - 1] - prices[I]) // 这里用rest 表示 cool down

rest[i] = max(rest[i - 1], sell[I - 1])

sell[I] = buy[I - 1] + prices[i]

代码如下

java

class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
if (0 == n) return 0; int[] buy = new int[n];
int[] rest = new int[n];
int[] sell = new int[n]; buy[0] = -prices[0];
rest[0] = 0;
sell[0] = Integer.MIN_VALUE; for (int i = 1; i < n; i++) {
buy[i] = Math.max(buy[i - 1], rest[i - 1] - prices[i]);
rest[i] = Math.max(rest[i - 1], sell[i - 1]);
sell[i] = buy[i - 1] + prices[i];
} return Math.max(rest[n - 1], sell[n - 1]);
}
}

c++

class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (0 == n) return 0; vector<int> buy(n, 0);
vector<int> rest(n, 0);
vector<int> sell(n, 0);
buy[0] = -prices[0];
rest[0] = 0; //不可能存在,所以收益取最小,因为i位置,我们希望取的是最大值,
//将sell设置为最小值,表示永远不可能取该值
sell[0] = INT_MIN; for (int i = 1; i < n; i++) {
buy[i] = max(buy[i - 1], rest[i - 1] - prices[i]);
rest[i] = max(rest[i - 1], sell[i - 1]);
sell[i] = buy[i - 1] + prices[i];
} return max(rest[n - 1], sell[n - 1]);
}
};

解题报告Best Time to Buy and Sell Stock with Cooldown的更多相关文章

  1. LeetCode解题报告—— Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  2. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

  3. 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. 买卖股票 ...

  4. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. [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 ...

  6. [LeetCode] 309. 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 ...

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

  8. LeetCode Best Time to Buy and Sell Stock with Cooldown

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

  9. 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票

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

随机推荐

  1. 关于ng的过滤器的详细解释angular-filter

    在使用ng框架做项目的时候,我们可能会使用到ng自带的过滤器,来进行数据的筛选 一:ng自带的过滤器:currency ,date,limitTo,lowercase,uppercase,number ...

  2. java代码数组求平均值,最大值,最小值

    (测试类) package com.badu; public class Tste { public static void main(String[] args) { Class5 sa=new C ...

  3. Java 对象的创建和使用

    1.创建对象 类名 对象名 = new 类名(): Telphone phone = new Telphone; 2.使用对象 引用对象的属性:对象名 . 属性 phone.screen = 5; / ...

  4. LockSupport分析

    LockSupport是java.util.concurrent.locks包中的一个工具类,主要提供了一些在创建锁和同步类中用来阻塞其他线程的原始操作. 当有多个线程需要获取同一个资源的锁的时候,如 ...

  5. Hadoop Map/Reduce的工作流

    问题描述 我们的数据分析平台是单一的Map/Reduce过程,由于半年来不断地增加需求,导致了问题已经不是那么地简单,特别是在Reduce阶段,一些大对象会常驻内存.因此越来越顶不住压力了,当前内存问 ...

  6. 简单租房子实例详解---(session、ajax、json前后台数据处理、分页)

    本次实例我们结合session.ajax.json前后台数据处理.分页技术做一个租房信息系统 一共有五个界面:包括 管理员和用户的登录界面 登录界面的后台 <?php session_start ...

  7. IDA Pro 权威指南学习笔记(八) - 基本 IDA 导航

    导航目标 在分析阶段,IDA 会通过检查二进制文件的符号表生成符号名称,或根据二进制文件引用位置的方式自动生成一个名称 反汇编窗口中显示的任何名称都是导航目标 双击任何一个符号,IDA 将跳转到相应的 ...

  8. 前端学习---JavaScript

    JavaScript基本知识 JavaScript是一门独立的语言,像我们学习php,python等需要安装apache,python3.6,那我们学习JavaScript只需要我们电脑有一个浏览器即 ...

  9. php502故障处理

    一次打开网站,发现502,第一反应肯定是php-fpm没启动,尝试启动还是502. 1.首先查询Nginx日志发现如下连接PHP失败: 2016/07/29 15:56:04 [error] 2376 ...

  10. h3c 云计算管理平台