当前位置:网站首页>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
边栏推荐
- Revit secondary development - modify wall thickness
- Variables and constants
- Revit secondary development - collision detection
- Revit secondary development - get the thickness / length / height of the beam
- . Net automapper use
- Redis集群安装
- 行测-图形推理-5-一笔画类
- UWA问答精选
- Gazebo import the mapping model created by blender
- 变量与常量
猜你喜欢
Vs custom template - take the custom class template as an example
行测-图形推理-7-相异图形类
Matplotlib快速入门
The PHP source code of the new website + remove authorization / support burning goose instead of pumping
Micro service remote debug, nocalhost + rainbow micro service development second bullet
行测-图形推理-1-汉字类
Redis official ORM framework is more elegant than redistemplate
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
Cannot find module 'xxx' or its corresponding type declaration
IP网络主动测评系统——X-Vision
随机推荐
Leetcode94. Middle order traversal of binary trees
变量与常量
Line test - graphic reasoning - 6 - similar graphic classes
Welcome to CSDN markdown editor
行测-图形推理-7-相异图形类
PCL .vtk文件与.pcd的相互转换
Debezium系列之:支持 mysql8 的 set role 语句
JS number is insufficient, and 0 is added
Revit secondary development - get the thickness / length / height of the beam
Write in front -- Talking about program development
Debezium series: binlogreader for source code reading
Unity local coordinates and world coordinates
ASEMI整流桥KBPC1510的型号数字代表什么
Revit secondary development - cut view
Leetcode206. Reverse linked list
OpenGL job - texture
Line test - graphic reasoning - 4 - alphabetic class
Add get disabled for RC form
Revit secondary development - shielding warning prompt window
“拧巴”的早教行业:万亿市场,难出巨头