# LeetCodes——Best Time to Buy and Sell Stock【系列问题】

Best Time to Buy and Sell Stock系列问题在面试过程中经常遇到，这里将下面几种问题做一个总结。
1.121 Best Time to Buy and Sell Stock
2.122 Best Time to Buy and Sell Stock II
3.123 Best Time to Buy and Sell Stock III
4.188 Best Time to Buy and Sell Stock IV
5.309 Best Time to Buy and Sell Stock with Cooldown
6.714 Best Time to Buy and Sell Stock with Transaction Fee

### 121 Best Time to Buy and Sell Stock 题目要求

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==0){
return 0;
}
int res = 0;
int mmin = prices[0];
for (int i=1;i<n;i++){
res = max(res,prices[i]-mmin);
mmin = min(mmin,prices[i]);
}
return res;
}
};

200 / 200 test cases passed.
Status: Accepted
Runtime: 8 ms

### 122 Best Time to Buy and Sell Stock II 题目要求

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) {
if (prices.size()<2){
return 0;
}
int maxPro = 0;
for (int i=1;i<prices.size();i++){
if(prices[i]-prices[i-1]>0){
maxPro+=prices[i]-prices[i-1];
}
}
return maxPro;
}
};

198 / 198 test cases passed.
Status: Accepted
Runtime: 12 ms

### 123 Best Time to Buy and Sell Stock III题目要求

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) {
//遍历，计算出第x天之前一次交易可以得到的最大收益，存储在formmer[n]中
//遍历，计算出第x天之后一次交易可以得到的最大收益，存储在latter[n]中
//遍历，计算formmer[i] + latter[i] 得到最大收益max
if (prices.size()<2){
return 0;
}
int n = prices.size();
vector<int> former(n,0);
vector<int> latter(n,0);
int minPrices = INT_MAX;
int maxPro = 0;
for (int i=0;i<n;i++){
minPrices = min(minPrices,prices[i]);
maxPro = max(maxPro,prices[i]-minPrices);
former[i] = maxPro;
}
int maxPrices = INT_MIN;
int maxPro1 = 0;
for (int i=n-1;i>=0;i--){
maxPrices = max(maxPrices,prices[i]);
maxPro1 = max(maxPro1,maxPrices-prices[i]);
latter[i] = maxPro1;
}
int res = 0;
for (int i=0;i<n;i++){
res = max(res,former[i]+latter[i]);
}
return res;
}
};

198 / 198 test cases passed.
Status: Accepted
Runtime: 8 ms

### 188 Best Time to Buy and Sell Stock IV 题目要求

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.

### 代码

class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int len = prices.size();
if(len == 0){
return 0;
}else if (len/2 < k){
return quickSolve(prices);
}
vector<int> sell(k+1,0);//第k次交易卖出的最大收益；
for (int i=0;i<len;i++){
for (int j=1;j<=k;j++){
}
}
return sell[k];
}
int quickSolve(vector<int>& prices) {
int len = prices.size(), profit = 0;
for (int i = 1; i < len; i++)
// as long as there is a price gap, we gain a profit.
if (prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
return profit;
}
};

211 / 211 test cases passed.
Status: Accepted
Runtime: 7 ms

### 309. Best Time to Buy and Sell Stock with Cooldown题目要求

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:
1.You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
2.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

### 代码

class Solution {//状态切换
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0;
int sell = 0;
int rest = 0;
int prev_sell = sell;
int prev_rest = rest;
for (int i=0;i<prices.size();i++){
prev_sell = sell;
prev_rest = rest;
}
return sell;
}
};

211 / 211 test cases passed.
Status: Accepted
Runtime: 10 ms

### 714 Best Time to Buy and Sell Stock with Transaction Fee题目要求

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.
You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)
Return the maximum profit you can make.

### 解题思路

s0[i] = max(s0[i-1], s1[i-1]+prices[i]);
s1[i] = max(s1[i-1], s0[i-1]-prices[i]-fee);

### 代码

class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
//s0 = profit having no stock
//s1 = profit having 1 stock
int s0 = 0, s1 = INT_MIN;
for(int p:prices) {
int tmp = s0;
s0 = max(s0, s1+p);
s1 = max(s1, tmp-p-fee);
}
return s0;
}
};

44 / 44 test cases passed.
Status: Accepted
Runtime: 139 ms