LeetCode——動態規劃——股票最大收益
阿新 • • 發佈:2018-11-26
股票最大收益
一、問題描述
給出每天股票的價格,設計一個演算法計算出最大收益。可以最多買賣兩個回合。而且賣出之後才能再買。
二、樣例
// 1
Input : [3, 3, 5, 0, 0, 3, 1, 4]
Output : 6
Explanation : 0-3, 1-4
// 2
Input : [1, 2, 3, 4, 5]
Output : 4
Explanation : 1-5
// 3
Input : [7, 6, 4, 3, 1]
Output : 0
Explanation : 0
三、解題思路
1、我的想法
構建一個大小為
陣列
,然後
表示以價格
買入,
賣出的收益。而
表示以價格
買入,然後以某價格賣出的最高收益。然後從每個大於零 的收益開始尋找最大收益,對於任意
,此時表示第一次交易,以
價格買入,
價格賣出,然後在此基礎上尋找第二次的最大收益,即尋找出
,
,對於每一行,都找到一個臨時最大值,最後得出最大收益。
很明顯時間複雜度
,空間複雜度
。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int length = prices.size();
vector<vector<int>> dp(length, vector<int>(length + 1, 0));
int max = 0;
for(int i = length - 1; i >= 0; -- i){
for(int j = i + 1; j < length; ++ j){
dp[i][j] = prices[j] - prices[i];
if(dp[i][j] > max)
max = dp[i][j];
}
dp[i][length] = max;
max = 0;
}
int temp;
for(int i = 0; i < length; ++ i){
for(int j = i + 1; j < length; ++ j){
if(dp[i][j] < 0)
continue;
temp = dp[i][j];
int temp_max = 0;
for(int k = j; k < length; ++ k){
if(temp_max < dp[k][length])
temp_max = dp[k][length];
}
temp += temp_max;
if(max < temp)
max = temp;
}
}
return max;
}
};
2、LeetCode童鞋答案
原連結
毫無疑問是用動態規劃,可如何能找到遞推式卻是一個永恆的問題。有時候比女盆友還難找。
表示總共進行
次交易,以第
天結尾時的最高收益,那遞推式
通過一定的程式碼優化,時間複雜度和空間複雜度為
class Solution {
public:
int maxProfit(vector<int>& prices) {
int length = prices.size();
if(length == 0)
return 0;
int k = 2, maximum = 0, tmp_max;
vector<vector<int>> dp(k+1, vector<int>(length, 0));
for(int i = 1; i < k+1; ++ i){
tmp_max = dp[i-1][0] - prices[0];
for(int j = 1; j < length; ++ j){
dp[i][j] = max(dp[i][j-1], prices[j] + tmp_max);
tmp_max = max(tmp_max, dp[i-1][j] - prices[j]);
maximum = max(dp[i][j], maximum);
}
}
return maximum;
}
};