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

LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题 LeetCode 123. Best Time to Buy and Sell Stock III–Python解法–动态规划 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部分题目C++,Python,Java的解法都有。 题目地址:Best Time to Buy and Sell Stock III - LeetCode... 阅读详情

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

Problem 309: Best Time to Buy and Sell Stock with Cooldown [Medium]

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]

来源:LeetCode

解题思路

本题仍然需要使用动态规划思想。与题解188:Best Time to Buy and Sell Stock IV同属于最佳时机买卖股票问题。

解题思路与之前不同的是,不再使用minCost和maxP的思路,而是直接使用maxP的状态转移方程,每天的maxP分为两种:maxP0和maxP1,分别代表当天不持有股票和当天持有股票的最大利润。本题中在买操作之前有一个冷却期,因此第i天的maxP0可以由第i-1天的maxP0和maxP1转移,而第i天的maxP1则由第i-1天的maxP1和第i-2天的maxP0转移。这是由于第i天买入股票的前提条件是在第i-2天之前已经卖出,而不能在i-1天才卖出,所以无法由第i-1天的maxP0转移至第i天的maxP1。

状态转移方程为:
m a x P 0 [ i ] = max ⁡ { m a x P 0 [ i − 1 ] , m a x P 1 [ i − 1 ] + p r i c e s [ i ] } m a x P 1 [ i ] = max ⁡ { m a x P 1 [ i − 1 ] , m a x P 0 [ i − 2 ] − p r i c e s [ i ] } maxP_0[i] = \max\{maxP_0[i-1], maxP_1[i-1] + prices[i]\} \\ maxP_1[i] = \max\{maxP_1[i-1], maxP_0[i-2] - prices[i]\} maxP0​[i]=max{maxP0​[i−1],maxP1​[i−1]+prices[i]}maxP1​[i]=max{maxP1​[i−1],maxP0​[i−2]−prices[i]}
其中, m a x P 1 [ i − 1 ] + p r i c e s [ i ] maxP_1[i-1] + prices[i] maxP1​[i−1]+prices[i]的意思是第i-1天持有的股票在当前天卖出; m a x P 0 [ i − 2 ] − p r i c e s [ i ] maxP_0[i-2] - prices[i] maxP0​[i−2]−prices[i]的意思是第i-2天之前卖出的股票在当前天买入。

由于maxP0更新只使用到前一天的maxP0和maxP1,maxP1更新也只使用到前一天的maxP1和再前一天的maxP0,因此可以不使用maxP0[N]和maxP1[N]来存储,只需使用常数空间。
状态转移更新为:
m a x P 0 = max ⁡ { m a x P 0 , m a x P 1 + p r i c e s [ i ] } m a x P 1 = max ⁡ { m a x P 1 , p r e m a x P 0 − p r i c e s [ i ] } maxP_0 = \max\{maxP_0, maxP_1 + prices[i]\} \\ maxP_1 = \max\{maxP_1, premaxP_0 - prices[i]\} maxP0​=max{maxP0​,maxP1​+prices[i]}maxP1​=max{maxP1​,premaxP0​−prices[i]}
其中, m a x P 0 maxP_0 maxP0​初始化为0, m a x P 1 maxP_1 maxP1​初始化为-prices[0], p r e m a x P 0 premaxP_0 premaxP0​指i-2天的 m a x P 0 maxP_0 maxP0​。

该方法时间复杂度为 O ( n ) O(n) O(n),空间复杂度为 O ( 1 ) O(1) O(1)。

运行结果:
在这里插入图片描述
要点:动态规划

Solution (Java)

class Solution {
    public int maxProfit(int[] prices) {
        int N = prices.length;
        if(N < 2) return 0;
        int minCost = prices[0];
        int[] maxP = new int[N];
        int maxP0 = 0;
        int maxP1 = -prices[0];
        int pre_maxP0 = 0;
        int temp;
        for(int i = 1; i < N; i++){
            temp = maxP0;
            maxP0 = Math.max(maxP0, maxP1 + prices[i]);
            maxP1 = Math.max(maxP1, pre_maxP0 - prices[i]);
            pre_maxP0 = temp;
        }
        return maxP0;
    }
}

修改过程

  • 一开始pre_maxP0采样先进先出队列,但是提交后发现运行时间需要2ms,后修改为两个变量temp和pre_maxP0。
微生物测序分析LEfSe LEfse分析定义 LEfse分析即LDA Effect Size分析,可以实现多个分组之间的比较,还进行分组比较的内部进行亚组比较分析,从而找到组间在丰度上有显著差异的物种(即biomaker); 阅读详情

相关推荐

目标检测 RT-DETR(2023)详细解读

在使用了这个技巧后,显然训练过程中,类别的标签不再是此前的0和1离散值,而是0~1的连续值。关于 Query Selection(查询向量选择),大家应该并不陌生,这个方法可谓在DETR领域大杀四方,如DAB-DETR对查询向量进行重构理解,将其解释为Anchor,DN-DETR通过查询降噪来应对匈牙利匹配的二义性所导致的训练时间长的问题,DINO提出从Encoder中选择Top-k特征进行学习等一系列方法,这都无疑向我们证明,查询向量很重要,选择好的Query能够让我们事半功倍。在RT-DETR中,

qq_51352130的博客 5904

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. You may complete as many transactions as you like (ie, buy one a...

ZTao-z的博客 550

Ubuntu22.04 apt 安装 ROS1 ROS Noetic Ninjemys

ubuntu 22.04 apt-get 安装 ros1 ros Noetic 最新版

autolabor的博客 5万+

LeetCode--Best Time to Buy and Sell Stock III (动态规划)

LeetCode--Best Time to Buy and Sell Stock III (动态规划) 动态规划,最大m字段和

艾斯 2580

【AI Agent 实战】OpenClaw 全面解析 + 全平台一键部署教程(本地 AI 智能体)

本文收录于 AI 智能体 & 自动化实战系列,手把手带你从零部署本地可控的 AI 执行助手关键词:OpenClaw、AI Agent、本地 AI 部署、自动化助手、Docker 部署

爱人爱己,风生水起 8310

在 Zemax 非序列模式下建立干涉仪模型

Zemax 利用其相干检测功能,将到达探测器的射线的能量相干地组合起来,同时考虑每条检测到的射线的幅度和相位。从对这些干涉条纹的定量评估来看,光学干涉仪具有广泛的应用,包括在天文学中用于测量天体的大小,在计量学中用于精确测量距离,在显微镜中用于提高分辨率,以及在物理和工程的各个领域中用于检测条件的微小变化。在光学干涉仪中,使用的是光波。非相干和相干相加的总功率是守恒的,但相干光束的峰值辐照度应该是非相干光束总和的 2 倍,这是理所当然的。请注意,条纹的缺失是显而易见的,因为不再从相干的角度观察探测器。

jkh920184196的博客 2389

LeetCode 第 123 题:买卖股票的最佳时机 III(动态规划)

思路:把状态定得再详细一点。 状态定义:dp[i][j] 表示在 [0, i] 区间里(这个状态依然是前缀性质的),状态为 j 的最大收益。 说明:j 的含义如下。 1、j = 0:还未开始交易; 2、j = 1:第 1 次买入一支股票; 3、j = 2:第 1 次卖出一支股票; 4、j = 3:第 2 次买入一支股票; 5、j = 4:第 2 次卖出一支股票。 状态转移方程:(请见代码注释...

李威威的博客 724

Python re库【正则表达式操作库】全面讲解及代码示例

以下是Pythonre库的全面讲解,包含主要功能、常用方法及代码示例。完整代码和注释附后。正则表达式(Regular Expression)是用于匹配、查找、替换字符串的工具,通过特定语法定义模式。?{n}[...][a-z]A|B\d[0-9]\D\s\S\w\Wre从字符串匹配模式,返回Match对象或None。在字符串中第一个匹配项,返回Match对象或None。返回所有匹配项的。返回所有匹配项的Match对象)。替换匹配项,返回新字符串。根据模式分割字符串,返回列表。通过。

记录点滴 1165

动态规划-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,

格物致知 297

PLC批量读写优化:C# Modbus TCP多寄存器批量操作(效率提升50%)

效率提升:请求次数减少90%以上,通信耗时降低50%-95%;资源节约:减少网络带宽占用和PLC/上位机CPU负载;稳定性提升:更少的请求意味着更少的通信异常;适配高频采集:满足工业现场100ms/次的高频率数据采集需求。工业落地时需注意:批量操作的核心是地址连续化规划,提前与PLC工程师协同规划寄存器地址,是优化的前提;同时需配合重试、校验机制,确保批量操作的可靠性。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1269

fastadmin管理员页面实现手机验证码登录功能-腾讯云短信插件

fastadmin管理员页面实现手机验证码登录功能-腾讯云短信插件

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 4206

LeetCode知识点总结 - 309

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

m0_59773145的博客 263

【RT-DETR涨点改进】全网独家-特征融合改进篇 | AAAI 2025 顶会| 高效涨点,RT-DETR利用CDFA对比度特征聚合模块,适用于目标检查(全网独家创新)

本文提出了一种基于CDFA模块的多级特征融合优化RT-DETR模型,用于解决医学图像分割中的软边界模糊、小目标和共现现象等问题。CDFA模块通过对比度特征聚合、局部特征增强和多级注意力机制,有效提升模型性能。研究提供了三种改进方案(rtdetr-r18/y50/l-CDFA),并在多个医学数据集上验证了其有效性。核心创新包括语义信息解耦、对比驱动特征融合和尺寸感知解码器。实验结果表明,该方法在分割精度和鲁棒性方面达到SOTA水平。

Ai缝合怪、助力小伙伴高效发论文 518

[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 515

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

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

weixin_41554427的博客 1166

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

309. Best Time to Buy and Sell Stock with Cooldown Description: Say you have an array for which the  element is the price of a given stock on day i. Design an algorithm to find the maximum profit. ...

Microstrong 1216

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[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 - Best Time To Buy and Sell Stock Series (I, II, III, IV, with Cooldown)

LeetCode - Best Time to Buy and Sell Stock 系列 解题报告本文记录笔者在解LeetCode上的Best Time to Buy and Sell Stock系列时的一些想法。该系列题目共有五道,它们背景相似,但因为条件限制不同,解决思路也不太相同。I Best Time to Buy and Sell Stock https://leet

Gregzeng的博客 688

【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的博客 587

LeetCode-309. Best Time to Buy and Sell Stock with Cooldown [C++][Java]

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: After you sell your stock, you cannot buy stock on the next day

贫道绝缘子的博客 1149

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

LeetCode解题报告:309. Best Time to Buy and Sell Stock with Cooldown

Problem 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, b...

DaVinciL的博客 479

LeetCode 309 Best Time to Buy and Sell Stock with Cooldown

LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 题意 给出一串数组prices,数组中第i个元素代表第i天股票的价格,每天你可以选择购入一张股票或者售出已有股票,但有如下要求: 你不能同时参与多个交易,即当你手中有一张股票时你只能在卖出之后才能再次购入股票 售出股票后你必须等待一天才能继续购入 设计一个算法使受益最大。 思路...

EricJeffrey的博客 220

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

原题链接原题链接解题思路第一感觉就是动态规划,但是还是难以将抽象的题目具体化,如何根据前面的状态推断现在这个状态(状态转移方程)。 根据题意可以明了,总共有三个状态,持有股票,卖掉股票,休息一天,后两种都可以归纳为未持有股票。状态有了,如何推断? 再往下分析,未持有股票的状态,最大利润有两种可能。一,和昨天一样保持未持有;二,昨天持有股票今天卖掉。sdp[i] = Math.max(sdp[i-

jly0612的博客 1531

【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: 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 o

qq_16927853的博客 345

Leetcode 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 complete as many transactions as you like (ie, buy one ...

hello world 179
上一篇: 【LeetCode】解题188:Best Time to Buy and Sell Stock IV(动态规划)
下一篇: 【LeetCode】解题714:Best Time to Buy and Sell Stock with Transaction Fee(动态规划)
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值