最佳时间买卖股票问题在leetcode中出现了很多次,并且这些题目之间也具有相似性,所以我将它们做成一个专题来进行总结。
Say you have an array for which the i t h i^th ith element is the price of a given stock on day i i i.
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
这是一道比较简单的编程题,也是这系列题中的一道入门题。题目大意是只准完成一项交易,求利润最大值。我们需要用到两个变量release1和hold1,其中release1代表的是当前利润最大值;hold1代表的是当前股票最低价。这样就可以用动态规划来求解。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int hold1 = INT_MIN, release1 = 0;
for(int i : prices) {
release1 = max(release1, hold1 + i);
hold1 = max(hold1, -i);
}
return release1;
}
};
Say you have an array for which the i t h i^th ith element is the price of a given stock on day i i i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
这道题在第一题的基础上不限制交易次数。没有交易次数的限制后,只要比前一天股票价格高就是盈利。
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() == 0) return 0;
int profit = 0;
for(int i = 1; i < prices.size(); i++) {
if(prices[i] > prices[i-1]) profit = profit + (prices[i] - prices[i-1]);
}
return profit;
}
};
Say you have an array for which the i t h i^th ith element is the price of a given stock on day i i 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 (i.e., you must sell the stock before you buy again).
这道题也是和第一题的思路类似,区别在于第一题是只能交易一次,这道题是最多交易两次。我们需要用到四个变量release1、release2和hold1、hold1,其中release1、hold1和第一题含义一样;hold2的含义就是在交易一次的基础上再买股票的最佳入手时机、release2则表示交易两次的最大利润。用动态规划来求解。
class Solution {
public:
int maxProfit(vector<int>& prices) {
int hold1 = INT_MIN, hold2 = INT_MIN;
int release1 = 0, release2 = 0;
for(int i:prices){
release2 = max(release2, hold2+i);
hold2 = max(hold2, release1-i);
release1 = max(release1, hold1+i);
hold1 = max(hold1, -i);
}
return release2;
}
};
Say you have an array for which the i t h i^th ith element is the price of a given stock on day i i i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
是第三题的一个推广,但是提交的时候可能会出现一个内存溢出的错误。原因是k值远远大于prices大小,这样用dp就不合理了。所以需要结合第二题的思路来进行解决。
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) {
if(k == 0) return 0;
if(k >= prices.size()) {
int profit = 0;
for(int i = 1; i < prices.size(); i++) {
if(prices[i] > prices[i-1]) profit = profit + (prices[i] - prices[i-1]);
}
return profit;
}
vector<int> hold(k, INT_MIN);
vector<int> release(k, 0);
for(int i : prices){
for(int j = k - 1; j >= 1; j--) {
release[j] = max(release[j], hold[j] + i);
hold[j] = max(hold[j], release[j - 1] - i);
}
release[0] = max(release[0], hold[0] + i);
hold[0] = max(hold[0], -i);
}
return release[k-1];
}
};