题目描述

给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。​

设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

  • 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
  • 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

示例:

输入: [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]

解决方案1

可以通过一个状态转换图,来解决。

定义状态

s0 : 没有买股票时
s1 :买入股票且没有卖出股票时
s2:卖出股票是

那么状态转换可以这样表示

so->s1   买股票  s0->s0不操作
s1->s2 卖股票 s1->s1不操作
s2->s0 经过冷冻期 s2->s2 不操作

转换图也可以画出来:



那么每一次价格变动都会在这三个状态里

得出递推式

s0[i] = max(s0[i - 1], s2[i - 1]);   不操作 和  冷冻期过去
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); 不操作 和 买股票
s2[i] = s1[i - 1] + prices[i]; 卖出股票

最终的状态应该会在s0和s2终态度有关, 因为s1是卖出股票 是 会比s0小的,

最优解表示为 max(s0[最后一次价格变动],s2[最后一次价格变动])

具体代码如下;

class Solution {
public:
int maxProfit(vector<int>& prices){
if (prices.size() <= 1) return 0;
vector<int> s0(prices.size(), 0);
vector<int> s1(prices.size(), 0);
vector<int> s2(prices.size(), 0);
//初始化
s1[0] = -prices[0];
s0[0] = 0;
s2[0] = INT_MIN;
//状态转移
for (int i = 1; i < prices.size(); i++) {
s0[i] = max(s0[i - 1], s2[i - 1]);
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]);
s2[i] = s1[i - 1] + prices[i];
}
return max(s0[prices.size() - 1], s2[prices.size() - 1]);
}
};

总结

这个动态规划问题竟然能用一种状态转换的方式来得到也是很神奇了, 动态规划关键是找到此时最优解和上一次的最优解之间的关系,而这个题目最优解是有2个状态产生的,而状态直接又有着紧密的关系,所以用状态转换图来理清关系,找到最优解的更新表示,最终得到结果。还是值得深思的。

LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案的更多相关文章

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

  2. LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)

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

  4. [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)

    原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...

  5. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

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

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

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

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

随机推荐

  1. P2060 马步距离(洛谷)

    我们无论遇到什么困难,都不要拖,微笑着面对他,战胜拖延的最好方法就是面对拖延. 今天又拖延了…… 早晨听完老师讲课,本想做一道题练练手的,结果因为懒,瘫了一上午.最后在固定的刷题时间去面对了这道题,然 ...

  2. T133316 57级返校测试重测-T4-字符串的修改

    大致题意: 有一个A字符串和一个B字符串, 操作将A或A的一个后缀修改为B, 求最少的操作数. 有三个操作为: 删除: 删除掉 A 中的某一个字符. 添加: 将某一个字符添加到 A 中任意位置. 替换 ...

  3. javascript原型:写一个合并后数组去掉同类项的方法

    <!DOCTYPE html> <html> <head> <title>test013_Array_prototype_unique()</ti ...

  4. 高效C++:设计与声明

    C++软件开发可以理解为设计一系列的类,让这些类相互使用,最终实现我们所需要的功能.类与类之间的相互关系可以很复杂,也可以很简单,如何简单高效的描述类与类之间的关系是设计的难点之一.遵循本文所提供的方 ...

  5. 五分钟带你深入了解Redis

    相信phper都知道Redis是什么,既然如此,为表仪式感,首先我还是得说说什么是Redis. Redis是什么 redis是一个高性能的key-value数据库,它是完全开源免费的,而且redis是 ...

  6. 题解 洛谷 P5331 【[SNOI2019]通信】

    考虑用费用流解决本题. 每个哨站看作一个点,并将其拆为两个点,建图方式为: \(S \longrightarrow x_i\) 容量为\(1\),费用为\(0\) \(x_i \longrightar ...

  7. 关于node-sass安装失败问题

    在进行Vue开发中npm run dev报错,按照提示尝试进行npm以及cnpm安装均失败 解决办法:npm uninstall node-sass卸载模块 指定淘宝镜像源安装 npm i node- ...

  8. MacOS入门

    原文池建强的blog 对普通用户来说,用好Mac主要有三点: 1.理解OSX的基本结构和特点 2.掌握多手势和快捷键(少量即可,多多益善) 3.用好工具 一.理解OSX的基本结构和特点 Mac OS ...

  9. matplotlib基础汇总_04

    3D图形 导包 import numpy as np import matplotlib.pyplot as plt #3d图形必须的 from mpl_toolkits.mplot3d.axes3d ...

  10. PHP arsort() 函数

    ------------恢复内容开始------------ 实例 对关联数组按照键值进行降序排序: <?php$age=array("Peter"=>"35 ...