当前位置:网站首页>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;
}
边栏推荐
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- C4D learning notes 1- animation - animation key frames
- Three. JS introductory learning notes 13: animation learning
- The "go to definition" in VS2010 does not respond or prompts the solution of "symbol not found"
- 深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
- Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
- 有钱人买房就是不一样
- Gd32 F3 pin mapping problem SW interface cannot be burned
- How to build your own super signature system (yunxiaoduo)?
猜你喜欢

UE4 exports the picture + text combination diagram through ucanvasrendertarget2d

融云斩获 2022 中国信创数字化办公门户卓越产品奖!

AE learning 02: timeline

webgl_ Graphic transformation (rotation, translation, zoom)

Three. JS introductory learning notes 18: how to export JSON files with Blender

持续创作,还得靠它!

Iterator and for of.. loop

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

企业级日志分析系统ELK

unnamed prototyped parameters not allowed when body is present
随机推荐
Virtual memory, physical memory /ram what
Use of SVN
Getting started with webgl (4)
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?
C4D learning notes 3- animation - animation rendering process case
【花雕体验】15 尝试搭建Beetle ESP32 C3之Arduino开发环境
Gd32 F3 pin mapping problem SW interface cannot be burned
通知Notification使用全解析
numpy--疫情数据分析案例
Vite path alias @ configuration
Cocos creator collision and collision callback do not take effect
[excelexport], Excel to Lua, JSON, XML development tool
LeetCode2_ Add two numbers
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
nodejs package. JSON version number ^ and~
Postman generate timestamp, future timestamp
Shader basic UV operations, translation, rotation, scaling