LeetCode -Best Time to Buy and Sell Stock with Cooldown

Dify Rerank 模型配置全流程解析:从报错定位到代码实现 阅读详情

LeetCode -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 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天的价格。 如果某天卖了股票,那么第二天不能买股票,有一天的冷冻期。

算法解释

状态机想法(discuss 大神)
这里写图片描述

其中

s0[i] = max(s0[i - 1], s2[i - 1]); // Stay at s0, or rest from s2
s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); // Stay at s1, or buy from s0
s2[i] = s1[i - 1] + prices[i]; // Only one way from s1


s0[0] = 0; // At the start, you don’t have any stock if you just rest
s1[0] = -prices[0]; // After buy, you should have -prices[0] profit. Be positive!
s2[0] = 0;


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

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

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


代码

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]);
    }
};

对意思进行理解优化后

int maxProfit(vector<int>& prices) {
        if (prices.size() < 2) return 0;
        int rest = 0, buy = -prices[0], sell = 0;
        for (int i = 1; i < prices.size(); ++i) {
            int last_sell = sell;
            sell = buy + prices[i];
            buy = max(rest - prices[i], buy);
            rest = max(rest, last_sell);
        }
        return max(rest, sell);
    }
华为od统一考试B卷【单词搜索】C++ 实现 找到它是一个小游戏,你需要在一个矩阵中找到给定的单词。假设给定单词 HELLOWORD,在矩阵中只要能找到 H->E->L->L->O->W->O->R->L->D连成的单词,就算通过。意区分英文字母大小写,并且您只能上下左右行走,不能走回头路。 阅读详情

相关推荐

OPC UA 服务端的模拟工具及客户端工具

详细介绍如何使用 Prosys OPC UA Simulation Server 作为 OPC UA 服务端的模拟工具来生成数据模型,并使用 Softing OPC UA Client 作为客户端工具,直观地查看和验证模拟生成的数据。

weixin_44462773的博客 9857

【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

正交试验实战指南:从PICT工具到混合水平表应用

本文详细介绍了正交试验法在测试设计中的实战应用,从基础概念到PICT工具的高级使用技巧。通过混合水平表设计和工程化实践案例,帮助测试工程师高效解决参数组合爆炸问题,提升测试覆盖率与效率。特别适合兼容性测试和复杂系统验证场景。

weixin_29230865的博客 259

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

VS Code配置Java开发环境避坑指南:从JDK到Spring Boot插件全流程

本文详细介绍了在VS Code中配置Java开发环境的完整流程,涵盖JDK选择、环境变量配置、核心插件安装、构建工具优化及Spring Boot项目专项设置。特别针对常见问题提供避坑方案,帮助开发者快速搭建高效稳定的开发环境,提升Java和Spring Boot开发效率。

weixin_28839699的博客 977

速腾聚创激光雷达复现FAST-LIO

记录复现FAST-LIO算法的过程和,代码梳理和理解。FAST-LIO的简化版、注释版。速腾激光雷达

abanchao的博客 2928

【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

如何运用好DeepSeek为自己服务:智能增强的范式革命 || 目录

1.1 认知增强的三次浪潮1.2 DeepSeek认知增强模型1.3 人机认知耦合协议1.4 认知增强的数学基础。

𝓓𝓸𝓶𝓲𝓷𝓪𝓽𝓲𝓷𝓰 𝓐𝓵𝓵 𝓣𝓱𝓲𝓷𝓰𝓼 1220

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

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

懂技术的猫 674

汽车零部件智能装配:C#+YOLOv8实现螺栓漏装检测与齿轮错位识别(附PLC通信与机械臂控制)

YOLOv8:小目标检测的最优解相比YOLOv7,YOLOv8的C2f特征融合网络对小目标(5-10mm螺栓)检测精度提升12%,且推理速度快8%;支持大输入尺寸(不显著增加耗时),可放大螺栓细节;官方提供yolov8n.pt(nano版),在边缘计算盒上能跑40+FPS,满足实时性。C#:工业软件的“兼容性核心”主流PLC(西门子S7-1200/1500)、机械臂(ABB、KUKA)的SDK多基于.NET开发,C#可直接调用,避免跨语言通信延迟;多线程管理成熟(Task。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1146

回归预测 | MATLAB实现SVR(支持向量机回归)K折交叉验证

回归预测 | MATLAB实现SVR(支持向量机回归)K折交叉验证 目录回归预测 | MATLAB实现SVR(支持向量机回归)K折交叉验证基本介绍划分原理留一划分交叉验证K折交叉数据下载程序设计参考资料致谢 基本介绍 在机器学习里,通常来说我们不能将全部用于数据训练模型,否则我们将没有数据集对该模型进行验证,从而评估我们的模型的预测效果。为了解决这一问题,有如下常用的方法: 第一种是最简单的,也是很容易就想到的。我们可以把整个数据集分成两部分,一部分用于训练,一部分用于验证,这也就是我们经常提到的训练集

机器学习之心的博客,关注并私信文章链接,获取对应文章源码和数据。 6375

[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

基于遗传算法改进BP神经网络的电池容量SOC预测

,遗传算法BP神经网络能很好的实现对电池容量soc的训练和预测,BP神经网络是一种成熟的神经,相对于其他神经网络,拥有很多的训练函数,传递函数,可以调节的参数非常多,对各种问题都可以达到一个比较理想的效果,关键看如何调试参数,选择训练传递函数,遗传算法改进BP神经网络的初始权值阈值,达到改进BP神经网络的目的,从而更好的实现对电池容量SOC的预测

abc991835105的博客 765

LeetCode:Best Time to Buy and Sell Stock with Cooldown

LeetCode:Best Time to Buy and Sell Stock with Cooldown

Stella_Chan的博客 367

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

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

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

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

【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

精雕软件(JDpaint) 5.5 绿色版

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

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

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

weixin_41554427的博客 1165

【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(Medium)解题报告

【LeetCode】309.Best Time to Buy and Sell Stock with Cooldown(Medium)解题报告题目地址:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/ 题目描述:   Say you have an array for

郝春雨的博客 325

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

miss_snow_m的专栏 477

leetcode笔记--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 lik

qingyujean的博客 792

Best Time to Buy and Sell Stock with Cooldown_LeetCode

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

weixin_30664615的博客 100

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

题目: 思路: 抄作业,看的博文https://blog.youkuaiyun.com/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 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
上一篇: LeetCode--Add two Number II
下一篇: LeetCode- Best Time to Buy and Sell Stock
Coder_zyq
博客等级 码龄10年 3粉丝 23原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值