LeetCode123. Best Time to Buy and Sell Stock III

保姆级教程:在Android车机上从零配置USB Gadget,搞定CarPlay有线连接 本文详细介绍了在Android车机上从零配置USB Gadget以实现CarPlay有线连接的完整实践指南。涵盖内核驱动开发、Android系统配置及用户空间通信架构,帮助开发者深入理解USB子系统和iAP2协议栈,实现稳定可靠的CarPlay有线连接功能。 阅读详情
题目链接:

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

题目描述:

用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。最多交易两次,手上最多只能持有一支股票,求最大收益。

思路:

最多交易两次。可以以i天为分界线,一个记录以prices[i]为结尾的最大收益(即i天之前的最大收益,从前往后),一个记录以prices[i]为开头的最大收益(i天之后的最大收益,从后往前)。
preProfit[i]+postProfit[i]为总的最大收益。比较最大收益找出最大值。

代码:
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len=prices.size();
        if(len==0){
            return 0;
        }
        int* preProfit=new int [len];
        int* postProfit=new int [len];
        int minPrice=prices[0];
        preProfit[0]=0;
        for(int i=1;i<len;i++){
            minPrice=min(minPrice,prices[i]);
            preProfit[i]=max(preProfit[i-1],prices[i]-minPrice);
        }
        int maxPrice=prices[len-1];
        postProfit[len-1]=0;
        for(int i=len-2;i>=0;i--){
            maxPrice=max(maxPrice,prices[i]);
            postProfit[i]=max(postProfit[i+1],maxPrice-prices[i]);
        }
        int maxRes=preProfit[0]+postProfit[0];
        for(int i=1;i<len;i++){
            if(maxRes<preProfit[i]+postProfit[i]){
                maxRes=preProfit[i]+postProfit[i];
            }
        }
        return maxRes;
    }
};
RK3588 EVB开发板原理图讲解【六】 该原理图展示了eMMC Flash与主控芯片之间的连接关系,以及相关的电源、信号线路和去耦电容等。 阅读详情

相关推荐

大语言模型(LLM)在组合优化中的新突破:LLM-LNS与FunSearch性能对比

本文深入探讨了大语言模型(LLM)在组合优化中的创新应用,重点对比了LLM-LNS与FunSearch两种方法的性能差异。研究表明,LLM-LNS在物流路径规划、资源调度等场景中展现出显著优势,其双层自演化架构能有效平衡探索与利用,相比传统方法提升效率约43%。文章还提供了实践选择指南,帮助开发者根据具体需求选用合适的技术方案。

weixin_42526415的博客 405

【LeetCode】解题123:Best Time to Buy and Sell Stock III(动态规划)

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 491

银河麒麟鲲鹏aarch64架构安装docker和K8s

鲲鹏云服务器上安装docker-云社区-华为云 (huaweicloud.com) 这里我下载的版本是docker-18.09.3.tag docker 安装包地址:https://download.docker.com/linux/static/stable 配置system服务 在github对应分支的contrib/init/systemd/ 下面,提供了对应的docker.service以及docker.socket文件,可以直接拷贝到本地文件使用: 对应链接:https://github.c

qq_50007696的博客 9056

LeetCode123—Best Time to Buy and Sell Stock III

LeetCode123—Best Time to Buy and Sell Stock III

上善若水 1320

SillyTavern 向量存储配置踩坑记:从卡死到本地部署 Ollama 跑通

用此命令看到镜像就算成功。

qq_62483451的博客 1259

WRF-Chem新手避坑指南:从chem_opt到emiss_opt,那些手册里没明说的参数配置实战

本文为WRF-Chem新手提供从chem_opt到emiss_opt的参数配置实战指南,解析官方文档中未明确的常见陷阱与解决方案。通过华北重霾案例,详细讲解化学机制选择、排放源配置及特殊参数组合的注意事项,帮助用户避开常见错误,提升模拟效率。

weixin_30897079的博客 278

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

题目来源:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 问题描述 123.Best Time to Buy and Sell Stock III Hard Say you have an array for which theithelement is the price of a given st...

da_kao_la的博客 342

科研党必备:用Python+Excel批量下载文献的保姆级教程(附常见错误解决)

本文提供了一套利用Python和Excel实现外文文献批量下载的自动化解决方案。教程详细介绍了如何从数据库导出并清洗DOI信息,编写健壮的Python脚本进行批量下载,并包含错误处理与进阶优化技巧,旨在帮助科研人员将重复性劳动自动化,大幅提升文献收集效率。

weixin_29050513的博客 305

leetcode 123. Best Time to Buy and Sell Stock III

/二是第i天买入了第二只股票(需支付prices[i]),第i天能买入第二只股票的前提是第i-1天处于已经卖出第一只股票但还没买入第二只股票的状态(对应dp[i-1][2])//二是第i天第一次买入股票(需支付prices[i]),第i天能第一次买入股票的前提是前一天(第i-1天)处于没有买过股票的状态(对应状态dp[i-1][0])//二是第i天卖出了第一只股票(收入prices[i]),第i天能卖出第一只股票的前提是第i-1天处于持有第一只股票的状态(对应dp[i-1][1])

weixin_41554427的博客 1378

元宇宙通证- 九、IT/ICT发展史全景长图

九、IT/ICT发展史全景长图 (一)综述 IT是Information Technology的缩写,意为”信息技术“,包含现代计算机、网络、通信等信息领域技术。自1844年人类历史上发出来第一份电报,实现了信息的长图传输,信息技术发展的历史大幕随之拉开。信息技术的普遍应用,是进入信息社会的标志。 IT技术最早发源于半导体,然后发展处计算机、通信网和互联网,进而演进到现在的AI、XR、计算机、大数据、物联网、工业互联网、区块链、量子计算等。 有人说元宇宙是下一代互联网,有人说元宇宙是互联网的终极形态。 我们

沧海之巅的专栏 1万+

【WSN定位】基于AOA算法实现目标无源定位附matlab代码

1 简介 2 部分代码 clear all; close all; clc;​stations = [0 0 0;20 0 0]; %基站位置​X = -100:100; %生成目标位置Y = X.*sin(X);Z = X+Y;N = length(X);Locations = zeros(N,3);angs = zeros(N,4);for i = 1:N angs(i,:) = Angs(stations*1e3,[X(i),Y(i),Z(i)]*1e3);end​for

qq_59747472的博客 2308

Leetcode 123. Best Time to Buy and Sell Stock III 股票买卖3 解题报告

1 解题思想嗯,是的,已经第三题了,数组代表什么我不想再复述一次,自己看: Leetcode 122. Best Time to Buy and Sell Stock II 股票买卖2 解题报告 Leetcode 121. Best Time to Buy and Sell Stock 股票买卖 解题报告这道题的难点或者说同点在于: 只允许买卖两次这道题本质上用的是第一题的方法,我也看了下Di

MebiuW的专栏 3456

假人有限元模型

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

LeetCode123:Best Time to Buy and Sell Stock III

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 at most two transactions. Note: You ma

3386

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

精雕软件(JDpaint) 5.5 绿色版

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

LeetCode力扣之123. Best Time to Buy and Sell Stock III

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 at most two transactions.Note:You may not engage...

LeeWei 304

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

leetcode123 Best Time to Buy and Sell Stock III

题目: 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 at most two transactions. Note:

每天进步一点点 2041

LeetCode 123:Best Time to Buy and Sell Stock III 三维DP完全解析

这 4 个状态可以理解为把三维 dp[i][j][k] 中真正会被用到的那几类状态抽出来,省去了显式的 i 维和 j 维,从而把空间压缩到 O(1)。当 j=2 时,j+1=3>K,就没有「还能卖 3 次」这个状态,所以不能通过「卖出」到达 dp[i][2][0],只能从「昨天不持股、还能卖 2 次」继承。注意:只有在 j+1≤2 时,第二项才有效,否则不存在 dp[i-1][j+1],这时直接视为「不可能」,不会取。dp[i][j]:第 i 天结束时,不持股,之后还可以做 j 次卖出的最大利润。

六六哥的博客 1987

[LeetCode] 123. Best Time to Buy and Sell Stock III

原题链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 1. 题目简介 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...

Ber03的博客 292

【leetcode】123. Best Time to Buy and Sell Stock III

题目如下: 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 at mosttwotransactions. Note:You ...

weixin_34067102的博客 167

Leetcode 123 Best Time to Buy and Sell Stock III 至多两次买卖股票最大收益

假设你有一个数组,里面记录的是每一天的股票的价格。 设计一个算法来计算最大收益。你至多可以完成两次交易

(ง •̀_•́)ง 6539

[Leetcode] 123. Best Time to Buy and Sell Stock III 解题报告

题目: 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 at most two transactions. Note:

魔豆(Magicbean)的博客 818

LeetCode //C - 123. Best Time to Buy and Sell Stock III

【代码】LeetCode //C - 123. Best Time to Buy and Sell Stock III。

Made in Code 1026

Leetcode 123. Best Time to Buy and Sell Stock III

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 at most twotransactions. Note: You may not en...

salmonwilliam的博客 304

LeetCode(123) Best Time to Buy and Sell Stock III (Java)

题目如下: 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 at most two transactions. Note: Y

BUILD 1619

[LeetCode]123. Best time to Buy and Sell Stock III

[LeetCode]123. Best time to Buy and Sell Stock III欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,...

birduni的博客 162

LeetCode123 Best Time to Buy and Sell Stock III

详细见:leetcode.com/problems/best-time-to-buy-and-sell-stock-iii Java Solution: github package leetcode; import java.util.ArrayList; /* * Say you have an array for which the ith element is th

zxwtry的博客 473

LeetCode: Best Time to Buy and Sell Stock III [123]

【题目】 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 at most two transactions. Note: You may not engage in multiple transactions at the same time (i

Harry Huang 1029
上一篇: LeetCode122. Best Time to Buy and Sell Stock II
下一篇: LeetCode77. Combinations
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值