leetcode 309.Best Time to Buy and Sell Stock with Cooldown (time : O(n))

鲁棒优化入门(一)——工具箱Xprog和RSOME的安装与使用 Xprog是由新加坡国立大学的Peng Xiong于2016年发布的一款matlab工具箱,可以用于求解确定性优化、随机优化、鲁棒优化和分布式鲁棒优化问题。 还有一个针对鲁棒随机优化设计的matlab工具箱——RSOME(Robust Stochastic Optimization Made Easy),也可以用来解决一般的线性规划、随机规划、鲁棒优化和数据驱动的分布式鲁棒优化问题。............... 阅读详情

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]

还是买卖股票的问题,但是这次不能卖了隔天就买,卖了之后要停一天才能再买入下一个。
但是如果买了,隔天是可以卖的。

思路:
前面123, 188题是用dp保存前面的profit,然后今天选择交易或不交易。

现在如果还用dp保存profit,因为买入的话要cool down一天,所以要参考dp[i - 2],但是不能确定在dp[i - 2]的时候是不是手上有股票,如果有,就不能在它的基础上再买。
所以dp[i] 不一定能在dp[i - 2]的基础上交易

同理,如果dp[i -1]的时候手上持有股票,在dp[i]的时候是可以卖的。
所以dp[i] 有时是可以在dp[i - 1]的基础上交易的

上述思路具有不确定性,所以要转换思路,因为今天能不能交易取决于前面手上是否持有股票,如果有,那么可以直接卖掉,不用 cool down一天。如果没有,就要考虑cool down一天的条件,只能参考两天前的profit。

所以定义两个dp数组,一个是持有股票的dp:hold[n], 一个是手上没有股票的dp:unhold[n]

边界条件:
hold[0] = -prices[0],
因为在0处如果持有股票,就要用prices[0]的价钱买入,profit就是-prices[0]
hold[1] = max(-prices[0], -prices[1])
因为不能同时买入两个股票,必须卖了才能买,而第二天是不能同时卖掉第一个再买入第二个的,不满足cool down,所以只能要么买prices[0], 要么买prices[1], 选其中profit较大的
unhold[0] = 0, 因为不买,没有profit

state变化:
hold[i]: hold表示持有,即买入。如果在i处不买,就保留hold[i - 1],
如果买入,那么前提是i-2处已经卖出,即unhold[i - 2]
profit = unhold[i - 2] - prices[i]
取买和不买的较大profit:max(hold[i - 1], unhold[i - 2] - prices[i])

unhold[i]: 不持有股票,即卖出。i处不卖,则保留unhold[i - 1]
i处卖掉,hold[i - 1] + prices[i], 取较大值

最后要取最大收益,也就是手上是没有股票的状态,unhold[n - 1]
(手上还有股票说明还有一部分收益没变现,肯定不是最大的)

   //1ms
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        
        int[] hold = new int[prices.length];
        int[] unhold = new int[prices.length];
        
        hold[0] = -prices[0];      
        
        for (int i = 1; i < prices.length; i++) {
            if (i == 1) {
                hold[1] = Math.max(-prices[0], -prices[1]);
            } else {
                hold[i] = Math.max(hold[i - 1], unhold[i - 2] - prices[i]);
            }
            unhold[i] = Math.max(unhold[i - 1], hold[i - 1] + prices[i]);
        }
        
        return unhold[prices.length - 1];
    }
Seq2Seq Attention模型详解 Seq2seq是一种Encoder-Decoder协同训练的端到端网络。传统Seq2seq只取用Encoder的rnn单元的最后一个隐层输出作为Decoder的输入,但由于时序串行的关系,最后一个隐层输出更关注于位于时序末尾的特征,而更弱化了位于时序前端的特征,所以这通常是“有偏”的。如今我们提到Seq2seq则常把它和Attention联系在一起,正是因为Attention采用各个rnn隐层输出的加权和,解决了“有偏”的问题。 阅读详情

相关推荐

Datawhale~数据挖掘实践之序列问题处理~天池·资金流入流出预测-挑战Baseline~Day01~数据探索与分析

写在前面✍ 本系列笔记基于天池平台上“资金流入流出预测-挑战Baseline”学习赛,记录如何完整的打一次数据挖掘类比赛。同时,该比赛属于序列建模问题,希望学习完成这个任务,可以对序列问题的处理有一定的独立思考能力和实践操作能力,可以将所学理论,用编码的形式和实际的问题得到深入。 第一篇,EDA Content: 初步了解赛题及数据 1.1 任务 1.2 用户基本信息数据 1.3 用户申购赎回数据 1.4 收益率表 1.5 银行间拆借利率表 1.6 提交格式 1.7 评分指标 EDA 1 初.

小卜妞的博客 2374

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

负雪明烛 3561

别再手动敲代码了!用STM32CubeMX+FreeRTOS图形化配置,5分钟搞定多任务通信框架

本文详细介绍了如何利用STM32CubeMX和FreeRTOS快速搭建多任务通信框架,大幅提升嵌入式开发效率。通过图形化配置工具,开发者可在5分钟内完成任务调度、时钟配置和内核对象生成,特别适合物联网设备开发。文章还提供了代码优化和调试技巧,帮助开发者轻松应对复杂应用场景。

weixin_30363285的博客 299

[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

【虚幻引擎UE】UE5 四步骤实现AI漫游与对话行为(含工程源码)

【虚幻引擎UE】UE5 四步骤实现AI漫游与对话行为(含工程源码)

灵境引路人 3241

【NodeRed】node-red-contrib-opcua实现PLC读写

在之前的博文【Node-RED】node-red-contrib-opcua-server模块使用(1)中,我们介绍了用node-red-contrib-opcua-server模块实现OPCUA服务端的,以及二次开发的内容。本节博文中我们采用官方社区自带的node-red-contrib-opcua组件开展对OPCUA客户端的介绍,详细阐述用该组件中OpcUa-Client节点实现客户端对PLC的读写,以及实现过程中遇到的问题。由于官方社区中并没有找到该组件的介绍实例,这也是本博文编写的第1个原因。

weixin_48618536的博客 968

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

基于STM32设计的数字电子秤

当前项目是采用采用STM32+称重模块+OLED实现了简单的电子秤项目,称重模块上采用24位的ADC芯片,精度较高。实现了称重,校准、去皮等功能。

1万+

[刷题]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的博客 312

夯爆了!这两组AI漫剧校园风人物三视图(附详细提示词)

生成一张高清8K图像,图像主体是一位亚洲年轻女性,拥有长直黑发和精致的五官,她穿着一套由白色衬衫、深红色领带、深蓝色牛仔外套和深蓝色牛仔百褶短裙组成的制服,搭配灰色长筒袜和黑色厚底皮鞋。图像由四部分组成,左一为女性特写头像,面部表情自然,眼神柔和,右侧三张图分别展示了女性的正面、侧面和背面全身像,整体构图均衡,遵循三分法则。图像整体呈现出一种类似于3D渲染的摄影风格,背景为纯白色,采用高对比度照明,强调主体细节和质感,色彩调色板以深蓝色和白色为主,整体色调清新自然,注重质感和光影效果的呈现。

欢迎来到鸾姝淡月博客,专为程序员和编程爱好者打造的技术博客。我们分享编程语言趋势、开发工具、编程哲学、职业发展等,助你掌握新技术,优化技能,激发编程热情。 243

7、农业市场价格预测与K-Means聚类算法的综合研究

本文综合研究了农业市场价格预测与K-Means聚类算法的原理、应用及拓展。在农业领域,基于历史数据和供应信息的价格预测模型可为农民、政府和金融机构提供决策支持;在数据处理领域,K-Means算法通过合理选择初始质心和距离度量方法,实现高效聚类分析。文章还探讨了两种技术面临的挑战与解决方案,并展望了其在大数据、人工智能融合背景下的未来发展趋势,展示了跨领域技术协同的应用潜力。

ujm56789012的博客 79

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

全覆盖路径规划开源项目Clean-robot-turtlebot3关键函数解析(上)

本系列文章主要介绍全覆盖路径规划开源项目Clean-robot-turtlebot3的相关内容,包含如下四篇文章,分别介绍了开源项目Clean-robot-turtlebot3流程的概括总结、ROS坐标系常用坐标及其变换、Clean-robot-turtlebot3关键函数解析等内容。

慕羽★的博客 2784

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

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

weixin_41554427的博客 1166

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

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

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

309. Best Time to Buy and Sell Stock with Cooldown

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 1. 思路 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75928/Share-my-DP-solution-(B...

scut_salmon的博客 251

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的博客 145

算法: 冷却时间买卖股票的最佳时机309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown You are given an array prices where prices[i] is the price of a given stock on the ith day. Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sel

AI架构师易筋 573

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

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

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

贫道绝缘子的博客 1150

leetcode -- Best Time to Buy and Sell Stock with Cooldown -- 不理解

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

xyqzki的专栏 4302

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的博客 375

leetcode 309 Best Time to Buy and Sell Stock with Cooldown 详细解答

解法1 参照leetcode 122, 解法几乎一样 但这里需要三个数组, sell: 每次卖出股票后自身的收益 buy: 每次买入股票后自身的收益 cooldown: 冷冻股票后自身的收益 根据题意很明显:cooldown[i] = sell[i-1] 因为题目说在卖出之后需要一个冷冻期 sell[i] = max( sell[i-1], buy[i-1] + prices[i] ) buy[i] = max( buy[i-1], cooldown[i-1] - prices[i] ) 代码如下: .

qq_22210659的博客 234
上一篇: leetcode 304. Range Sum Query 2D - Immutable
下一篇: leetcode 312. Burst Balloons
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址