一、121. Best Time to Buy and Sell Stock
1、题目描述
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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0
In this case, no transaction is done, i.e. max profit = 0.
2、解题思路:
只需要一层循环,用temp记录每相邻两天差值的和的变化,如果当累加和temp为负数时,temp设为0,表示股票卖掉,重新开始,用max记录temp变化过程中的最大值。
3、代码实现
class Solution {
public:
int maxProfit(vector<int>& prices) {
int temp=0;
int Max=0;
for(int i=1;i<prices.size();i++){
temp=max(0,temp+=prices[i]-prices[i-1]);
Max=max(Max,temp);
}
return Max;
}
};
二、122. Best Time to Buy and Sell Stock II
1、题目描述
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).
2、解题思路
贪心算法,只要后一天比前一天股票价格高,我们就在前一天买后一天卖,挣每相邻两天的差价。
3、代码实现
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()==0)return 0;
int count=0;
for(int i=0;i<prices.size()-1;i++){
if(prices[i+1]>prices[i])
count+=(prices[i+1]-prices[i]);
}
return count;
}
};
三、
Best Time to Buy and Sell Stock III
1、题目描述
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.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
2、解题思路
动态规划完成,也就是将第四部分中的交易k次具体化为2次。
3、代码实现
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1)
return 0;
vector<vector<int>> dp(3,vector<int>(n,0));
for (int i = 1; i <= 2; i++) {
int localMax = dp[i-1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = max(dp[i][j-1], prices[j] + localMax);
localMax = max(localMax, dp[i-1][j] - prices[j]);
}
}
return dp[2][n-1];
}
};
四、188. Best Time to Buy and Sell Stock IV
1、题目描述
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).
2、主要思想动态规划求解,设dp[i][j]表示截止到第j天,交易i次所得到的最大收益。
状态转移方程是:dp[i][j]=max{dp[i][j-1],dp[i-1][t]+prices[j]-prices[t]}=max{dp[i][j-1],prices[j]+max{dp[i-1][t]}}
max中的第一项表示第j天没有交易,也就是前i次交易全部在j-1天完成,第二项表示第i次交易在第j天完成,设第i次交易开始的日期为第t天,结束日期为第j天,那么第i次交易的收益为prices[j]-prices[t],再加上前t天完成的前i-1次收益的最大值,其中t的取值范围是[0,j-1]。注意代码中没有循环三次,而是巧妙的用变量localMax记录dp[i-1][t]-prices[t]中的最大值,其中t的取值范围为[0,j-1]。其中当k>=n/2的情况是为了处理边界值防止超时。
3、代码实现
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n = prices.size();
if (n <= 1)
return 0;
if (k >= n/2) {
int maxPro = 0;
for (int i = 1; i < n; i++) {
if (prices[i] > prices[i-1])
maxPro += prices[i] - prices[i-1];
}
return maxPro;
}
vector<vector<int>> dp(k+1,vector<int>(n,0));
for (int i = 1; i <= k; i++) {
int localMax = dp[i-1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = max(dp[i][j-1], prices[j] + localMax);
localMax = max(localMax, dp[i-1][j] - prices[j]);
}
}
return dp[k][n-1];
}
};