121.
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n < )
return ;
int mini = prices[], ans = , i;
for(i = ; i < n; i++)
{
if(prices[i]-mini > ans)
ans = prices[i]-mini;
if(prices[i] < mini)
mini = prices[i];
}
return ans;
}
};
122.
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n < )
return ;
int mini = prices[], maxi = prices[], ans = , i;
for(i = ; i < n; i++)
{
if(prices[i] > maxi)
maxi = prices[i];
else if(prices[i] < maxi)
{
ans += maxi - mini;
maxi = mini = prices[i];
}
}
ans += maxi - mini;
return ans;
}
};
123.
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n < )
return ;
vector<int> forward(n, ), backward(n, );
int mini, maxi, ans, i;
forward[] = ;
mini = prices[];
for(i = ; i < n; i++)
{
forward[i] = max(forward[i-], prices[i] - mini);
if(prices[i] < mini)
mini = prices[i];
}
backward[n-] = ;
maxi = prices[n-];
for(i = n-; i >= ; i--)
{
backward[i] = max(backward[i+], maxi - prices[i]);
if(prices[i] > maxi)
maxi = prices[i];
}
ans = ;
for(i = ; i < n; i++)
{
ans = max(ans, forward[i] + backward[i]);
}
return ans;
}
};
188.
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n = prices.size(), i, j;
if(n < )
return ;
if(k >= (n>>))
{
int ans = ;
for(i = ; i < n-; i++)
{
if(prices[i+]-prices[i] > )
ans += prices[i+]-prices[i];
}
return ans;
}
vector<int> buy(k+, INT_MIN), sell(k+, );
for(i = ; i < n; i++)
{
for(j = ; j <= k; j++)
{
buy[j] = max(buy[j], sell[j-] - prices[i]);
sell[j] = max(sell[j], buy[j] + prices[i]);
}
}
return sell[k];
}
};
buy[i]表示买i个最多剩多少钱。sell[i]表示卖i个最多有多少钱。
buy[j] = max(buy[j], sell[j-1] - prices[i]); //看买prices[i]是否有原来划算
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n = prices.size(), i, j;
if(n < )
return ;
if(k >= (n>>))
{
int ans = ;
for(i = ; i < n-; i++)
{
if(prices[i+]-prices[i] > )
ans += prices[i+]-prices[i];
}
return ans;
}
vector<vector<int>> dp(n, vector<int>(k+, )); //dp[i][j]表示到第i天卖j个最多赚多少钱
for(i = ; i <= k; i++)
{
int buy = -prices[];
for(j = ; j < n; j++)
{
dp[j][i] = max(j > ? dp[j-][i] : , buy + prices[j]);
buy = max(buy, dp[j][i-] - prices[j]);
}
}
return dp[n-][k];
}
};
和上面一个算法思路一样。
309.
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
- You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
- After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n <= )
return ;
vector<int> sell(n+, );
int buy = -prices[], i;
for(i = ; i <= n; i++)
{
sell[i] = max(sell[i-], buy + prices[i-]);
buy = max(buy, sell[i-] - prices[i-]);
}
return sell[n];
}
};
sell[i-2]表示cooldown[i-1]。