【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown

从开通VPS到搭建宝塔面板全教程 你在输入域名(此时我们可能没有类似aaa.com这样的域名,所以要输入VPS的IP地址)的时候,下面的根目录也会跟着变动,等输入完成后,你可以把根目录再改回成/www/wwwroot/dq。这时,在你的浏览器中,输入外网面板地址的URL,输入username和password,即可登陆宝塔面板。第一次登陆,需要绑定你的宝塔账号,你可以到宝塔的注册界面,用手机注册一个,然后绑定,首先看一下你的VPS操作系统是什么类型的(其实你在选择VPS的时候,就已经选定了,我选择的事Centos系统)。 阅读详情

309. Best Time to Buy and Sell Stock with Cooldown

Description:

Say you have an array for which the i^{th} 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]

解题思路:

(1)动态规划解法:

这道题可以用动态规划思路来解决。这道题的难点在于如何写出状态转移方程。下面我们来分析一下题目:

在每一次股票操作之后,都可能出现三种状态:休息(rest)、持有(hold)和已卖出(sold)。下图是这三种状态的状态机。

1)对于rest,有两种情况:一种是,上一个时间再休息,现在接着休息。另一种是,上一个时间是卖出,那么现在必须是休息状态。

2)对于hold,有两种情况:一种是,上一个时间是休息,现在可以买入股票,变成持有状态。另一种是,上一个时间是持有,现在可以暂停交易进入休息,依然变成持有状态。

3)对于sold,只能是把持有的股票卖出,变成已卖出状态。

状态机

 根据以上分析写出,状态转移方程:

带入例子中,验证状态转移方程是否正确:

已经AC的代码:

class Solution:
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :type: int
        """
        if len(prices) <= 1:
            return  0
        hold = [0] * len(prices)
        sold = [0] * len(prices)
        rest = [0] * len(prices)
        hold[0] = -prices[0]
        sold[0] = -float("inf")
        for i in range(1,len(prices)):
            hold[i] = max(hold[i - 1], rest[i - 1] - prices[i])
            sold[i] = hold[i - 1] + prices[i]
            rest[i] = max(rest[i - 1], sold[i - 1])
        return max(sold[-1], rest[-1])

solution = Solution()
Input = [1, 2, 3, 0, 2]
print(solution.maxProfit(Input))

Reference:

【1】花花酱 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown(视频)

【2】【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown

从零开始:用ADS仿真设计一个2.4GHz Wi-Fi低噪声放大器(含完整电路图) 本文提供了一份详细的ADS软件仿真指南,手把手教你从零开始设计一个2.4GHz Wi-Fi低噪声放大器。内容涵盖核心指标理解、晶体管选型与建模、匹配网络设计、偏置电路集成,并通过S参数仿真、噪声优化及线性度验证等步骤,最终呈现完整的优化电路图,助力射频初学者掌握LNA设计全流程。 阅读详情

相关推荐

使用单调栈求解最大矩形面积问题的C++实现

通过本文的介绍,我们详细讲解了如何使用单调栈求解砖墙中最大矩形面积的问题,并提供了完整的C++代码实现。希望通过本文的学习,你能掌握单调栈的基本概念及其在实际问题中的应用。如果你有任何疑问或建议,欢迎在评论区留言交流。

m0_57781768的博客 814

leetcode oj java 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 algorithm to find the maximum profit. You may complete as many transactions as you lik

Xcy的博客 629

新手必看:如何用R/qtl快速完成QTL定位分析(附实战代码)

本文为新手提供了使用R/qtl进行QTL定位分析的完整实战指南。内容涵盖从环境搭建、数据准备与清洗,到核心分析流程(包括遗传图谱计算、单QTL扫描、置换检验确定阈值)以及进阶结果可视化的全步骤。通过清晰的代码示例,帮助读者快速掌握这一数量遗传学关键分析技术,实现从数据到QTL峰值的完整分析闭环。

y7z8a的博客 501

【LeetCode题解】309. Best Time to Buy and Sell Stock with Cooldown

此题在之前121题的基础上做了细微调整,首先可以有多次买入或卖出的事件发生,且在卖出股票的下一天不能进行股票交易活动 解题思路: 需要创建两个辅助数组sell和buy,sell[i]第i天不持有股票的最大利润,buy[i]表示第i天持有股票的最大利润。 一天的交易状态就是否持有股票来说,只包含持有状态和不持有状态两种 1、对于持有状态,今日可能没有进行买入操作所以跟昨天持股状态一样;也可能

duxin5的博客 394

RV1126平台JD9366触摸屏驱动移植实战:从源码到烧录的完整指南

本文详细介绍了在RV1126平台上移植JD9366触摸屏驱动的完整流程,从环境准备、源码获取到驱动配置、内核编译、设备树设置以及固件烧录与测试验证。通过实战经验分享和常见问题排查,帮助开发者高效完成触摸屏驱动移植,适用于嵌入式Linux开发场景。

weixin_30872337的博客 409

QMT Python API 实战指南:从安装到策略开发的完整流程

本文提供了一份详尽的QMT Python API实战指南,涵盖从环境配置到策略开发的完整流程。文章深入解析了xtdata行情模块与xttrader交易模块的核心架构与协作方式,并通过一个双均线策略实例,手把手教你如何获取数据、计算信号并执行交易,是量化交易开发者快速上手QMT Python接口文档的实用教程。

butter的博客 1416

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

【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode...

负雪明烛 3555

数字电路实验避坑指南:如何正确搭建主从JK触发器测试电路(附Proteus仿真)

本文详细解析了主从JK触发器的工作原理及实验搭建指南,重点解决了常见的Catching Problem现象,并提供了Proteus仿真优化方案。通过元器件选型、电路连接步骤和常见错误排查,帮助读者正确搭建测试电路,确保数字电路实验的稳定性和可靠性。

weixin_30790841的博客 470

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

卷卷萌的博客 466

RSL-RL:开源人形机器人强化学习控制研究库

摘要:RSL-RL是一款专为机器人领域设计的开源强化学习库,其核心特点是紧凑性、可修改性和高性能。该库聚焦PPO和BC两大主流算法,并整合了对称性增强、好奇心驱动等机器人专用技术。通过纯GPU训练优化,RSL-RL在大型仿真中展现出卓越的吞吐性能,已成功应用于腿足运动控制、全身协调等多个机器人研究场景。其轻量级架构包含运行器、算法和网络三个可独立修改的组件,支持TensorBoard等主流日志工具。该框架特别适合需要快速原型开发的机器人研究者,但不适用于通用机器学习基准测试。开源地址:https://git

2506_90492529的博客 1675

麒麟服务器操作系统QT系列软件工具手册

对Qt提供的开发工具进行了一系列的优化之后,在日常的修复bug和维护框架的过程中,桌面研发对Qt的基础软件库也进行了部分优化工作,包括qtbase、qtwayland、qtscript、qt4-x11等Qt基础组件,涉及的Qt框架版本主要涉及到5.9和5.12两个版本。qt 的翻译,是由 ts 文件生成 qm 文件然后进行翻译,国产操作系统上qt 的翻译是由单独的qttranslations包中进行维护,其次部分汉化问题也是qt组件维护工作的一部分,qt的翻译机制如图4-7所示。

太极淘的博客 543

[刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown

题目 代码 class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1) return 0; int l = prices.length; int a=0, b = -prices[0], c = 0; for (int i = 1; i < l; ++i){ int temp = a;

qq_27637285的博客 309

【python股票价格预测】基于CNN-LSTM的股票价格预测(附python代码)

【python股票价格预测】基于CNN-LSTM的股票价格预测(附python代码)

k8291121的博客 2008

【Java力扣算法】LeetCode 309 Best Time to Buy and Sell Stock with Cooldown(动态规划)

题目: 给定一个整数数组,其中第i个元素代表了第i天的股票价格 。​ 设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。 示例: 输入: [1,2,3,0,2] 输出: 3 解释: 对应的交易状态为: ...

懂技术的猫 674

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown

Description 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 (i...

Learning from the mistakes 513

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

leetcode 309. Best Time to Buy and Sell Stock with Cooldown

这道题与的区别就是卖出股票后的一天不能买股票。仍然用动态规划解决。这类题目的关键是要分析每一天有几种状态,用dp数组变量记录这些状态。

weixin_41554427的博客 1165

【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)

LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划)Problem 309: Best Time to Buy and Sell Stock with Cooldown [Medium]解题思路Solution (Java)修改过程 Problem 309: Best Time to Buy and Sell Sto...

Fayedily的博客 323

【leetcode 动态规划 C++】309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size() < 2) return 0; vector<int> buy(prices.size(), 0); vector<int> sell(price

izhxxx的博客 180

【leetcode】【medium】309. Best Time to Buy and Sell Stock with Cooldown​​​​​​​

309. Best Time to Buy and Sell Stock with Cooldown Say you have an array for which theithelement is the price of a given stock on dayi. Design an algorithm to find the maximum profit. You may com...

lemonade13的博客 146

【leetcode笔记】Python实现 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 algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy ...

Jack_Kuo的博客 585

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

cool down time: at least one day //we use dp, and the following explaination is crystal clear // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75931/Easiest-JAVA-solution-with-explanations class Solution { public i

Tech in Pieces 210

Leetcode 309. Best Time to Buy and Sell Stock with Cooldown

这道题用的是dp的思想,用buy, sell, cooldown记录当前这些状态下最多的profit。 对于day i 而言: sell[i] = buy[i-1] + prices[i] buy[i] = max(muy[i-1], cooldown[i-1] - prices[i]) cooldown[i] = max(cooldown[i-1]...

I_ren的博客 373

【leetcode】309. Best Time to Buy and Sell Stock with Cooldown

题目: 思路: 抄作业,看的博文https://blog.csdn.net/qq508618087/article/details/51671504。 动态规划。设置两个数组——数组buy和数组sell(两个数组均从0开始计算)。buy[i]指购买第i天股票所能获得的最大利润;sell[i]指卖掉第i天股票所能获得的最大利润。动态规划公式如下: buy[i] = max(sell[i-2]-prices[i], buy[i-1]); sell[i] = max(buy[i-1]+prices[i],s

zxc120389574的博客 178

LeetCode: 309. Best Time to Buy and Sell Stock with Cooldown

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 algorithm to find the maximum profit...

杨领well的专栏 193

Leetcode309. Best Time to Buy and Sell Stock with Cooldown

最佳股票买卖时期:① 当卖出股票后进入一天的冷冻期,不能进行任何操作。 ② 必须在再次购买前出售之前的股票

magic_jiayu的博客 271

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

题目链接: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 fi...

小榕流光的专栏 4741
上一篇: 【LeetCode】337. House Robber |||
下一篇: 动态规划学习-【0-1背包问题】
Microstrong0305
Microstrong0305 领域专家: 算法与数据结构技术领域 领域专家: 算法与数据结构技术领域
博客等级 码龄12年 6405粉丝 312原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值