当前位置:网站首页>[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
边栏推荐
- 阿里云回应用户注册信息泄露事件
- Zhongchuang storage | how to choose a useful distributed storage cloud disk
- Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
- [leetcode refers to offer 27. Image of binary tree (simple)]
- pikachuxss如何获取cookie靶场,返回首页总是失败
- go defer
- Assertionerror: invalid device ID and runtimeerror: CUDA error: invalid device ordinal
- [leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
- Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
- Prim、Kruskal
猜你喜欢
Win 11K in 100 days, super complete learning guide for job transfer test
MySQL基础之写表(创建表)
ROS学习笔记-----ROS的使用教程
airbase 初步分析
Some grounded words
ROS learning notes - tutorial on the use of ROS
Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
PHP的Laravel与Composer部署项目时常见问题
Deno 1.13.2 发布
MySQL进阶之数据的增删改查(DML)
随机推荐
41. The first missing positive number
常用60类图表使用场景、制作工具推荐
Alibaba cloud responded to the disclosure of user registration information
Google 尝试在 Chrome 中使用 Rust
Awk example skills
What if Jenkins forgot his password
Reference of custom message in ROS function pack failed
Unit function expansion
亚马逊和Epic将入驻,微软应用商城向第三方开放
Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
pikachuxss如何获取cookie靶场,返回首页总是失败
使用mbean 自动执行heap dump
Preliminary analysis of Airbase
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
一些接地气的话儿
Go limit depth traversal of files in directory
Mysql database common sense storage engine
unity 功能扩展
Valueerror: invalid literal for int() with base 10 conversion error related to data type