当前位置:网站首页>【LeetCode】4. Best time to buy and sell stock
【LeetCode】4. Best time to buy and sell stock
2022-07-03 07:42:00 【AQin1012】
Title Description
Description in English
You are given an array prices where prices[i] is the price of a given stock on the i(th) day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
English address
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Description of Chinese version
Given an array prices , It's the first i Elements prices[i] Represents the number of shares in a given stock i Sky price . You can only choose One day Buy this stock , And choose A different day in the future Sell the stock . Design an algorithm to calculate the maximum profit you can get . Return the maximum profit you can make from the deal . If you can't make any profit , return 0.
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 ; meanwhile , You can't sell stocks before you buy them .
Example 2:
Input :prices = [7,6,4,3,1]
Output :0
explain : under these circumstances , No deal is done , So the biggest profit is 0.
Constraints· Tips :
1 <= prices.length <= 10(5)
0 <= prices[i] <= 10(4)
Address of Chinese version
Their thinking
You need to traverse the input array from scratch , Set two variables respectively ( Current minimum and current maximum ), At the beginning, assign the first value of the array to two variables at the same time , Then start with the second , When a number smaller than the current minimum value is encountered, it will be re assigned to the current minimum value , If you encounter a number larger than the current maximum value, you will re assign it to the current maximum value , After the traversal is over , return ( Current maximum - Current minimum ) The difference between the . Take every value , Get the maximum difference between the value behind him and him , Go back to the biggest one
How to solve the problem
My version
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2) {
return 0;
}
int minValue = prices[0];
int step = 0;
for (int i = 1; i < prices.length; i++) {
int value = (prices[i] - minValue);
if (value > 0) {
if (step < value) {
step = value;
}
} else {
minValue = prices[i];
}
}
return step;
}
}
Official edition
Dynamic programming
public class Solution {
public int maxProfit(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}
This is my closest official solution ~~ And the flower *・゜゚・*:.。..。.:*・'(*゚▽゚*)'・*:.。. .。.:*・゜゚・*
边栏推荐
- Go language foundation ----- 11 ----- regular expression
- Mail sending of vertx
- Reconnaissance et détection d'images - Notes
- Unified handling and interception of exception exceptions of vertx
- 不出网上线CS的各种姿势
- 华为交换机:配置telnet和ssh、web访问
- Inverted chain disk storage in Lucene (pfordelta)
- Sent by mqtt client server of vertx
- SQL create temporary table
- Introduction of novel RNA based cancer therapies
猜你喜欢
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
技术干货|昇思MindSpore创新模型EPP-MVSNet-高精高效的三维重建
Go language foundation ----- 09 ----- exception handling (error, panic, recover)
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
Why is data service the direction of the next generation data center?
Go language foundation ----- 16 ----- goroutine, GPM model
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
随机推荐
Structure of golang
opensips与对方tls sip trunk对接注意事项
Industrial resilience
C2-关于VCF文件合并的几种方法
Responsive MySQL of vertx
Industrial resilience
微软安全响应中心
Go language - loop statement
PgSQL converts string to double type (to_number())
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
Go language foundation ----- 05 ----- structure
UA camouflage, get and post in requests carry parameters to obtain JSON format content
VMware network mode - bridge, host only, NAT network
Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore
技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
Why is data service the direction of the next generation data center?
技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
Iterm2设置
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题