当前位置:网站首页>Leetcode 0122. the best time to buy and sell stocks II
Leetcode 0122. the best time to buy and sell stocks II
2022-07-25 05:27:00 【Tisfy】
【LetMeFly】122. The best time to buy and sell stocks II
Force button topic link :https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/
Give you an array of integers prices , among prices[i] Represents a stock i Sky price .
Every day , You can decide whether to buy and / Or sell shares . You at any time most Can only hold A wave of Stocks . You can also buy... First , And then in On the same day sell .
return What you can get Maximum profits .
Example 1:
Input :prices = [7,1,5,3,6,4]
Output :7
explain : In the 2 God ( Stock price = 1) Buy when , In the 3 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 .
And then , In the 4 God ( Stock price = 3) Buy when , In the 5 God ( Stock price = 6) Sell when , The exchange will make a profit = 6 - 3 = 3 .
The total profit is 4 + 3 = 7 .Example 2:
Input :prices = [1,2,3,4,5] Output :4 explain : In the 1 God ( Stock price = 1) Buy when , In the 5 God ( Stock price = 5) Sell when , The exchange will make a profit = 5 - 1 = 4 . The total profit is 4 .
Example 3:
Input :prices = [7,6,4,3,1] Output :0 explain : under these circumstances , There is no positive profit from trading , So you can get the maximum profit by not participating in the transaction , The biggest profit is 0 .
Tips :
1 <= prices.length <= 3 * 1040 <= prices[i] <= 104
Method 1 : greedy
In fact, this problem should be simple in medium difficulty .
Since you can buy stocks many times , that As long as I can earn , I'll take it .
Because at most one share is held at the same time , Therefore, in order not to affect my low price purchase behind , As long as you sell it, you can make money , I'll sell .
Then we only need to traverse the array , If tomorrow's stock is more expensive than today , Buy today and sell tomorrow .
That's it .
- Time complexity O ( N ) O(N) O(N), among N N N Is the number of days for which the stock amount is known ( p r i c e s . s i z e ( ) prices.size() prices.size()).
- Spatial complexity O ( 1 ) O(1) O(1)
AC Code
C++
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans = 0;
for (int i = 1; i < prices.size(); i++) {
if (prices[i] > prices[i - 1]) {
ans += prices[i] - prices[i - 1];
}
}
return ans;
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125868266
边栏推荐
- Solution of win11 blue screen code 0x0000001a
- Sword finger offer II 012. the sum of the left and right subarrays is equal
- TCL shows a number of folding screen mobile phones: the screen and hinge are independently developed!
- Wechat applet related operation examples
- Learning records [email protected] R & D effectiveness measurement indicators
- How to carry out the function test with simple appearance and complex interior?
- 单点登录(一处登录,处处可用)
- Obj file format and.Mtl file format
- 初步了解Panda3d粒子系统
- Add click event to unity 3D object
猜你喜欢
随机推荐
typora+PicGo+阿里云OSS 搭建以及报错解决【转载】
Performance Optimization: how to solve the slow loading speed of the first screen of spa single page application?
Sword finger offer special shock edition day 9
Delivery practice of private PAAS platform based on cloud native
Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
Pikachu vulnerability platform exercise
聊聊 Redis 是如何进行请求处理
Which side of Nacos has the SQL script of this column?
What content does the software test plan include and how to write it. Share test plan template
Project management tools - project developer tools
基环树入门
编程大杂烩(二)
background
The price is 17300! Why does Huawei mate x face Samsung fold?
The third day of rhcsa summer vacation
Redis cluster setup (Windows)
Unity中使用UniRx入门总结
Uniapp custom application exit execution content
Zhanrui's first 5g baseband chip was officially released and successfully ranked among the first tier of 5g!
Interface idempotency









