leetcode.Best Time to Buy and Sell Stock with Cooldown

LeetCode github集合,附CMU大神整理笔记 Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步。github目前同步的题目是2020-09-17日之后写的题。之前写过的题会陆续跟新到github中。 目前大概280个题目 Github 项目链接:https://github.com/sunliancheng/leetcode_github ... 阅读详情

这里都用了状态机,需要注意的是状态机表示当前状态下的最大收益

a typical DP problem

key1 :   deduce the transition function .

(buy,sell,rest) represents before day i what is the max profit  when the  sequence end with (buy, sell ,rest)

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

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

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

key2: simplifize the function 

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

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

key3: initialize i = 0

buy[0] = max(INT_MIN, -price[0])

sell[0] = 0;

here is the code

int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if(len == 0) return 0;
        vector<int> buy(len);
        vector<int> sell(len);
        buy[0] = -prices[0];
        sell[0] = 0;
        for(int i = 1; i < len ; i++){
            if(i == 1){
                buy[1] = max(-prices[1],buy[0]);
            }else{
              buy[i] = max(sell[i-2]-prices[i],buy[i-1]);
            }
              sell[i] = max(buy[i-1]+prices[i],sell[i-1]);
        }
        return sell[len-1];
    }

714. Best Time to Buy and Sell Stock with Transaction Fee

一样的dp思想,需要注意的是一次买入再卖出才算一次transaction

int maxProfit(vector<int>& prices, int fee) {
        int len = prices.size();
        if(len == 0) return 0;
        vector<int> sell(len);
        vector<int> buy(len);
        buy[0] = -prices[0];
        sell[0] = 0;
        for(int i = 1; i < len ;i++){
            buy[i] = max(buy[i-1], sell[i-1]-prices[i]); //买入的时候不需要付费
            sell[i] = max(sell[i-1], buy[i-1]+prices[i] -fee);
        }
        return sell[len-1];
    }

123. Best Time to Buy and Sell Stock III

int maxProfit(vector<int>& prices, int fee) {

       if(prices.size() == 0) return 0;
       int s1 = -prices[0], s2 = INT_MIN, s3 = INT_MIN,s4 = INT_MIN;
       for(int i = 0; i < prices.size(); i++){
            s1 = max(s1, -prices[i]);
            s2 = max(s2, s1+prices[i]);
            s3 = max(s3, s2-prices[i]);
            s4 = max(s4, s3+prices[i]);
       }
       return max(0,s4);
    }

122. Best Time to Buy and Sell Stock II

int maxProfit(vector<int>& prices, int fee) {
        int len = prices.size();
        if(len == 0) return 0;
        vector<int> sell(len);
        vector<int> buy(len);
        buy[0] = -prices[0];
        sell[0] = 0;
        for(int i = 1; i < len ;i++){
            buy[i] = max(buy[i-1], sell[i-1]-prices[i]); 
            sell[i] = max(sell[i-1], buy[i-1]+prices[i]);
        }
        return sell[len-1];
    }

 

别再凭感觉选电容了!手把手教你计算电机控制器里的母线电容(附Excel计算工具) 本文详细解析了电机控制器中母线电容的选型方法,从理论计算到工程实践,帮助工程师避免常见误区。通过精确计算纹波电压、电容容量等关键参数,并结合薄膜电容与电解电容的性能对比,提供实用的选型建议。附带的Excel计算工具可快速完成参数计算,适用于工业伺服驱动和电动车电机控制器设计。 阅读详情

相关推荐

OpenEarthMap 与 xBD 数据集合并

整合好的数据:链接: https://pan.baidu.com/s/1uJ_Zg6n-s3u1D49OW52D7Q?pwd=2vwx 提取码: 2vwx完整整合步骤与代码如下。

qq_60293814的博客 1597

LeetCode2.2.2(Reverse Linked List II)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->nullptr, m = 2 and n = 4, return 1->4->3->2->5->nullptr. Note: Given m, n satisfy the

杨煜冬煜杨的博客 1272

npu-smi info 华为昇腾NPU 状态监控工具解读

用于查询所有设备的基本信息。

Lixora's DB Home 2488

工具推荐--刷LeetCode的神器

本文首发于微信公众号:【坂本先生】,文章地址为: https://mp.weixin.qq.com/s/vHv5hO8nils_g2VSKwu1Cg如有转载请标明出处 今天给大家安利一款快速刷LeetCode的工具,能够让你专注于题目本身,而不是如何去建立题目。这个工具是基于IDEA的,名叫LeetCode Editor,它的官方GitHub地址为:https://github.com/...

风过留声 1853

ADS软件操作(8)——负载牵引(Loadpull)

负载牵引:通过不断调节输入和输出端的阻抗,找到让有源器件输出功率最大的输入、输出匹配阻抗。这种方法可以准确地测量出器件在大信号条件下的最优性能,反映出器件输入、输出阻抗随频率喝输入功率变化的特性,为器件和电路的设计优化提供了坚实的基础。白话版:由于通信制式越来越复杂,对放大器的线性度和效率要求越来越高。由于放大器的效率和是个永恒的矛盾,所以如何平衡这样的矛盾达到系统设计的最优就是一个需要解决的难题。如何解决?调节输入和输出端的阻抗也就是负载牵引 (Load-Pull)原理來改善点,从而降低谐波的。

Zhuanzhuyan的博客 4954

Discrete Optimization (Coursera) 学习笔记 Week 1 introduction (开个帖子)

Week 1 Introduction 这位教授Pascal Van Hentenryck 真的算是优化领域神级别的人物了吧?截一张Google scholar的图大家感受一下… 居然在Coursera上看到了他开的课:Discrete Optimization。试听了几节后,发现讲得很有激情,而且深入浅出。遂决定花暑假跟完这一门课。 这门课每周花两三个小时介绍基础概念,然后得花十几个小时写作业(coding)… 一开始以为Coursera上的课的作业应该不会太难,然后第一次作业(knapsack)我就被

ZJ_11701的博客 658

LeetCode 解题笔记终于开放下载了,覆盖字节、蚂蚁、腾讯多家大厂真题(附5000页大厂试题)...

点击“开发者技术前线”,选择“星标????”在看|星标|留言, 真爱今天给大家分享2套手册。先看看一大牛整理了一套初学到进阶的 LeetCode学习资料,分享一个火爆 GitHub 的...

开发者技术前线-DevolperFront 442

[AI工具评测] 为什么功能全面的 Kimi Claw,最终还是被放弃?

《KimiClaw:功能整合与体验落差的AI工具实践》 KimiClaw作为集LLM、Agent、云端执行和SaaS分发于一体的AI工具,理论上具备个人开发者所需的全栈能力:原生整合大模型与Agent、支持云端Docker部署、可生成Web应用并通过飞书协作。然而实际高频使用中暴露三大核心问题:响应延迟导致思考节奏断裂(非流式输出)、云端黑箱执行缺乏可控性(无法实时干预)、系统稳定性不足(网关和隧道偶发故障)。这些体验落差表明,功能完整性不等于使用高效性——对开发者而言,实时响应、执行透明度和系统可靠性比自

技术小甜甜:传播可落地技术,帮助个人和企业降低解决真实场景问题的成本 536

算法统治者,打破传统方式,GitHub爆火的Leetcode刷题指南来袭

阿里巴巴内部算法学习指南1.算法思想排序贪心DP/动态规划剪枝尺取法2.数据结构图搜索树线型3.大厂算法真题使用LeetCode的目的不是硬记题目,而是学习不同的解题思路。如果每道题⾃⼰都能优化到 100% 了,那么⼀段时间以后进步会很⼤。所以总的来说,实在没思路,看解题思路;实在优化不到 100%,看看代码。有了这份文档的话,对于那些想要通过LeetCode提高算法能力的编程爱好者是一个莫大的福音。如果是学习算法、数据结构、或者是刷题找工作的小伙伴,一定不要错过啦!

知识分享官 243

C语言使用开源库libusb进行USB设备操作

https://libusb.info/libusb is a C library that provides generic access to USB devices. It is intended to be used by developers to facilitate the production of applications that communicate with USB hardware. It is portable: Using a single cross-platform AP

m0_60352504的博客 1555

SAP Fiori Launchpad 上看不到任何 tile 应该怎么办?

中心UI组件是一个框架,为所有SAPFiori应用程序提供公共基础设施SAPFiorilaunchpad是所有SAPFioriUI的基础,并为SAPFiori应用程序提供基本功能,如登录,界面大小,应用程序之间的导航,和基于角色的应用程序目录。ABAP前端服务器包含所有基础设施组件,用于为客户端生成SAPFiori应用程序特定的UI,并与SAPBusinessSuite后端系统通信。SAPFiori应用程序的处理过程如下首先,客户端加载SAPFiori应用程序的界面。...

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

【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

mjlab & MimicKit 宇树 G1 人形机器人强化学习训练部署

基于mjlab框架训练Unitree G1人形机器人执行双旋踢的示例。参考动作源自Jason Peng的MimicKit项目。数据转换脚本(MimicKit pkl → mjlab csv)完整训练结果与超参数预训练ONNX检查点用于部署实体硬件部署指南⚠️ 免责声明仅用于教育目的。对于因尝试复现此处展示结果而导致的财产损失或人身伤害,我们概不负责。操作硬件时请谨慎行事并保持良好判断力。

2506_90492529的博客 1700

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

假人有限元模型

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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

【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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

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

懂技术的猫 674

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

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

[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

LeetCode:Best Time to Buy and Sell Stock with Cooldown

LeetCode:Best Time to Buy and Sell Stock with Cooldown

Stella_Chan的博客 368

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

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

这道题用的是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

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 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
上一篇: python numpy的一些用法
下一篇: leetcode : same tree or symmetric tree
kotomi_du
博客等级 码龄10年 5粉丝 91原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值