// Best Time to Buy and Sell Stock I class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int mj = 0, mjIdx = 0; int ret = 0; for(int i = 1; i < prices.size(); ++i) { if(prices[i] >= prices[i-1]) { mj = prices[i] - prices[mjIdx]; } else { if(prices[i] < prices[mjIdx]) { mjIdx = i; } } ret = max(ret, mj); } return ret; } }; // Best Time to Buy and Sell Stock II class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int ms = 0; int ret = 0; for(int i = 1; i < prices.size(); ++i) { if(prices[i] >= prices[i-1]) { ms += prices[i] - prices[i-1]; } else { ret += ms; ms = 0; } } ret += ms; return ret; } }; // Best Time to Buy and Sell Stock III class Solution { public: int maxProfit(vector<int> &prices) { if(prices.size() < 2) return 0; int n = prices.size(); vector<int> l(n); vector<int> r(n); int minv = prices[0]; l[0] = 0; for(int i = 1; i < n; ++i) { minv = min(minv, prices[i]); l[i] = max(l[i-1], prices[i] - minv); } int maxv = prices[n-1]; r[n-1] = 0; for(int i = n-2; i >= 0; --i) { maxv = max(maxv, prices[i]); r[i] = max(r[i+1], maxv - prices[i]); } int ret = 0; for(int i = 0; i < n; ++i) { ret = max(ret, l[i] + r[i]); } return ret; } };
欢迎关注微信公众号——计算机视觉
相关推荐
《最佳买卖股票时机II》是LeetCode中的一道经典编程题目,主要涉及动态规划和数组操作的知识点。在这个问题中,我们被赋予一个整数数组`prices`,表示某支股票每天的价格。任务是找到在允许进行多次交易的情况下,...
java java_leetcode题解之Best Time to Buy and Sell Stock I.java
java java_leetcode题解之Best Time to Buy and Sell Stock II.java
java java_leetcode题解之Best Time to Buy and Sell Stock III.java
java java_leetcode题解之Best Time to Buy and Sell Stock with Cooldown
java java_leetcode题解之Best Time to Buy and Sell Stock IV.java
python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III
javascript js_leetcode题解之123-best-time-to-buy-and-sell-stock-iii.js
python python_leetcode题解之122_Best_Time_to_Buy_and_Sell_Stock_II
javascript js_leetcode题解之122-best-time-to-buy-and-sell-stock-ii.js
java lru leetcode leetcode-java leetcode刷题笔记 ...III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV 217.Contains Duplicate 263.Ugly Number 264.Ugly Number II
Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖股票的最佳时机Ⅳ Best Time to Buy and ...
python python_leetcode题解121_Best_Time_to_Buy_and_Sell_Stock
javascript js_leetcode题解之121-best-time-to-buy-and-sell-stock.js
- Best Time to Buy and Sell Stock III:存在多日限制交易,最多两次交易。 - Best Time to Buy and Sell Stock IV:与前三者不同,可能需要多次买入和卖出。 - Best Time to Buy and Sell Stock with Cooldown...
股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock ...一次交易,找最大收益 ...i ...i) ...i-low) ...i-buy1) #找第一次交易最大收益 buy2 = min(buy2, i-prof1) #找用第一次收益购买的股票仍
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
It is written to help C++ developers of all skill levels and shows by example how to write understandable, flexible, maintainable, and efficient C++ code. Even if you are a seasoned C++ developer, ...