309. Best Time to Buy and Sell Stock with Cooldown

金蝶个版本下载地址,记得保存收藏。 专业版 V10.0 http://downloads.cmcloud.cn/kis/kis_pro/10.0/kiszybV10sp1.rar V11.0 http://downloads.cmcloud.cn/kis/kis_pro/11.0/KISV11Setup.rar V12.0 http://dow... 阅读详情

题目:

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:

prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]

题目大意:

给定一个数组,第i个元素代表某只股票在第i天的价格。

设计一个算法计算最大收益。你可以完成多次交易(亦即,多次买入、卖出同一只股票),需要满足下列限制:

你不可以在同一时间参与多个交易(亦即,在买入股票之前必须卖出)。
在卖出股票之后,你不可以在第二天马上买入。(亦即,需要一天的冷却(CD)时间)
测试用例见题目描述。

解题思路:

动态规划(Dynamic Programming)

时间复杂度:O(n)

本题与Best Time to Buy and Sell Stock II唯一的区别是在卖出股票后需要间隔至少一天才能再次买入。

对一天的状态有:buy买入,sell卖出,cooldown冷却。

但是对于这一天是否持股只有两种状态:持股状态(buy),没有持股状态(sell,cooldown)。

对于当天持股状态时,至当天的为止的最大利润有两种可能:1、今天没有买入,跟昨天持股状态一样;2、今天买入,昨天是冷却期,利润是前天卖出股票时候得到的利润减去今天股票的价钱。 二者取最大值。

对于当天未持股状态,至当天为止的最大利润有两种可能:1、今天没有卖出,跟昨天未持股状态一样;2、昨天持有股票,今天卖出了,利润是昨天持有股票时候的利润加上今天股票的价钱。 二者取最大值。

直至最后一天的状态应该是卖出状态。最终利润为sell[n-1];

状态转移方程:

sell[i] = max(sell[i-1], buy[i-1] + price[i]);

buy[i] = max(buy[i-1], sell[i-2] - price[i]);

/*
 另一种思路,首先用3个辅助数组表示buy,sell,rest
 buy[i]表示在第i天之前最后一个操作是买,此时的最大收益, 即第i天为买。

 sell[i]表示在第i天之前最后一个操作是卖,此时的最大收益。

 rest[i]表示在第i天之前最后一个操作是冷冻期,此时的最大收益。

 我们写出递推式为:

 buy[i]  = max(rest[i-1] - price, buy[i-1])
 sell[i] = max(buy[i-1] + price, sell[i-1])
 rest[i] = max(sell[i-1], buy[i-1], rest[i-1])

 上述递推式很好的表示了在买之前有冷冻期,买之前要卖掉之前的股票。
 一个小技巧是如何保证[buy, rest, buy]的情况不会出现,这是由于buy[i] <= rest[i], 即rest[i] = max(sell[i-1], rest[i-1]),
 即冷冻期前面只可能是冷冻期或者卖,绝对不会出现买!这保证了[buy, rest, buy]不会出现。

 另外,由于冷冻期的存在,我们可以得出rest[i] = sell[i-1],这样,我们可以将上面三个递推式精简到两个:

 buy[i]  = max(sell[i-2] - price, buy[i-1])
 sell[i] = max(buy[i-1] + price, sell[i-1])
*/
 public static int maxProfitON(int[] prices)
    {
        int size = prices.length;
        int[] buy = new int[size];
        int[] sell = new int[size];
        if (size < 2)
            return 0;

        buy[0] = -prices[0];
        buy[1] = Math.max(buy[0],-prices[1]);
        sell[0] = 0;
        sell[1] = Math.max(sell[0],buy[0]+prices[1]);

        //buy[i]  = max(rest[i-1]-price, buy[i-1])
        //sell[i] = max(buy[i-1]+price, sell[i-1])
        //根据最初始的状态转移方程,先写出来初始化

        for (int i = 2 ; i < prices.length ; i ++)
        {
            buy[i] = Math.max(buy[i-1],sell[i-2]-prices[i]);
            sell[i] = Math.max(sell[i-1],buy[i-1] + prices[i]);
        }

        return sell[size-1];

    }
DSP复习笔记(5)——外部接口模块XINTF 【注意】 1. 芯片型号:TMS320F28335 2. 适用于NJUSTEO李彧晟老师课程 3. 参考资料:《TMS320F28335 DSP原理、开发及应用》 4. 对应PPT第二章 5. 本章内容较多而且较为重要,请结合PPT一起复习 阅读详情

相关推荐

Cygwin安装教程

简介 cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件 Cygwin就是一个windows软件,该软件就是在windows上仿真linux操作系统 简言之,cygwin是一个在windows平台上运行的 linux模拟环境,使用一个Dll(动态链接库)来实现 这样,我们可以开发出Cygwin下的UNIX工具,使用这个DLL运行在W...

Errol's Blog 13万+

[刷题]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

300Kg载重履带式无人车底盘定制开发技术详解

通过精准的需求分析、科学的方案设计、严格的研发与测试以及完善的交付与培训流程,可以打造出符合客户需求的优质产品。300Kg载重履带式无人车底盘的定制开发,旨在打造一款承载能力强、行驶稳定、适应复杂地形的无人运输平台。根据应用场景选择合适的动力源,并进行性能优化,确保无人车在载重情况下仍能保持稳定的行驶速度和动力输出。根据需求分析结果,设计无人车底盘的总体方案,包括动力系统选择、结构设计、负载管理策略等。进行详细的研发工作,包括零部件的选型与采购、样机的制作与调试等。

专注多旋翼、复合翼无人机技术研究,从事低空经济行业。 960

[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

统信UOS系统网络服务异常排查:WIFI图标消失与网络设置缺失的解决方案

本文详细解析了统信UOS系统WIFI图标消失与网络设置缺失的常见问题,提供了从基础修复到进阶排查的完整解决方案。重点分析了NetworkManager服务异常的根本原因,包括系统更新中断、权限配置错误等,并给出实用的命令行操作指南和预防措施,帮助用户快速恢复网络功能。

weixin_29234239的博客 622

【Boost5】Boost变换器的输入电容

节点的电流有3个,一个是来自电源输入的,由于我们把Vin看成理想电源以及线路的电感的存在,它可以近似看作是恒定的电流源Iin,一个是流向输入电容Cin的电流Icin,另外一个节点是电感L电流IL。电容电流波形在整个开关周期内的平均值为0,充电和放电在一个周期中各占1/2,在图中充电部分面积和放电部分面积相等,那么在充电或放电的电荷量Q=I*t,从图中可以看出,在一个周期内电容电流变化量是ΔIL,那在电容在充电1/2T周期中电流的变化量是1/2ΔIL。输入电容上的ESR也会随着电流变化,影响输入电压的变化。

qq_43416206的博客 3351

Day 51 | 309. Best Time to Buy and Sell Stock with Cooldown | 714. with Transaction Fee

Day 51 | 309. Best Time to Buy and Sell Stock with Cooldown | 714. with Transaction Fee

KKKKKKyle的博客 144

深入解析LinuxCNC源码:从界面开发到硬件交互

本文深入解析LinuxCNC开源数控系统的源码架构与二次开发实践。详细介绍了从GTK+界面定制、Python脚本绑定,到硬件抽象层(HAL)配置与核心运动控制模块的完整开发流程。通过实战案例,帮助开发者掌握如何基于LinuxCNC源码进行界面开发和硬件交互定制,实现个性化数控系统。

weixin_29164843的博客 542

#309 Best Time to Buy and Sell Stock with Cooldown

Description You are given an array prices where prices[i] is the price of a given stock on the ithi^{th}ith day. 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 multi

YY_Tina的博客 170

【声纳与人工智能融合——从理论前沿到自主系统实战】第一章 声纳AI的数据基础:仿真、数据集与表征学习

该数据集的特点是包含了多种底质背景(沙地、泥地、碎石区)与噪声条件,适合验证模型的背景抑制能力。Urashima数据集由日本海洋研究开发机构(JAMSTEC)发布,基于自主水下航行器(AUV)采集,包含前视声纳序列数据,主要用于水下导航与避障算法评估,其数据包含了精确的姿态与深度标注,支持三维场景重建算法的训练。北约海洋研究与实验中心(CMRE)发布的CMRE datasets包含侧扫与合成孔径声纳数据,特别适用于跨传感器域自适应研究,数据集中包含了同一区域的多种声纳成像结果,为跨模态学习提供了基准。

u012133341的博客 104

点云从入门到精通技术详解100篇-点云的特征检测(续)

对每个维度的序列分别计算 DTW 距离,最后。权重将距离限制在较小的范围内,对于时间跨度大的数据集,过度限制了点对点的路径,同的,因此不能用欧氏距离来计算两个多元时间序列之间的距离,本文选用的是动态时。到的股票数据是未知分类的,因此该数据的相似性分析缺少有效的评价方式,通常需要。时间序列,简而言之,就是与时间相关的数据的集合,应用在生活工作中的方方面。时间序列的分段线性化表示的难点在于如何找出分段点,目前,对于分段点的研究。经过分段线性化之后,每个多元时间序列的维度是相同的,但是每一维的长度是不。

getusushu的博客 334

【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 1214

30天入门 SAP BTP 开发系列之八:动手在 SAP BTP 上开发第一个 ABAP 透明表

ABAP Cloud 环境下创建透明表的方法与数据迁移实践 本文介绍了在 SAP BTP ABAP 环境中创建透明表并迁移数据的完整流程。与传统的 SE11 事务码不同,ABAP Cloud 需通过 ABAP Development Tool 创建数据库表,使用代码定义表结构。作者详细演示了如何: 在开发包中创建名为 Z优快云_TRAVEL 的透明表 使用 ABAP 代码定义表字段和约束 通过 ABAP 类实现数据从标准表/DMO/TRAVEL 到新建表的迁移 验证数据迁移结果 文中还提供了相关技术概念的

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1789

【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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

WINCC7.3破解文件

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

309. Best Time to Buy and Sell Stock with Cooldown 714. Best Time to Buy and Sell Stock with Transa

【代码】309. Best Time to Buy and Sell Stock with Cooldown 714. Best Time to Buy and Sell Stock with Transa。

Fai_B的博客 1034

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

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

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

weixin_41554427的博客 1165

309 Best Time to Buy and Sell Stock with Cooldown

1 题目 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, bu...

weixin_39145266的博客 181

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

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

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

珍妮的选择的博客 327

【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

分析 难度 中 来源 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题目 Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm ...

ov流岚vo的专栏 305
上一篇: Spring中Bean及@Bean的理解
下一篇: Oracle和Mysql的区别
minGW_Lee
博客等级 码龄11年 19粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值