当前位置:网站首页>【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 *・゜゚・*:.。..。.:*・'(*゚▽゚*)'・*:.。. .。.:*・゜゚・*
边栏推荐
- 【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
- JUnit unit test of vertx
- 技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
- 华为交换机Console密码重置、设备初始化、默认密码
- Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
- [coppeliasim4.3] C calls UR5 in the remoteapi control scenario
- 【MySQL 12】MySQL 8.0.18 重新初始化
- C2-关于VCF文件合并的几种方法
- Mail sending of vertx
- PDO and SDO concepts
猜你喜欢

c语言指针的概念

Traversal in Lucene

Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward

Go language foundation ----- 06 ----- anonymous fields, fields with the same name

Inverted chain disk storage in Lucene (pfordelta)

【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)

项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现

Comparison of advantages and disadvantages between most complete SQL and NoSQL

Go language foundation ----- 11 ----- regular expression

项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
随机推荐
go语言-循环语句
s7700设备如何清除console密码
Lucene skip table
Sent by mqtt client server of vertx
哪一刻你才发现青春结束了
Lucene hnsw merge optimization
OSPF protocol summary
【踩坑系列】mysql 修改root密码失败
The concept of C language pointer
Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
SQL create temporary table
技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务
【MySQL 13】安装MySQL后第一次修改密码,可以可跳过MySQL密码验证进行登录
Collector in ES (percentile / base)
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
Usage of requests module
【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
Implementation of breadth first in aggregation in ES
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
Responsive MySQL of vertx
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/