当前位置:网站首页>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
边栏推荐
- C编程 --“最大子数组的和” 的动态规划的解法
- Odoo14 | about the abnormal display of statusbar keyword after use and Its Solutions
- Easyrecovery free data recovery tool is easy to operate and restore data with one click
- Three billion dollars! Horizon becomes the world's highest valued AI chip Unicorn
- Guanghetong and Intel released the global version of 5g communication module
- ping命令
- Panda3D keyboard moving scene
- 聊聊 Redis 是如何进行请求处理
- Openfegin remote call lost request header problem
- 项目管理工具——项目开发者工具
猜你喜欢

自己实现is_convertible

ThreadLocal

Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems

Wechat applet related operation examples

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

自己实现is_base_of

微信小程序相关操作示例

rhce第一天
![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](/img/bf/e38a8fd813f88a83f61a1abfa3b95d.png)
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

项目管理工具——项目开发者工具
随机推荐
Shenzhen on call test, subject 4 on call test, subject 3 theory, second theory on call test instructions
AirServer 7.3.0中文版手机设备无线传送电脑屏幕工具
Redis cluster setup (Windows)
接口幂等性
Oracle split branches
Deep error
STL notes (VII): container deque
TCL shows a number of folding screen mobile phones: the screen and hinge are independently developed!
Event cycle mechanism browser nodejs async await execution sequence promise execution sequence interview questions
JS common code questions array de duplication - Custom New throttling and anti shake - deep copy - instanceof URL parameter extraction - thousand separator - array to tree structure - array flattening
Bypass XSS filters in Web Applications
STL notes (II): template and operator overloading
Unity中使用UniRx入门总结
Implement is by yourself_ convertible
Terminate 5g chip cooperation! The official response of Intel and zhanrui came
基环树入门
LeetCode第302场周赛
Learning records [email protected] R & D effectiveness measurement indicators
学习记录[email protected]研发效能度量指标
剑指offer专项突击版第9天