当前位置:网站首页>121. 买卖股票的最佳时机
121. 买卖股票的最佳时机
2022-07-07 13:49:00 【zzu菜】
121. 买卖股票的最佳时机
给定一个数组 prices
,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
示例 1:
输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
示例 2:
输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
提示:
1 <= prices.length <= 105
0 <= prices[i] <= 104
思考
首先我们假设第一天卖出股票,即当前卖出股票的价格为sellPrice
如果之后第i天的股票价格比卖出的股票价格低 sellPrice = prices[i]
否则 max = Math.max(max,prices[i]-sellPrice);
public int maxProfit(int[] prices) {
int sellPrice = prices[0];
int max = Integer.MIN_VALUE;
for (int i=1;i<prices.length;i++){
if(prices[i]<sellPrice){
sellPrice = prices[i];
}else {
max = Math.max(max,prices[i]-sellPrice);
}
}
if (max<0) return 0;
return max;
}
边栏推荐
- It's different for rich people to buy a house
- Shader basic UV operations, translation, rotation, scaling
- Yunxiaoduo software internal test distribution test platform description document
- 深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
- Points for attention in porting gd32 F4 series programs to gd32 F3 series
- Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
- Three. JS introductory learning notes 11:three JS group composite object
- OpenGL's distinction and understanding of VAO, VBO and EBO
- SPI master rx time out中断
- Getting started with webgl (4)
猜你喜欢
Virtual memory, physical memory /ram what
Getting started with webgl (2)
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
Iterator and for of.. loop
The download button and debug button in keil are grayed out
Unity3D_ Class fishing project, bullet rebound effect is achieved
Unity drawing plug-in = = [support the update of the original atlas]
星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
Ue4/ue5 multi thread development attachment plug-in download address
随机推荐
Simple understanding and application of TS generics
Async and await
Three. JS introductory learning notes 15: threejs frame animation module
Summary of knowledge points of xlua hot update solution
如何在shell中实现 backspace
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Use moviepy Editor clips videos and intercepts video clips in batches
SPI master rx time out中断
Numpy --- basic learning notes
Webcodecs parameter settings -avc1.42e01e meaning
Please supervise the 2022 plan
讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
[excelexport], Excel to Lua, JSON, XML development tool
C4D learning notes 3- animation - animation rendering process case
Function: JS Click to copy content function
Tkinter after how to refresh data and cancel refreshing
The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
A link opens the applet code. After compilation, it is easy to understand