当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

In order to grasp the redis data structure, I drew 40 pictures (full version)

Pikachu target clearance and source code analysis
![[redis] ① introduction and installation of redis](/img/87/af98c862524a81d4636f1cb3be5181.png)
[redis] ① introduction and installation of redis

Super super super realistic digital people! Keep you on the air 24 hours a day

MPLS experiment

hcia综合实验

Redis killed twelve questions. How many questions can you carry?

基于数据要素流通视角的数据溯源研究进展

SQL server failed to send mail, prompting that the recipient must be specified

测试左移和测试右移的概念
随机推荐
YOLOV3
Four characteristics and isolation level of MySQL transactions
2022/7/25 exam summary
Are you still counting the time complexity?
为了拿捏 Redis 数据结构,我画了 40 张图(完整版)
Sorting out the encapsulation classes of control elements in appium
Wechat applet dynamic style | parameter transfer
Pikachu靶机通关和源码分析
2022/7/24 examination summary
Opencv learning Day6
Hcip day 11
基于SEIR模型的网络医疗众筹传播建模与仿真分析
2022/7/19 exam summary
HCIP 第十一天
HCIP第十二天
Hefei approved in advance
YOLOV2 YOLO9000
Tarjan 求强连通分量 O(n+m) ,缩点
[untitled] how to realize pluggable configuration?
Semaphore