当前位置:网站首页>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--数据清洗
- The unity vector rotates at a point
- LeetCode2_ Add two numbers
- 一个普通人除了去工厂上班赚钱,还能干什么工作?
- L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
- Syntax of generator function (state machine)
- Webgl texture
- leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
- 一大波开源小抄来袭
- Cocos makes Scrollview to realize the effect of zooming in the middle and zooming out on both sides
猜你喜欢
通知Notification使用全解析

强化实时数据管理,英方软件助力医保平台安全建设

C4D learning notes 1- animation - animation key frames

numpy--疫情数据分析案例

【微信小程序】Chapter(5):微信小程序基础API接口

Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models

numpy--数据清洗

Getting started with webgl (1)

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

Postman generate timestamp, future timestamp
随机推荐
分步式监控平台zabbix
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
深度之眼(六)——矩阵的逆(附:logistic模型一些想法)
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
Function: JS Click to copy content function
Three. JS introduction learning notes 12: the model moves along any trajectory line
持续创作,还得靠它!
Vite path alias @ configuration
postman生成时间戳,未来时间戳
20th anniversary of agile: a failed uprising
JS array foreach source code parsing
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Streaming end, server end, player end
torch.numel作用
hellogolang
如何在shell中实现 backspace
How does geojson data merge the boundaries of regions?
VS2005 strange breakpoint is invalid or member variable value cannot be viewed
Use of SVN