当前位置:网站首页>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
- Regular expression string
- Write sequence frame animation with shader
- Three. JS introductory learning notes 11:three JS group composite object
- 保证接口数据安全的10种方案
- Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
- webgl_ Graphic transformation (rotation, translation, zoom)
- Using eating in cocos Creator
- Aerospace Hongtu information won the bid for the database system research and development project of a unit in Urumqi
- AE learning 01: AE complete project summary
猜你喜欢

Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?

Vertex shader to slice shader procedure, varying variable

C4D learning notes 2- animation - timeline and time function

Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?

Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping

Three. JS introductory learning notes 10:three JS grid

TS as a general cache method

Postman generate timestamp, future timestamp

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc

Mesh merging under ue4/ue5 runtime
随机推荐
Application example of infinite list [uigridview]
Dotween -- ease function
Mysql database backup script
47_Opencv中的轮廓查找 cv::findContours()
航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
企业级日志分析系统ELK
torch.numel作用
Getting started with webgl (4)
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import
Use of SVN
招标公告:福建省农村信用社联合社数据库审计系统采购项目(重新招标)
Numpy --- basic learning notes
C4D learning notes 3- animation - animation rendering process case
TS as a general cache method
Vite path alias @ configuration
分步式监控平台zabbix
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address