题目:

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]

提示:

这道题可以用动态规划的思路解决。但是一开始想的时候总是抽象不出状态转移方程来,之后看到了一种用状态机的思路,觉得很清晰,特此拿来分享,先看如下状态转移图:

这里我们把状态分成了三个,根据每个状态的指向,我们可以得出下面的状态转移方程:

  • s0[i] = max(s0[i-1], s2[i-1])
  • s1[i] = max(s1[i-1], s0[i-1] - price[i])
  • s2[i] = s1[i-1] + price[i]

这样就清晰了很多。

代码:

 class Solution {
public:
int maxProfit(vector<int>& prices){
if (prices.size() <= ) return ;
vector<int> s0(prices.size(), );
vector<int> s1(prices.size(), );
vector<int> s2(prices.size(), );
s1[] = -prices[];
s0[] = ;
s2[] = INT_MIN;
for (int i = ; i < prices.size(); i++) {
s0[i] = max(s0[i - ], s2[i - ]);
s1[i] = max(s1[i - ], s0[i - ] - prices[i]);
s2[i] = s1[i - ] + prices[i];
}
return max(s0[prices.size() - ], s2[prices.size() - ]);
}
};

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

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

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

  2. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  3. 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)

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

  4. 【leetcode】714. 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 ...

  5. 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...

  6. 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)

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

  7. 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)

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

  8. 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)

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

  9. 【LeetCode】122. 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 a ...

随机推荐

  1. Tomcat--各个目录详解(二)

    Tomcat整体目录: 一.bin文件(存放启动和关闭tomcat脚本) 其中.bat和.sh文件很多都是成对出现的,作用是一样的,一个是Windows的,一个是Linux. ① startup文件: ...

  2. protobuf那些事

    大家好,俺又来写博客了.......上次剧情预告说,这次会写hive的博客.......好吧,那俺就不打算写hive了.......老码农路子就是要野(本人不老,不能说得影响了找女票)......这次 ...

  3. ASP.NET Core 菜鸟之路:从Startup.cs说起

    1.前言 本文主要是以Visual Studio 2017 默认的 WebApi 模板作为基架,基于Asp .Net Core 1.0,本文面向的是初学者,如果你有 ASP.NET Core 相关实践 ...

  4. 【PHP】文件写入和读取详解

    文章提纲: 一.实现文件读取和写入的基本思路 二.使用fopen方法打开文件 三.文件读取和文件写入操作 四.使用fclose方法关闭文件 五.文件指针的移动 六.Windows和UNIX下的回车和换 ...

  5. 基本DOS命令之 netstat 命令详解

     netstat 命令(查看端口) netstat 命令用于显示与 IP .TCP .UDP 和 ICMP 协议相关的统计数据,一般用于检验本机各端口的网络连接情况,可以使用 netstat 命令查看 ...

  6. jquery源码 Callback

    工具方法.对函数的统一管理. jquery2.0.3版本$.Callback()部分的源码如下: // String to Object options format cache var option ...

  7. 开发一个Servlet示例

    Servlet响应请求步骤: Servlet是一个基于Java技术的Web组件,运行在服务器端,用户利用Servlet可以很轻松地扩展Web服务器的功能,使其满足特定的应用需要.Tomcat是一个常用 ...

  8. MyBatis起步

    作用:封装了JDBC操作,简化数据库访问代码.封装的功能:1.获取连接,执行SQL,释放连接2.SQL参数设置(可以直接传入对象,Mybtis会将对象的属性传入SQL语句) #{属性值}取代JDBC的 ...

  9. 局域网内部署 Docker Registry

    在局域网内部署 Docker Registry 可以极大的提升平时 pull.push 镜像的速度,从而缩短自动化操作的过程.同时也可以缓解带宽不足的问题,真是一举多得.本文将从创建单机的 Docke ...

  10. PHP中array_merge函数与array+array的区别

    在PHP中可以使用array_merge函数和两个数组相加array+array的方式进行数组合并,但两者效果并不相同,下面为大家介绍两者具体的使用区别. 区别如下: 当下标为数值时,array_me ...