121. Best Time to Buy and Sell Stock
Easy
4229193Add to ListShare
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 (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.
Example 1:
Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 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 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution {
public:
/*只需要遍历一次数组,用一个变量记录遍历过数中的最小值,然后每次计算当前值和这个最小值之间的差值最为利润,然后每次选较大的利润来更新。当遍历完成后当前利润即为所求*/
int maxProfit(vector<int>& prices) {
if(prices.size()<=1) return 0;
int minnow=prices[0];
int res=0;
for(int i=1;i<prices.size();i++){
if(prices[i]-minnow>res) res=prices[i]-minnow;
minnow=min(minnow,prices[i]);
}
return res;
}
};
122. Best Time to Buy and Sell Stock II
Easy
17541599Add to ListShare
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 (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).
Example 1:
Input: [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution {
public:
/*这道题由于可以无限次买入和卖出。我们都知道炒股想挣钱当然是低价买入高价抛出,那么这里我们只需要从第二天开始,如果当前价格比之前价格高,则把差值加入利润中,因为我们可以昨天买入,今日卖出,若明日价更高的话,还可以今日买入,明日再抛出。以此类推,遍历完整个数组后即可求得最大利润。*/
int maxProfit(vector<int>& prices) {
int res=0;
if(prices.size()<=1) return res;
for(int i=0;i<prices.size()-1;i++){
if(prices[i+1]>prices[i])
res+=prices[i+1]-prices[i];
}
return res;
}
};
123. Best Time to Buy and Sell Stock III
Hard
172169Add to ListShare
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 (i.e., you must sell the stock before you buy again).
Example 1:
Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution {
public:
/*Runtime: 24 ms, faster than 10.68% of C++ online submissions for Best Time to Buy and Sell Stock III.
Memory Usage: 11.4 MB, less than 14.29% of C++ online submissions for Best Time to Buy and Sell Stock III.*/
/*假设,l[i][j]表示第i天卖出了第j支股票,局部最大值,
g[i][j]表示第i天共卖出了j支股票,全局最大值。
那么第i天的买卖情况:
(1)第i天卖出了今天刚买的,没赚
(2)第i天卖出了昨天刚买的,l[i][j]=g[i-1][j-1]+diff,
(3)第i天卖出了昨天以前买的股票,说明昨天没买,l[i][j]=l[i-1][j]+diff
g[i][j]=max(l[i][j],g[i-1][j]),今天交易了,或者今天没交易,取较大值*/
int maxProfit(vector<int>& prices) {
if (prices.empty()) return 0;
int n = prices.size();
vector<vector<int> > g(n,vector<int>(3)),l(n,vector<int>(3));
for (int i = 1; i < prices.size(); ++i) {
int diff = prices[i] - prices[i - 1];
for (int j = 1; j <= 2; ++j) {
l[i][j] = max(g[i - 1][j - 1], l[i - 1][j]) + diff;
g[i][j] = max(l[i][j], g[i - 1][j]);
}
}
return g[n - 1][2];
}
};
188. Best Time to Buy and Sell Stock IV
Hard
120074Add to ListShare
Say you have an array for which the i-th 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).
Example 1:
Input: [2,4,1], k = 2 Output: 2 Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
Example 2:
Input: [3,2,6,5,0,3], k = 2 Output: 7 Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
if(prices.size()==0) return 0;
//dp[i][k][0]=max(dp[i-1][k][0],dp[i-1][k][1]+p[i]);
//dp[i][k][1]=max(dp[i-1][k][1],dp[i-1][k-1][0]-p[i]);
if(k>prices.size()/2){
int dp_i_0=0,dp_i_1=-100000;
for(int i=0;i<prices.size();i++){
int tmp=dp_i_0;
dp_i_0=max(dp_i_0,dp_i_1+prices[i]);
dp_i_1=max(dp_i_1,tmp-prices[i]);
}
return dp_i_0;
}else{
vector<vector<vector<int> > > dp(prices.size(),vector<vector<int> >(k+1,vector<int>(2)));
for(int i=1;i<prices.size();i++){
dp[i][0][0]=0;dp[i][0][1]=-10000000;
for(int j=k;j>=1;j--){
dp[0][j][0]=0;dp[0][j][1]=-prices[0];
dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]+prices[i]);
dp[i][j][1]=max(dp[i-1][j][1],dp[i-1][j-1][0]-prices[i]);
}
}
return dp[prices.size()-1][k][0];
}
}
};