当前位置:网站首页>Leetcode notes 121. the best time to buy and sell stocks
Leetcode notes 121. the best time to buy and sell stocks
2022-07-26 00:38:00 【Lyrig~】
Leetcode note 121. The best time to buy and sell stocks
Title Description
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.
Tips :
1 <= prices.length <= 105
0 <= prices[i] <= 104
Ideas
The first thought of violent solution , It goes without saying , Is to calculate all possible situations , The advantage of this method is that it takes less memory , After all, you don't need to save anything , But its time complexity is O ( n 2 ) \mathcal{O}(n^2) O(n2), This is unacceptable .
The second way of thinking , Through analysis , The essence of maximum benefit , yes a r g m a x { m a x c o s t i − m i n c o s t i } argmax\{maxcost_i - mincost_i\} argmax{ maxcosti−mincosti}, namely The first 1 ~ N The maximum cost in a day - The first 1~i The minimum cost in a day , And these two values , It only takes one time to solve , So the time complexity of this method is O ( n ) \mathcal{O}(n) O(n) That's good .
Code
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxp[100005] {
};
int minp[100005] {
};
minp[0] = prices[0];
maxp[prices.size() - 1] = prices[prices.size() - 1];
int ans = 0;
for(int i = 1; i < prices.size(); ++i)
{
minp[i] = min(minp[i - 1], prices[i]);
}
ans = max(0, maxp[prices.size() - 1] - minp[prices.size() - 1]);
for(int j = prices.size() - 2; j >= 0; -- j)
{
maxp[j] = max(maxp[j + 1], prices[j]);
ans = max(ans, maxp[j] - minp[j]);
}
return ans;
}
};
边栏推荐
- SereTOD2022 Track1代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
- [GOM引擎]假人配置的脚本设置方法
- Revision of Journal of Computational Physics
- BGP comprehensive experiment
- DC-6 -- vulnhub range
- HCIP第十三天
- YOLOV2 YOLO9000
- Redis夺命十二问,你能扛到第几问?
- 【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
- The way of understanding JS: write a perfect combination inheritance (Es5)
猜你喜欢

The way to understand JS: six common inheritance methods of JS

8 tips - database performance optimization, yyds~

Redis夺命十二问,你能扛到第几问?

向左旋转k个字符串(细节)

JMeter/IDEA中引用jar包json-path.jar的坎坷之路

Distributed transactions: the final consistency scheme of reliable messages

Study on gene targeting preparation of tissue plasminogen activator loaded on albumin nano ultrasonic microbubbles

快速入门顺序表链表

Quick start sequence table chain table

基于MFFMB的电商评论文本分类研究
随机推荐
Use of redis
Introduction of MySQL transactions
基于网络分析和文本挖掘的意见领袖影响力研究
In order to grasp the redis data structure, I drew 40 pictures (full version)
【NumPy中数组创建】
Redis夺命十二问,你能扛到第几问?
P4047 [jsoi2010] tribal Division
SQL server failed to send mail, prompting that the recipient must be specified
D3D计算着色器入门
分布式事务 :可靠消息最终一致性方案
Nodejs learning
[redis] ② redis general command; Why is redis so fast?; Redis data type
前缀异或和,异或差分数组
使用LocalDate类完成日历设计
Study on bovine serum protein modified phenolic acids and alkaloids small molecules / coupled microspheres protein / bovine erythrocyte SOD
Super super super realistic digital people! Keep you on the air 24 hours a day
[contents] mqtt, nodejs projects
HCIP 第十一天
12. Neural network model
PC website realizes wechat code scanning login function (II)