Best Time to Buy and Sell Stock I II III

I2C通信协议SCL与SDA引脚特性:核心要点总结 深入讲解I2C通信协议中SCL和SDA引脚的工作机制与电气特性,帮助理解i2c通信协议的稳定传输原理,掌握上拉电阻配置与信号时序控制要点。 阅读详情

Best Time to Buy and Sell Stock I II III

I
对于第一题,本质意义是求出数组中某两个元素(day[i],day[i])的最大差值。但是,要求j>i

int maxProfit(vector<int> &prices) {
        if(prices.size() ==0)
            return 0;
        
        int min=prices[0], profit=0;
        for(int i=0; i<prices.size(); i++)
        {
            if(prices[i] - min > profit)
                profit = prices[i] - min;
                
            if(prices[i] < min)
                min = prices[i];
            
            
        }
        
        return profit;
    }

II
第二题是可以进行任意多次交易,那么对数组遍历,找到临时最小值和临时最大值。
int maxProfit(vector<int> &prices) {
        if (prices.size() == 0)
		return 0;

	int profit = 0, tmpMin=prices[0], tmpMax=prices[0];
	int n = prices.size();
	for (int i = 1; i < n; i++)
	{
	    //寻找比tmpMIN小的元素
		if (prices[i] < tmpMin)
		{
		    //比tmpMin小,则更新较小的
			tmpMin = prices[i];
		}
		else
		{
		    //比tmpMin大,则可以出手卖掉,并更新tmpMin
			profit += (prices[i]-tmpMin);
			tmpMin = prices[i];
		}
	}

	return profit;
    }

III
第三题比较复杂,只允许买卖两次,有两种方法。
其一,由于两次操作之间没有交互,即第一次买卖完成之后才能进行第二次的买卖,因此可以用分治法将[0...n]中取任意一个i,分别去求[0...i]和[i+1...n-1]的最大利润,那么两者之和就是两次操作的最大利润,但是这个算法的时间复杂度是O^2,超时了。

//取[m, n]之间的最大利润
int Profit1(vector<int>& prices, int m, int n)
{
	int size = prices.size();
	if(m >= size)
		return 0;

	int minPrice = prices[m], profit = 0;
	for (int i = m+1; i < n; i++)
	{
		if (prices[i] - minPrice > profit)
		{
			profit = prices[i] - minPrice;
		}

		if (prices[i] < minPrice)
		{
			minPrice = prices[i];
		}
	}

	return profit;
}

int maxProfit(vector<int> &prices)
{
	if(prices.size() == 0)
		return 0;

	int n = prices.size();
	int max1 = 0;
	for (int i = 1; i < n; i++)
	{
		int tmp1 = Profit1(prices, 0, i);		
		int tmp2 = Profit1(prices, i+1, n);
		if(tmp1 + tmp2 > max1)
			max1 = tmp1 + tmp2;
	}

	return max1;
}

方法二,DP,参考了一位网友的方法: http://blog.youkuaiyun.com/pickless/article/details/12034365
DP的关键是找出地推关系式
int maxProfit(vector<int> &prices)
{
	int n = prices.size();
	if(0==n || 1==n)
		return 0;

	int * l = new int[n];
	int * r = new int[n];
	memset(l, 0, sizeof(int) * n);
	memset(r, 0, sizeof(int) * n);


	int minPrice = prices[0];
	for (int i = 1; i < n; i++)
	{
		l[i] = prices[i] - minPrice > l[i-1]? prices[i] - minPrice : l[i-1];
		minPrice = prices[i] < minPrice ? prices[i] : minPrice;
	}

	int maxPrice = prices[n-1];
	for (int i = n-2; i >=0; i--)
	{
		r[i] = maxPrice - prices[i] > r[i+1] ? maxPrice - prices[i] : r[i+1];
		maxPrice = prices[i] > maxPrice ? prices[i] : maxPrice;
	}

	int profit = 0;
	for (int i = 0; i < n; i++)
	{
		profit = l[i] + r[i] > profit ? l[i] + r[i] : profit;
	}

	delete l;
	delete r;

	return profit;
}





JMeter压力测试全攻略:从入门到精通 本文系统讲解JMeter压力测试的完整流程与结果评估方法。开篇介绍压力测试的核心概念及JMeter工具特性,随后从环境搭建入手,详细说明JDK配置、JMeter安装及中文界面设置。主体部分围绕测试流程展开,涵盖测试计划创建、线程组参数调优、HTTP请求配置、断言与监听器添加等实操步骤;结果评估环节深入解析聚合报告指标、结果树分析及性能瓶颈定位方法,并通过电商接口测试案例演示全流程应用。文末总结关键要点,为读者提供从基础操作到分布式压测、性能调优的进阶学习方向,助力掌握JMeter在系统性能评估中的核心应用。 阅读详情

相关推荐

MaixCAM2 4K AI相机硬件解析与开发实战

嵌入式AI视觉开发中,边缘计算设备的选择直接影响项目落地效果。以NPU为核心的AI加速技术通过专用指令集优化,可在低功耗下实现10+TOPS算力,典型应用包括工业质检、智能门禁等实时场景。MaixCAM2作为新一代4K AI相机,搭载AX630 SoC芯片,其双核Cortex-A53与NPU的异构架构,在运行YOLOv11n模型时可达113FPS,显著优于传统方案。开发时需注意模型量化部署技巧,如INT8量化需500+张校准图片覆盖多光照条件,避免准确率下降。设备扩展性方面,PMOD接口支持热成像、ToF传

weixin_30819085的博客 690

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益。分析:动态规划法。从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求。**代码:**O(n)时

跳出温水的青蛙 2万+

使用 Python 实现无人机避障的人工势场算法

人工势场算法是一种基于力的控制方法,通过将目标位置视为一个吸引力源,而将障碍物视为排斥力源,来引导移动体朝着目标前进,同时避开障碍物。接下来,我们将定义几个类来实现人工势场算法,包括无人机类、障碍物类和势场类。

闲人编程的博客 2585

[leetcode]Best Time to Buy and Sell Stock I, II, III

这三道与股票相关的题目很有意思,有一些常用的小tricky和代码简洁技巧都用到了。 第一题 Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transacti

有礼貌的程序员 1272

@transactional滥用导致数据源连接池耗尽问题

@transactional滥用导致数据源连接池耗尽问题

qq_43205267的博客 1198

FPGA实战:MIG IP核AXI接口DDR3读写优化指南

本文详细解析了FPGA中MIG IP核AXI接口的DDR3读写优化策略,涵盖关键参数配置、突发传输技巧及时序收敛方案。通过AXI接口优化和突发长度选择,可实现高达12.8GB/s的理论带宽,提升DDR3访问效率。文章还提供了Vivado配置实例和调试技巧,助力开发者解决FPGA内存控制中的实际问题。

weixin_29168393的博客 545

leetcode Best Time to Buy and Sell Stock I II III

这三个题目是连续的就一同写下自己的想法吧。 题目1: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one an

guoxiaozhu的专栏 1574

Moodle平台题库建设技术

Moodle平台题库建设技术简述1新的改变1功能快捷键1合理的创建标题,有助于目录的生成1如何改变文本的样式1插入链接与图片1如何插入一段漂亮的代码片1生成一个适合你的列表1创建一个表格1设定内容居中、居左、居右1SmartyPants1创建一个自定义列表1如何创建一个注脚注释也是必不可少的1KaTeX数学公式1新的甘特图功能,丰富你的文章1UML 图表1FLowchart流程图1导出与导入导出1...

lxf78的博客 4534

[Leetcode][Python] Best Time to Buy and Sell Stock I,II,III,IV总结

[Leetcode][Python] Best Time to Buy and Sell Stock I,II,III,IV121. Best Time to Buy and Sell Stock I题目大意代码122. Best Time to Buy and Sell Stock II题目大意代码123. Best Time to Buy and Sell Stock III题目大意代码188. Best Time to Buy and Sell Stock IV题目大意代码 121. Best Tim

TwinkleZhao的博客 298

【软件介绍】GWAS meta分析软件:METAL

Meta-analysis是对多个GWAS分析结果进行综合评价。METAL是GWAS meta分析最常用的工具之一,本文简单介绍METAL软件基本用法。

hucy_Bioinfo 1万+

[从零开始学习FPGA编程-18]:快速入门篇 - 操作步骤2-6- VerilogHDL时序电路语法分析(以计数器为例)

目录前言:第1章 功能模块代码:counter.v第2章 驱动代码:tcounter.v前文以及阐述了VerilogHDL基本语法,本文将以modelsim自带的计数器程序来解读VerilogHDL的语法。代码路径:

文火冰糖(王文兵)的博客 7万+

LeetCode - Best Time To Buy and Sell Stock Series (I, II, III, IV, with Cooldown)

LeetCode - Best Time to Buy and Sell Stock 系列 解题报告本文记录笔者在解LeetCode上的Best Time to Buy and Sell Stock系列时的一些想法。该系列题目共有五道,它们背景相似,但因为条件限制不同,解决思路也不太相同。I Best Time to Buy and Sell Stock https://leet

Gregzeng的博客 687

XCP实战系列介绍07-使用ASAP2 Editor生成A2l文件详解

介绍通过ASAP2 Editor工具生成A2l文件步骤详解

initiallizer的博客 7296

Best Time to Buy and Sell Stock I,II,III [leetcode]

Best Time to Buy and Sell Stock I 只能作一次操作时:维护preMin记录之前出现的最小值 代码如下: int maxProfit(vector &prices) { if (prices.size() == 0) return 0; int profit = 0; int preMin = prices

Peerlessbloom 829

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

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

计算机控制技术及应用

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

leetcode Best Time to Buy and Sell Stock I II III IV

Best Time to Buy and Sell Stock 遍历一次数组,记录最小值,每次计算与最小值之间的差值为利润,最后选取利润最大的。 int INF=0x7ffffffe; int maxProfit(vector&amp;lt;int&amp;gt;&amp;amp; prices) { int min=INF; int profit=0; int tmp; for (...

肥肥很菜 318

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

LeetCode:Best Time to Buy and Sell Stock I II III

LeetCode:Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, ...

hulang_better_me的博客 198

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

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

Best Time to Buy and Sell Stock I & II & III

(1) Best Time to Buy and Sell Stock 题目是求数组中shudui

文森的学术之路 669

leetcode--Best Time to Buy and Sell Stock i ii iii

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction

sinat_24520925的专栏 525

Best Time to Buy and Sell Stock I && II && III

题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie,

快乐de胖虎 3173

121. Best Time to Buy and Sell Stock I II III IV

121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one an

CarryPotMan的博客 339

LeetCode Best Time to Buy and Sell Stock I II III

Best Time to Buy and Sell Stock  Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, b

那些年 1012

Best Time to Buy and Sell Stock I && II && III

题目1:Best Time to Buy and Sell Stock Say you have an array for which theithelement is the price of a given stock on dayi. If you were only permitted to complete at most one transact...

diandingyin9417的博客 87

【LeetCode】Best Time to Buy and Sell Stock I && II && III

1、Best Time to Buy and Sell Stock  Total Accepted: 7973 Total Submissions: 25534 My Submissions Say you have an array for which the ith element is the price of a given stock on day i. If you were o

wangzq的专栏 5607

自动机解决股票买卖问题(Best Time to Buy and Sell Stock I II III IV等)详细教程

Best Time to Buy and Sell Stock系列问题在面试过程中经常遇到,这里将下面几种问题做一个总结。 121 Best Time to Buy and Sell Stock 122 Best Time to Buy and Sell Stock II 123 Best Time to Buy and Sell Stock III 188 Best Time to Buy an...

rainMan的博客 635

Best Time to Buy and Sell Stock I II III IV

Best Time to Buy and Sell Stock I Description: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (

Acceptyly的专栏 571

LeetCode - Best Time to Buy and Sell Stock I && II && III && IV

Best Time to Buy and Sell Stock I https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 这道题非常简单,在每个位置卖的话,它能获得的最大值就是它的值减去在它之前出现的最小值。 如果在它之前没有比它小的值,那么就是当天买当天卖,即profit为0。所以就不断更新已出现过的数中最小的,然

chilakaka的专栏 342
上一篇: LeetCode Add Two Numbers
下一篇: Unique Binary Search Trees
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值