当前位置:网站首页>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;
}
边栏推荐
- Numpy -- epidemic data analysis case
- 航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
- Is it reliable to open an account on Tongda letter with your mobile phone? Is there any potential safety hazard in such stock speculation
- Three. JS introductory learning notes 15: threejs frame animation module
- Enterprise log analysis system elk
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- C4D learning notes 1- animation - animation key frames
- VS2005 strange breakpoint is invalid or member variable value cannot be viewed
- Three. JS introductory learning notes 0: illustration of how webgl and threejs work
- How to understand that binary complement represents negative numbers
猜你喜欢
分步式监控平台zabbix
Three. JS introductory learning notes 18: how to export JSON files with Blender
torch.numel作用
尤雨溪,来了!
C4D learning notes 2- animation - timeline and time function
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Unity3D_ Class fishing project, bullet rebound effect is achieved
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
numpy--数据清洗
Three. JS introductory learning notes 11:three JS group composite object
随机推荐
Numpy -- epidemic data analysis case
LeetCode1_ Sum of two numbers
山东老博会,2022中国智慧养老展会,智能化养老、适老科技展
47_Opencv中的轮廓查找 cv::findContours()
There are many ways to realize the pause function in JS
VS2005 strange breakpoint is invalid or member variable value cannot be viewed
Detailed explanation of Cocos creator 2.4.0 rendering process
Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
[wechat applet] Chapter (5): basic API interface of wechat applet
Bidding announcement: Panjin people's Hospital Panjin hospital database maintenance project
Shader Language
Whole process analysis of unity3d rendering pipeline
Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
Enterprise log analysis system elk
Particle effect for ugui
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Numpy -- data cleaning
Ue4/ue5 multi thread development attachment plug-in download address
Virtual memory, physical memory /ram what
Notification uses full resolution