当前位置:网站首页>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
边栏推荐
- Zhou Chen, vice president of zhanrui market, responded to everything about 5g chip chunteng 510!
- TCL shows a number of folding screen mobile phones: the screen and hinge are independently developed!
- Which side of Nacos has the SQL script of this column?
- Add transition layer to JS page
- Terminate 5g chip cooperation! The official response of Intel and zhanrui came
- The third day of rhcsa summer vacation
- FinClip实现微信授权登录的三种方案
- Solve the problem that uni app applet obtains routes and route parameters
- Single sign on (one sign on, available everywhere)
- 2022-07-24:以下go语言代码输出什么?A:[]int{};B:[]int(nil);C:panic;D:编译错误。 package main import ( “fmt“ ) f
猜你喜欢

Odoo14 | about the abnormal display of statusbar keyword after use and Its Solutions

Airserver 7.3.0 Chinese version mobile device wireless transmission computer screen tool

Necessary skills for mobile terminal test: ADB command and packet capturing

systemverilog中function和task区别

Project management tools - project developer tools

传输线理论之相速、相位等的概念

自己实现is_class

Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies

Preliminary understanding of Panda3D particle system

Easyrecovery free data recovery tool is easy to operate and restore data with one click
随机推荐
1310_ Implementation analysis of a printf
编程大杂烩(二)
What is virtual DOM? How to implement a virtual DOM
STL notes (I): knowledge system
微服务 - 网关Gateway组件
The global shipment of glory 8x series exceeded 10million units, and the glory V20 exceeded 1.5 million units!
编程大杂烩(一)
STL notes (II): template and operator overloading
Solution of win11 blue screen code 0x0000001a
2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f
nacos中哪边有这个列的sql脚本啊?
Odoo14 | about the abnormal display of statusbar keyword after use and Its Solutions
Delivery practice of private PAAS platform based on cloud native
Anshi semiconductor management paid a visit to Wentai technology and Gree Electric appliance
STL notes (III): input / output stream
微服务 - Hystrix 熔断器
STL notes (VI): container vector
The difference between $write and $display in SystemVerilog
Add click event to unity 3D object
聊聊 Redis 是如何进行请求处理