当前位置:网站首页>121. The best time to buy and sell stocks
121. The best time to buy and sell stocks
2022-07-07 16:02:00 【Zzu dish】
121. The best time to buy and sell stocks
Given an array prices , It's the first i Elements prices[i] Represents the number of shares in a given stock i Sky price .
You can only choose One day Buy this stock , And choose A different day in the future Sell the stock . Design an algorithm to calculate the maximum profit you can get .
Return the maximum profit you can make from the deal . If you can't make any profit , return 0 .
Example 1:
Input :[7,1,5,3,6,4]
Output :5
explain : In the 2 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 6) Sell when , Maximum profit = 6-1 = 5 .
Note that profit cannot be 7-1 = 6, Because the selling price needs to be higher than the buying price ; meanwhile , You can't sell stocks before you buy them .
Example 2:
Input :prices = [7,6,4,3,1]
Output :0
explain : under these circumstances , No deal is done , So the biggest profit is 0.
Tips :
1 <= prices.length <= 105
0 <= prices[i] <= 104
reflection
First, let's assume that we sell stocks on the first day , That is, the current selling price of the stock is sellPrice
If the next i The stock price of days is lower than the stock price sold sellPrice = prices[i]
otherwise 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;
}
边栏推荐
- TCP framework___ Unity
- SPI master RX time out interrupt
- The inevitable trend of the intelligent development of ankerui power grid is that microcomputer protection devices are used in power systems
- Learn good-looking custom scroll bars in 1 minute
- Enterprise log analysis system elk
- postman生成时间戳,未来时间戳
- OpenGL's distinction and understanding of VAO, VBO and EBO
- 讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
- LeetCode1_ Sum of two numbers
- numpy--疫情数据分析案例
猜你喜欢

20th anniversary of agile: a failed uprising

Cut ffmpeg as needed, and use emscripten to compile and run

分步式监控平台zabbix

How does geojson data merge the boundaries of regions?

The unity vector rotates at a point
![[wechat applet] Chapter (5): basic API interface of wechat applet](/img/63/e819113c7c93e273525bc850892972.png)
[wechat applet] Chapter (5): basic API interface of wechat applet

AE learning 01: AE complete project summary

Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
![Application example of infinite list [uigridview]](/img/11/3be1c63680e6de8f068e79690ecf12.jpg)
Application example of infinite list [uigridview]

Vite path alias @ configuration
随机推荐
企业级日志分析系统ELK
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
Getting started with webgl (4)
Streaming end, server end, player end
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
./ Functions of configure, make and make install
Getting started with webgl (2)
Unity3D_ Class fishing project, bullet rebound effect is achieved
A JS script can be directly put into the browser to perform operations
SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术
Vite path alias @ configuration
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
numpy--疫情数据分析案例
SPI master RX time out interrupt
Using eating in cocos Creator
Annexb and avcc are two methods of data segmentation in decoding
2022山东智慧养老展,适老穿戴设备展,养老展,山东老博会
Clang compile link ffmpeg FAQ
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展