代码已在 leetcode 上验证通过。
通解方式,学习:https://leetcode-cn.com/circle/article/qiAgHn/
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k - 1][0] - prices[i])
121.买卖股票的最佳时机
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。
注意:你不能在买入股票前卖出股票。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() <= 0) return 0;
int profit0=0, profit1=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
profit0 = max(profit0, profit1+prices[i]);
profit1 = max(profit1, -prices[i]);
}
return profit0;
}
};
122.买卖股票的最佳时机 II
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() <= 0) return 0;
int profit0=0, profit1=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
int newProfit0 = max(profit0, profit1+prices[i]);
int newProfit1 = max(profit1, profit0-prices[i]);
profit0 = newProfit0;
profit1 = newProfit1;
}
return profit0;
}
};
123.买卖股票的最佳时机 III
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() <= 0) return 0;
int profitOne0=0, profitOne1=-prices[0];
int profitTwo0=0, profitTwo1=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
profitTwo0 = max(profitTwo0, profitTwo1+prices[i]);
profitTwo1 = max(profitTwo1, profitOne0-prices[i]);
profitOne0 = max(profitOne0, profitOne1+prices[i]);
profitOne1 = max(profitOne1, -prices[i]);
}
return profitTwo0;
}
};
188.买卖股票的最佳时机 IV
给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if (prices.size() <= 0) return 0;
if (k >= prices.size()/2) return maxProfitInf(prices);
vector<vector<int>> profit(k+1, vector<int>(2,0));
for (int i = 1; i <= k; i++) profit[i][1]=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
for (int j = k; j > 0; j--)
{
profit[j][0] = max(profit[j][0], profit[j][1]+prices[i]);
profit[j][1] = max(profit[j][1], profit[j-1][0]-prices[i]);
}
}
return profit[k][0];
}
int maxProfitInf(vector<int>& prices) {
if (prices.size() <= 0) return 0;
int profit0=0, profit1=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
profit0 = max(profit0, profit1+prices[i]);
profit1 = max(profit1, profit0-prices[i]);
}
return profit0;
}
};
309.最佳买卖股票时机含冷冻期
给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。
设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):
你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.size() <= 0) return 0;
int prevprofit0=0, profit0=0, profit1=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
int newProfit0 = max(profit0, profit1+prices[i]);
int newProfit1 = max(profit1, prevprofit0-prices[i]);
prevprofit0 = profit0;
profit0 = newProfit0;
profit1 = newProfit1;
}
return profit0;
}
};
714.买卖股票的最佳时机含手续费
给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;非负整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
if (prices.size() <= 0) return 0;
int profit0=0, profit1=-prices[0];
for (int i = 1; i < prices.size(); i++)
{
int newProfit0 = max(profit0, profit1+prices[i]-fee);
int newProfit1 = max(profit1, profit0-prices[i]);
profit0 = newProfit0;
profit1 = newProfit1;
}
return profit0;
}
};
附录
本人代码,买卖股票的最佳时期123.
1:只可卖一次
class Solution {
public:
int maxProfit(vector<int>& prices) {
int minPri=INT_MAX, maxPro=0;
for (int i = 0 ; i < prices.size(); i++)
{
minPri = min(minPri, prices[i]);
maxPro = max(maxPro, prices[i]-minPri);
}
return maxPro;
}
};
2:可以卖无限次
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxPro = 0;
for (int i = 1; i < prices.size(); i++)
if (prices[i] > prices[i-1])
maxPro += prices[i] - prices[i-1];
return maxPro;
}
};
3:只可以卖俩次
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxPro = 0;
int n = prices.size();
vector<int> first(n,0);
vector<int> second(n,0);
int minF=INT_MAX, maxS=INT_MIN;
int proF=INT_MIN, proS=INT_MAX;
for (int i = 0; i < n; i++)
{
minF = min(minF, prices[i]);
proF = max(proF, prices[i] - minF);
first[i] = proF;
}
for (int i = n-1; i >= 0; i--)
{
maxS = max(maxS, prices[i]);
proS = min(proS, prices[i] - maxS);
second[i] = proS;
}
for (int i = 0; i < n; i++)
maxPro = max(maxPro, first[i] - second[i]);
return maxPro;
}
};