Best Time to Buy and Sell Stock系列分析
- Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
題目描述:只能進行一次交易
那麼我們找到差值最大的兩個數,並且後面的值比較大即可。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size= prices.size();
int profit = 0;
for(int i=0;i<size;i++){
for(int j=i+1;j<size;j++){
if((prices[j] - prices[i])>profit){
cout<<i<<" "<<j<<endl;
profit = prices[j] - prices[i];
}
}
}
return profit;
}
};
- Best Time to Buy and Sell Stock II
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/
題目描述:可以進行不止一次交易,但每次交易不能重疊
如果後一個值比前一個值大,我們就進行一次交易,得到區域性最優值之和。
區域性最優值>=全域性最優值
class Solution {
public:
int maxProfit(vector<int>& prices) {
int size= prices.size();
int profit = 0;
for(int i=1;i<size;i++){
if(prices[i-1]<prices[i]){
cout<< prices[i]<<" "<<prices[i-1]<<endl;
profit += (prices[i]-prices[i-1]);
}
}
return profit;
}
};
- Best Time to Buy and Sell Stock III
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/
題目描述:明確規定最多隻能進行兩次交易
將問題拆分成兩個子問題,第0天到第cc天,第cc+1天到最後一天
class Solution {
public:
int maxProfit(vector<int>& prices) {
const int size = prices.size();
if(size<2){
return 0;
}
int profit1[size+1], profit2[size+1];
int minprice = prices[0];
int maxprice = prices[size-1];
profit1[0] = 0;
for(int i=1;i<size;i++){
profit1[i] = max(profit1[i-1], prices[i] - minprice);
if(minprice > prices[i]){
minprice = prices[i];
}
}
profit2[size-1] = 0;
for(int i=size-2;i>=0;i--){
profit2[i] = max(profit2[i+1], maxprice - prices[i]);
if(maxprice < prices[i]){
maxprice = prices[i];
}
}
int global = 0;
for(int i=0;i<size;i++){
global = max(global, profit1[i]+profit2[i]);
}
return global;
}
};
- Best Time to Buy and Sell Stock IV
來自 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/discuss/
題目描述:明確規定最多隻能進行k次交易
將問題拆分成k個子問題,第0天到第cc天,第cc+1天到最後一天
用global和local來更新,考慮到第j天的交易可能跟第j-1天重複了 本來是1次交易但記錄成兩次
另外,如果k很大的時候,退化成第二種問題。注意,當k很大的時候,我們開陣列的時候要小心,不然會爆掉,然後Runtime error
local[j] = max(global[j-1]+max(diff,0), local[j]+diff);
global[j] = max(local[j], global[j]);
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int size = prices.size();
if(size<2){
return 0;
}
if(k>=size){
return solveMaxProfit(prices);
}
int local[k+1]={0};//k太大的時候 建立陣列的時候爆掉了···
int global[k+1]={0};
for(int i=0;i<size-1;i++){
int diff = prices[i+1]-prices[i];
for(int j=k;j>=1;j--){
local[j] = max(global[j-1]+max(diff,0), local[j]+diff);
global[j] = max(local[j], global[j]);
}
}
return global[k];
}
int solveMaxProfit(vector<int>& prices) {
int size = prices.size();
int result = 0;
for(int i=1;i<size;i++){
if(prices[i] - prices[i-1]>0){
result += prices[i]-prices[i-1];
}
}
return result;
}
};
相關文章
- Best Time to Buy and Sell Stock系列
- 121|Best Time to Buy and Sell Stock
- [leetcode]Best Time to Buy and Sell StockLeetCode
- 121. Best Time to Buy and Sell Stock
- 貪心法-Best Time to Buy and Sell Stock
- [LeetCode] 121. Best Time to Buy and Sell StockLeetCode
- leetcode_best-time-to-buy-and-sell-stock-iiLeetCode
- LeetCode 309. Best Time to Buy and Sell Stock with CooldownLeetCode
- leetcode best-time-to-buy-and-sell-stock-iii(Java)LeetCodeJava
- [LeetCode] 122. Best Time to Buy and Sell Stock IILeetCode
- 【Lintcode】393. Best Time to Buy and Sell Stock IV
- 【leetcode】40-best-time-to-buy-and-sell-stock 力扣 121. 買賣股票的最佳時機LeetCode力扣
- 42-best-time-to-buy-and-sell-stock-iii 力扣 123. 買賣股票的最佳時機 III力扣
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳買賣股票時機含冷凍期(Medium)(JAVA)LeetCodeJava
- 44-best-time-to-buy-and-sell-stock-with-cooldown 力扣 309. 買賣股票的最佳時機包含冷凍期力扣
- Time Series Analysis (Best MSE Predictor & Best Linear Predictor)
- [LeetCode] 2073. Time Needed to Buy TicketsLeetCode
- 【PAT_1062】To Buy or Not to Buy
- use_sim_time-ROS系列ROS
- DedeCMS最新通殺注入(buy_action.php)漏洞分析PHP
- TTEC遭勒索軟體攻擊後影響客戶業務 包括Verizon、Best Buy、美國銀行等
- SAP Stock Inconsistency
- The best LeetCode NodesLeetCode
- Best Team With No Conflicts
- SELL 指令碼程式設計指令碼程式設計
- Best Wishes「兔」You!
- Ghost Push —— Monkey Test & Time Service病毒分析報告
- 大資料分析筆記 (7) - 時間序列分析(Time Series Analysis)大資料筆記
- 攻防世界-best_rsa
- 25 Best Java Books In 2022Java
- 題解:P10688 Buy Tickets
- 第 74 期 time.Timer 原始碼分析 (Go 1.14)原始碼Go
- time time_t tm用法
- How to Buy a Fake Cal Poly Pomona Diploma Quickly?UI
- The Best Way to Export an SVG from SketchExportSVG
- [ARC060F] Best Representation
- 矩陣樹定理 BEST 定理矩陣
- iOS使用Instrument Time Profiler工具分析和優化效能問題iOS優化