当前位置:网站首页>【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 *・゜゚・*:.。..。.:*・'(*゚▽゚*)'・*:.。. .。.:*・゜゚・*
边栏推荐
- 华为交换机Console密码重置、设备初始化、默认密码
- Mail sending of vertx
- 技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
- 技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
- List exercises after class
- 昇思MindSpore再升级,深度科学计算的极致创新
- PAT甲级 1027 Colors in Mars
- 华为交换机:配置telnet和ssh、web访问
- 技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务
- HCIA notes
猜你喜欢

Harmonyos third training notes

研究显示乳腺癌细胞更容易在患者睡觉时进入血液

Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!

Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function

Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition

Iterm2设置

項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
![[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211](/img/55/fea5fe315932b92993d21f861befbe.png)
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211

Application of pigeon nest principle in Lucene minshouldmatchsumscorer

密西根大学张阳教授受聘中国上海交通大学客座教授(图)
随机推荐
s7700设备如何清除console密码
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
技术干货|百行代码写BERT,昇思MindSpore能力大赏
【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
Structure of golang
Robots protocol
Inverted chain disk storage in Lucene (pfordelta)
The difference between typescript let and VaR
HDMI2.1与HDMI2.0的区别以及转换PD信号。
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
Go language foundation ----- 05 ----- structure
Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
Industrial resilience
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
PAT甲级 1032 Sharing
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
Read config configuration file of vertx
Logging log configuration of vertx
【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/