当前位置:网站首页>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;
}
边栏推荐
- C4D learning notes 3- animation - animation rendering process case
- 分步式監控平臺zabbix
- Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
- [wechat applet] Chapter (5): basic API interface of wechat applet
- JS array foreach source code parsing
- Streaming end, server end, player end
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 尤雨溪,来了!
- 星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
- Detailed explanation of unity hot update knowledge points and introduction to common solution principles
猜你喜欢

Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)

OpenGL's distinction and understanding of VAO, VBO and EBO

Three. JS introductory learning notes 11:three JS group composite object

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

LeetCode3_ Longest substring without duplicate characters

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

保证接口数据安全的10种方案

无线传感器网络--ZigBee和6LoWPAN

Annexb and avcc are two methods of data segmentation in decoding

After UE4 is packaged, mesh has no material problem
随机推荐
Postman generate timestamp, future timestamp
Three. JS introductory learning notes 15: threejs frame animation module
Gd32 F3 pin mapping problem SW interface cannot be burned
Three. JS introductory learning notes 04: external model import - no material obj model
用手机在通达信上开户靠谱吗?这样炒股有没有什么安全隐患
webgl_ Enter the three-dimensional world (1)
numpy--疫情数据分析案例
Cut ffmpeg as needed, and use emscripten to compile and run
Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?
AE learning 02: timeline
Clang compile link ffmpeg FAQ
Introduction to pyGame games
Syntax of generator function (state machine)
AE learning 01: AE complete project summary
Application example of infinite list [uigridview]
There are many ways to realize the pause function in JS
Three. JS introduction learning notes 12: the model moves along any trajectory line
The rebound problem of using Scrollview in cocos Creator
Numpy -- data cleaning
Shader basic UV operations, translation, rotation, scaling