当前位置:网站首页>Sword finger offer 63 Maximum profit of stock
Sword finger offer 63 Maximum profit of stock
2022-07-07 22:52:00 【Yes' level training strategy】
subject : Suppose you store the price of a stock in an array in chronological order , What's the maximum profit you can get from buying and selling this stock at one time ?
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 .
Example 2:
Input : [7,6,4,3,1]
Output : 0
explain : under these circumstances , No deal is done , So the biggest profit is 0.
This topic is relatively simple , Looking for the most profitable , That is to find the cheapest day to buy , Then I bought it on the most expensive day .
The most intuitive violent solution is twice for The cycle can be completed , But the time complexity is O(n^2).
Actually , Just borrow a variable minPrice Record the cheapest price , Then traverse the array once , Subtract the price of the day each time minPrice obtain res , The biggest record res , It is the biggest profit .
class Solution {
public int maxProfit(int[] prices) {
int minPrice = Integer.MAX_VALUE;
int res = 0;
for (int i = 0; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]);
res = Math.max(res, prices[i] - minPrice);
}
return res;
}
}
Time complexity O(n), Spatial complexity O(1)
A medium problem , There are many variants of stock trading , You can practice specially .
https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof
边栏推荐
- Matplotlib quick start
- GBU1510-ASEMI电源专用15A整流桥GBU1510
- “拧巴”的早教行业:万亿市场,难出巨头
- OpenGL configure assimp
- De la famille debezium: SET ROLE statements supportant mysql8
- 微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
- Revit secondary development - wall opening
- Revit secondary development - intercept project error / warning pop-up
- Debezium series: introducing support for the final operator
- Gazebo import the mapping model created by blender
猜你喜欢

How to judge whether the input content is "number"

微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈

0-5VAC转4-20mA交流电流隔离变送器/转换模块
![[problem] pytorch installation](/img/9f/1419c471838e3af8ea2158266254a5.jpg)
[problem] pytorch installation

ASP. Net core introduction V

Common verification rules of form components -2 (continuously updating ~)

行测-图形推理-2-黑白格类

Leetcode1984. Minimum difference in student scores

Matplotlib quick start

How to choose the appropriate automated testing tools?
随机推荐
IP network active evaluation system -- x-vision
行测-图形推理-7-相异图形类
Debezium系列之: 支持在 KILL 命令中使用变量
[environment] pycharm sets the tool to convert QRC into py file
Revit secondary development - project file to family file
Loki, the "open source star picking program", realizes the efficient management of harbor logs
Debezium series: introducing support for the final operator
Leetcode interview question 02.07 Linked list intersection [double pointer]
Aspose. Word operation word document (I)
Visual studio 2019 installation
OpeGL personal notes - lights
Record problems fgui tween animation will be inexplicably killed
Unity local coordinates and world coordinates
Write in front -- Talking about program development
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Cannot find module 'xxx' or its corresponding type declaration
Kaggle-Titanic
「开源摘星计划」Loki实现Harbor日志的高效管理
行测-图形推理-5-一笔画类
行测-图形推理-9-线条问题类