当前位置:网站首页>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 - Hide occlusion elements
- Ligne - raisonnement graphique - 4 - classe de lettres
- 微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
- C development - interprocess communication - named pipeline
- Remove the default background color of chrome input input box
- Visual design form QT designer design gui single form program
- 苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
- 筑起云端 “免疫”屏障,让你的数据有备无患
- Revit secondary development - cut view
- 微服务架构开源框架详情介绍
猜你喜欢
Quick sort (diagram +c code)
Antd date component appears in English
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
Line test - graphic reasoning - 2 - black and white lattice class
Visual design form QT designer design gui single form program
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
Vs custom template - take the custom class template as an example
Basic knowledge of linked list
Leetcode interview question 02.07 Linked list intersection [double pointer]
随机推荐
Debezium系列之:源码阅读之BinlogReader
Robot autonomous exploration series papers environment code
“拧巴”的早教行业:万亿市场,难出巨头
UWA问答精选
微服务架构开源框架详情介绍
Loki, the "open source star picking program", realizes the efficient management of harbor logs
ASEMI整流桥KBPC1510的型号数字代表什么
CTF练习
Common verification rules of form components -2 (continuously updating ~)
Revit secondary development - intercept project error / warning pop-up
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
Debezium系列之: 支持在 KILL 命令中使用变量
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
IP网络主动测评系统——X-Vision
如何选择合适的自动化测试工具?
C development -- WPF simple animation
Debezium series: introducing support for the final operator
Remember an experience of using selectmany
Write in front -- Talking about program development
php 获取图片信息的方法