当前位置:网站首页>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
边栏推荐
- Vs custom template - take the custom class template as an example
- GBU1510-ASEMI电源专用15A整流桥GBU1510
- 行测-图形推理-4-字母类
- This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
- Remember that a development is encountered in the pit of origin string sorting
- Micro service remote debug, nocalhost + rainbow micro service development second bullet
- 软件测评中心▏自动化测试有哪些基本流程和注意事项?
- LeetCode203. Remove linked list elements
- Quick sort (diagram +c code)
- 行测-图形推理-5-一笔画类
猜你喜欢
行测-图形推理-7-相异图形类
Blender exchange group, welcome to the water group ~
IP network active evaluation system -- x-vision
Visual studio 2019 installation
LeetCode203. Remove linked list elements
Line test - graphic reasoning - 6 - similar graphic classes
行测-图形推理-3-对称图形类
行测-图形推理-5-一笔画类
Redis集群安装
Robot autonomous exploration series papers environment code
随机推荐
[interview arrangement] 0211 game engine server
Revit secondary development - Hide occlusion elements
Ren Qian code compilation error modification
Get the week start time and week end time of the current date
PCL .vtk文件与.pcd的相互转换
Firefox browser installation impression notes clipping
Debezium系列之:mysql墓碑事件
PHP method of obtaining image information
UWA问答精选
Revit secondary development - collision detection
OpeGL personal notes - lights
Revit secondary development - cut view
行测-图形推理-8-图群类
Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution
Redis集群安装
Debezium系列之:支持 mysql8 的 set role 語句
[environment] pycharm sets the tool to convert QRC into py file
Details of the open source framework of microservice architecture
CTF练习
How to choose the appropriate automated testing tools?