当前位置:网站首页>[leetcode refers to the maximum profit of offer 63. Stock (medium)]
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
2022-04-23 21:21:00 【Minaldo7】
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.
Limit :
0 <= The length of the array <= 10^5
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/gu-piao-de-zui-da-li-run-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length <= 1) {
return 0;
}
int minInit = prices[0];
int maxGet = 0;
for(int i=1;i<prices.length;i++){
maxGet = Math.max(maxGet, prices[i]-minInit);
minInit = Math.min(minInit,prices[i]);
}
return maxGet;
}
}
Execution results :
Another solution :
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length <= 1) {
return 0;
}
int res = 0, min = prices[0];
for(int i = 1; i < prices.length; i++) {
if(prices[i] <= min) {
// Change the minimum buying price
min = prices[i];
}else {
// Change maximum profit
res = Math.max(res, prices[i] - min);
}
}
return res;
}
}
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479508.html
边栏推荐
- 3-5 obtaining cookies through XSS and the use of XSS background management system
- Detectron2 usage model
- 如何发挥测试策略的指导性作用
- MySQL数据库常识之储存引擎
- Google 尝试在 Chrome 中使用 Rust
- go array
- Graph traversal - BFS, DFS
- MySQL advanced common functions
- UKFslam
- The computer is out of power. How did I pass the terrible interview of Tencent cloud?
猜你喜欢
GSI-ECM工程建设管理数字化平台
How to make Jenkins job run automatically after startup
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Prim、Kruskal
go interface
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
CUDA, NVIDIA driver, cudnn download address and version correspondence
On the three paradigms of database design
flomo软件推荐
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
随机推荐
presto on spark 支持3.1.3记录
ubutnu20安裝CenterNet
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Problem brushing plan -- dynamic programming (IV)
[leetcode refers to offer 27. Image of binary tree (simple)]
IOT design and development
Common commands of MySQL in Linux
MySQL数据库常识之储存引擎
Automatic heap dump using MBean
The computer is out of power. How did I pass the terrible interview of Tencent cloud?
pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
启牛学堂有用吗,推荐的证券账户是否安全
Thinkphp5 + data large screen display effect
Ubutnu20 installer centernet
Addition, deletion, modification and query of advanced MySQL data (DML)
MySQL进阶之表的增删改查
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Explore ASP Net core read request The correct way of body
Presto on spark supports 3.1.3 records