LeetCode----best-time-to-buy-and-sell-stock-iii

IFIX数据库Datebase当前值问号 IFIX数据库Datebase当前值问号,已解决,亲测有效 在OPC PowerTool有数据的情况下,IFIX数据库Datebase当前值显示???? 问题分析及解决方法 我的IFIX5.8版本,其它版本可参考 打开IFIX工作台:应用程序–任务控制–I/O驱动–OPC Client;查看OPC驱动是否被禁用,点击启动,变成激活状态即可;若点击启动无法激活OPC驱动,需要重新安装OPC驱动,方法如下: 下载I/O Driver文件 下载链接:https://download.csdn.net/down 阅读详情

题目描述


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

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

----------------------------------------------------------------------------------------------------------------
思路:循环利用解决best-time-to-buy-and-sell-stock-i(一次交易的最大收益)的方法,将数组分为front和back两部分并分别求其中的一个最大收益作和。
           每次更新保留较大收益。

代码实现:

1
class Solution {
2
public:
3
    int maxProfit(vector<int> &prices) {
4
        if (prices.size()<=1)
5
            return 0;
6
        int mx = 0;//mx记录当前最大收益
7
        vector<int>::iterator iter;
8
        for (iter=prices.begin(); iter!=prices.end(); ++iter){
9
            //将数组分为前后两部分并分别计算最大收益
10
            vector<int> front(prices.begin(), iter+1);
11
            vector<int> back(iter+1, prices.end());
12
            int a = maxP(front);
13
            int b = maxP(back);
14
            mx =max(a+b, mx); //更新当前最大收益
15
        }
16
        return mx;
17
    }
18
    int maxP(vector<int> &price){//处理一次交易最大收益
19
        if (price.size() <= 1)
20
            return 0;
21
        int mx = 0;
22
        for (int i=0; i<price.size()-1; ++i){
23
            for (int j=i+1; j<price.size(); ++j){
24
                mx = max(mx, price[j]-price[i]);
25
            }
26
        }
27
        return mx;
28
    }
29
};
您的代码已保存
网上 波峰波谷 解答:

classSolution {
public:
    intmaxProfit(vector<int> &prices) {
        if(prices.size() < 2)
            return0;
        constint n = prices.size();
        vector<int> f(n, 0);
        vector<int> g(n, 0);
        for(inti = 1, valley = prices[0]; i < n; ++i) {
            valley = min(valley, prices[i]);
            f[i] = max(f[i - 1], prices[i] - valley);
        }
        for(inti = n - 2, peak = prices[n - 1]; i >= 0; --i) {
            peak = max(peak, prices[i]);
            g[i] = max(g[i], peak - prices[i]);
        }
        intmax_profit = 0;
        for(inti = 0; i < n; ++i)
            max_profit = max(max_profit, g[i] + f[i]);
         
        returnmax_profit;
    }
};
------------------------------------------------------------
------------------------------------------------------------
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        //You may complete at most two transactions.(you must sell the stock before you buy again)
        //方式一:爆搜 (630ms 8552k)
        /*
        int len=prices.size();
        int maxPro=0;
        for(int i=0;i<len;i++){ //以i作为两次交易的分界线
            int max1=0,max2=0;
            for(int j=0;j<=i;j++){
                for(int k=j+1;k<=i;k++){
                    int val = prices[k]-prices[j];
                    if(val>max1){
                        max1=val;
                    }
                }
            }
            for(int j=i;j<len;j++){
                for(int k=j+1;k<len;k++){
                    int val = prices[k]-prices[j];
                    if(val>max2){
                        max2=val;
                    }
                }
            }
            if(max1+max2>maxPro)maxPro = max1+max2;
        }
        return maxPro;
        */
         
         
        //方式二:波峰波谷检测法,可以减小不必要的搜索空间,相比于第一种效率提升了不少    (180ms 8568k)
        int len = prices.size();
        if(len<=1)return 0;
        vector<int> flag(len,0);
        for(int i=1;i<len-1;i++){
            if(prices[i]>prices[i-1]&&prices[i]>=prices[i+1])flag[i]=1;  //标记为波峰(比较时注意相等情况)
            if(prices[i]<prices[i-1]&&prices[i]<=prices[i+1])flag[i]=-1;  //标记为波谷
        }
        if(prices[0]<=prices[1])flag[0]=-1;
        if(prices[len-1]>prices[len-2])flag[len-1]=1;
        //搜索
        int maxPro=0;
        for(int k=0;k<len;k++){
            int max1=0,max2=0;
            for(int i=0;i<=k;i++){
                if(flag[i]==-1){
                    for(int j=i+1;j<=k;j++){
                        if(flag[j]==1){
                            int val = prices[j]-prices[i];
                            if(val>max1)max1 = val;
                        }
                    }
                }
            }
            for(int i=k;i<len;i++){
                if(flag[i]==-1){
                    for(int j=i+1;j<len;j++){
                        if(flag[j]==1){
                            int val = prices[j]-prices[i];
                            if(val>max2)max2 = val;
                        }
                    }
                }
            }
            int val = max1+max2;
            if(val>maxPro)maxPro = val;
        }
        return maxPro;
    }
};

STM32嵌入式标准化封装五层架构设计与工程实践 在嵌入式系统开发中,代码封装是保障可维护性、可复用性与可靠性的核心工程能力。其本质是通过分层抽象(硬件抽象层→设备驱动层→应用逻辑层)解耦硬件依赖与业务逻辑,实现跨芯片移植、多任务安全及确定性实时响应。关键技术包括HAL库重构以消除硬编码初始化、驱动层提供同步/异步/事件驱动三类UART接口、中断优先级矩阵管理确保响应确定性、双阶段时钟配置兼顾启动鲁棒性与运行能效,以及面向Modbus等工业协议的语义化API封装。这些实践直击STM32项目中引脚变更难、HAL裸调风险高、中断处理不安全、时钟配置僵化等典型痛 阅读详情

相关推荐

Mplus—随机截距交叉滞后模型(Random Intercepts Cross-Lagged Panel Model, RI-CLPM)

介绍随机截距交叉滞后模型(Random Intercepts Cross-Lagged Panel Model, RI-CLPM)的相关知识,例如模型内容、Mplus语法及对应网址等。

m0_54401011的博客 9553

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

u010005161的博客 426

LayaBox CommandBuffer描边使用详细

项目需要使用LayaBox做一个给3D物体动态描边的一个功能,前前后后踩了一些坑算是把这个东西给做出来了把我改好的代码贴上,分享给大家。 (1)想要使用描边你的引擎版本必须 >=2.9.0 (2)你必须下载 Laya官方的BlurShader库才能使用这个功能,下载地址:https://github.com/layabox/layaair-demo/tree/master/h5/3d/ts/LayaAir3D_PostProcess/BlurShader 下载完之后把BlurShader放到自己项目的

小智队长 1740

best-time-to-buy-and-sell-stock-iii

package com.ytx.array; /**best-time-to-buy-and-sell-stock-iii  *  *     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 fin

天行健,君子以自强不息 240

小米手机免登录开启USB安装与调试(MIUI 9-11实测,已Root可用)

本文详细介绍了小米手机免登录开启USB安装与调试的底层方案,适用于已Root的MIUI 9-11版本设备。通过修改系统配置、禁用MIUI安装器验证和强制开启USB调试等步骤,帮助开发者绕过账号限制,实现快速调试。文章包含不同MIUI版本的兼容性处理和常见问题解决方法,实测有效。

weixin_33675507的博客 308

【乐企-工具篇】生成发票左上角二维码

【代码】【乐企-业务篇】生成发票左上角二维码。

九七的博客 712

Leetcode Best Time to Buy and Sell Stock III

/** * 题目:https://oj.leetcode.com/problems/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 t

wyj7260的专栏 1120

【WorkBuddy专栏39】零基础也能做小程序——WorkBuddy微信小程序开发完全指南

【WorkBuddy专栏39】零基础也能做小程序——WorkBuddy微信小程序开发完全指南

AI人人懂 3383

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

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

(ง •̀_•́)ง 6544

Motoman机器人离线编程——Motocom32开发简介

欢迎关注Jungle的Motoman机器人离线编程专栏系列文章: Motoman机器人离线编程——Motocom32开发简介 Motoman机器人离线编程——Visio Studio环境配置 Motoman机器人离线编程——Motocom32库函数的使用与封装 Motoman机器人离线编程——Motocom32参考文献及注意事项 Motoman机器人离线编程——MotocomES介绍 ...

冯Jungle的个人博客 8721

图解移动机器人中的运动规划(Motion Planning),附几十种规划算法

运动规划是移动机器人的核心理论和工程技术之一,本文作为教程先导,解析了运动规划的两大组件——路径规划和轨迹规划。欢迎感兴趣的同学订阅专栏!

FRIGIDWINTER的博客 4万+

leetcode之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 dayi. Design an algorithm to find the maximum profit. You may complete at mosttwo transactions.(发现这些问题变化了啥没?)

漫漫学习路 329

计算机控制技术及应用

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

leetcode best-time-to-buy-and-sell-stock-iii(Java)

leetcode题目 best-time-to-buy-and-sell-stock-iii – newcoder 28 买卖股票的最佳时机III – leetcode 123 题目描述 * Say you have an array for which the i th element * is the price of a given stock on day i. * * Des...

聚沙成塔 686

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【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

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

Best Time to Buy and Sell Stock III -- LeetCode

原题链接: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/  这道题是Best Time to Buy and Sell Stock的扩展,现在我们最多可以进行两次交易。我们仍然使用动态规划来完成,事实上可以解决非常通用的情况,也就是最多进行k次交易的情况。 这里我们先解释最多可以进行k次交易的算法,

Code Ganker 4万+

LeetCode Best Time to Buy and Sell Stock III

给定每天的股票价格,如果最多允许两次交易,但手中最多只能持有一支股票,在再次买入的时候必须将之前的股票卖出,求能获取的最大利润。

DRFish 2555

LeetCode123—Best Time to Buy and Sell Stock III

LeetCode123—Best Time to Buy and Sell Stock III

上善若水 1320

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

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

LeetCode 123: Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock IIISay 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 tr

一只码畜 421

leetcode 123. Best Time to Buy and Sell Stock III 股票最多两次买入、两次卖出的最大利润

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 数组的最大2子段和,允许在一天内买入又卖出,相当于不交易。因为题目规定的是最多交易两次,而不是必须交易两次。 class Solution { public: int maxProfit(vector<int>& prices)...

rock4you 315

LeetCode-Best Time to Buy and Sell Stock III算法对比

LeetCode 123.Best Time to Buy and Sell Stock III 由于先做了这类题目的 I II,看到III就直接按照先求局部最大值,取最大的前两个的和。(错误的代码就不贴了。。。)后来一直wrong answer. 忽然明白了限定两手交易,仍然可能将两个局部最大值连起来组成更大数值。一时想不起来解法 看到两个算法: 1.大牛1用分治策略,移动分割点分

catsanddogsman的专栏 564

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 305

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

3387

leetcode 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 eng

不积跬步,无以至千里 1727
上一篇: LeetCode----surrounded-regions
下一篇: LeetCode----sum-root-to-leaf-numbers
macans
博客等级 码龄11年 72粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值