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

带隙基准电路设计实战:从HSPICE仿真到Virtuoso版图全流程解析 在模拟集成电路设计中,基准电压源是确保系统精度与稳定性的核心模块。其基本原理在于巧妙结合具有正、负温度系数的电压,通过加权求和实现近似零温度系数的稳定输出,这一特性对于高精度数据转换器、电源管理芯片等至关重要。从工程实践角度,利用HSPICE进行深入的直流工作点、温度扫描和工艺角仿真,是验证电路性能、优化温度系数和电源抑制比的关键步骤。而借助Cadence Virtuoso进行版图实现时,必须重点关注电阻与BJT的匹配布局、敏感节点的保护以及电源完整性设计,以确保设计意图在硅片上得以准确呈现。通过完整的从前 阅读详情

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

题目描述

这里写图片描述

思路

动态规划
举例来说

index012345
原数组326503
到当前位置的最小值322200
到当前位置的最大收益004444
从当前位置到结尾的最大值666533
从当前位置到结尾的最大收益443330

因为买入卖出不能同时进行,所以将最大收益数组对角求和取最大值
在这个例子中是
max(4, 0 + 4, 0 + 3, 4 + 3, 4 +3, 4 + 0) = 7
即为最终结果

代码

自己写的O(n)时间、O(1)空间的代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if (len == 0)
            return 0;
        vector<int> curPreMin(len), curPostMax(len), curPreMaxGap(len), curPostMaxGap(len);

        curPreMin[0] = prices[0];
        curPreMaxGap[0] = 0;
        for (int i = 1; i < len; ++i){
            curPreMin[i] = min(curPreMin[i - 1], prices[i]);
            curPreMaxGap[i] = max(curPreMaxGap[i - 1], prices[i] - curPreMin[i]);
        }

        curPostMax[len - 1] = prices[len - 1];
        curPostMaxGap[len - 1] = 0;
        for (int i = len - 2; i >= 0; --i) {
            curPostMax[i] = max(curPostMax[i + 1], prices[i]);
            curPostMaxGap[i] = max(curPostMaxGap[i + 1], curPostMax[i] - prices[i]);
        }

        int res = curPostMaxGap[0];
        for (int i = 0; i < len - 1; ++i)
            res = max(res, curPreMaxGap[i] + curPostMaxGap[i + 1]);

        return res;
    }
};

int main() {
    vector<int> prices = { 3,2,6,5,0,3 };
    Solution s;
    cout << s.maxProfit(prices) << endl;

    system("pause");
    return 0;
}

ps:答案给出了O(1)空间的代码

/*
It is similar to other buy/sell problems. just do DP and define an array of states to track the current maximum profits at different stages. For example, in the below code

states[][0]: one buy
states[][1]: one buy, one sell
states[][2]: two buys, one sell
states[][3]: two buy, two sells
The states transistions occurs when buy/sell operations are executed. For example, state[][0] can move to state[][1] via one sell operation.
*/

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int states[2][4] = {INT_MIN, 0, INT_MIN, 0}; // 0: 1 buy, 1: one buy/sell, 2: 2 buys/1 sell, 3, 2 buys/sells
        int len = prices.size(), i, cur = 0, next =1;
        for(i=0; i<len; ++i)
        {
            states[next][0] = max(states[cur][0], -prices[i]);
            states[next][1] = max(states[cur][1], states[cur][0]+prices[i]);
            states[next][2] = max(states[cur][2], states[cur][1]-prices[i]);
            states[next][3] = max(states[cur][3], states[cur][2]+prices[i]);
            swap(next, cur);
        }
        return max(states[cur][1], states[cur][3]);
    }
};
LVGL 磁罗盘效果 环境:LVGL 9.x + SDL2,窗口 1280×800。界面文案用英文(默认 Montserrat,无 CJK)。本文按实现顺序讲:先建立两个耦合转动体,再把磁倾角误差从卡面倾斜里推出来,最后用同一套投影把刻度、数字和目标画到卡上。画面上的迟滞、涡流、摆动和读数偏差都来自方程,没有补间动画。 阅读详情

相关推荐

calibre QRC提取寄生参数

typical将上面的文件QCI_flow里面的文件 在Cp 到当前 ,其实应该是某几个文件,但是我给忘记了 就全cp过来了 ,无影响,不过也希望大佬指正。

2202_75711937的博客 1523

【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

winpcap无法安装提示新版本已经安装解决办法

问题显示如下该问题会导致ensp里的云识别不到网卡可能是因为之前安装了npcap,而且安装时勾选了第四个选项导致无法安装,需要完全卸载npcap解决办法:办法一:在控制面板的程序与卸载里面卸载npcap和winPcap,然后再安装winPcap,如果搜不到往下看办法二:在C:\Windows\SysWOW64目录下搜索wpcap.dll和packet.dll并重命名为wpcap.dll.old和packet.dll.old办法三:如果搜不到这两个文件需要安装win10Pcap才能找到这两个文件下载地址:Wi

qq_73202377的博客 3万+

LeetCode123—Best Time to Buy and Sell Stock III

LeetCode123—Best Time to Buy and Sell Stock III

上善若水 1320

手把手教你用Qwen-7B打造医疗大模型:从数据准备到RLHF全流程实战

本文是一篇基于Qwen-7B-Chat模型构建医疗领域大模型的实战全流程指南。文章详细拆解了从数据准备、环境搭建,到增量预训练、监督微调(SFT)以及偏好对齐(DPO)的核心技术步骤,并分享了部署与持续优化的实践经验。通过LoRA等高效微调技术,帮助开发者掌握将通用大模型改造为专业领域专家的完整方法论。

weixin_29191761的博客 129

图机器学习(7)——图神经网络 (Graph Neural Network, GNN)

图神经网络 (Graph Neural Network, GNN) 是一类专门处理图结构数据的深度学习方法。这类方法也被称为几何深度学习 (geometric deep learning),在社交网络分析、计算机图形学等众多领域正受到日益广泛的关注。在本节中,我们介绍了 GNN 的核心原理,将卷积等经典深度学习范式拓展至非欧几里得图空间,实现了局部结构与全局特征的层次化提取。

盼小辉丶的博客 1万+

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

PX4控制云台的两种方式 方法适用控舵机 控灯

云台控制插件是mount_control,运行mavros启动文件的时候需要注意该插件是否正常启动,需要在px4_pluginlists.yaml配置文件中将mount_control从blacklisted中移除后才可启动。后会默认占用AUX端口1-4,此时通过AUX通道1-4输出PWM控制信号至云台,对应的RC通道映射功能失效。控制信号输入为RC 则接收机必须连接对应的通道至AUX辅助通道,并通过MNT_MAN_*参数设置pitch/roll/yaw对应的AUX通道。配置为AUTO或MAVLINK,

z1872385的博客 3669

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

秃鹰搜索优化算法(BES)详解:从自然捕食到全局优化

秃鹰搜索优化算法(Bald Eagle Search Optimization Algorithm,BES)是由马来西亚学者Alsattar等人于2020年提出的一种新型元启发式优化算法。该算法灵感来源于秃鹰在自然界中高超的捕食行为,特别是其独特的狩猎策略和卓越的视觉搜索能力,模拟了秃鹰捕食鲑鱼等猎物的完整过程。

qq_42212808的博客 1353

Python开发MCP Client

全部例子只是MCP Client访问MCP Server的过程,那么这样其实就失去了MCP协议的创建的初衷,和大模型集成后才能发挥MCP最大的作用。

Criss@陈磊 2973

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

人像抠图 + OpenGL ES 还能这样玩?没想到吧(附带源码)

OpenGL ES 利用抠图算法实现人像留色。

字节流动 3万+

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

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

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

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

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

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[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的博客 293

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 至多两次买卖股票最大收益

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

(ง •̀_•́)ง 6539

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

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 1027

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

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

[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]415. Add Strings
下一篇: [LeetCode]405. Convert a Number to Hexadecimal
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值