# LeetCode 买卖股票的合适时间

Title: 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.

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.

class Solution {
public:
int maxProfit(vector<int>& prices) {

int n = prices.size(), buy = INT_MIN, sell = 0;
if(n==0) return 0;

for(int i=0; i<n; i++)
{
buy =  max(buy, -prices[i]);
sell = max(sell, prices[i] + buy);
}
return sell;
}
};

Title: 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) {

int buy = INT_MIN, sell = 0, n = prices.size();
if(n==0) return 0;

for(int i=0; i<n; i++)
{
buy = max(buy, sell-prices[i]);
sell = max(sell, buy + prices[i]);
}
return sell;
}
};

Title: 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.

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(vector<int>& prices) {

int n = prices.size();
if(n==0) return 0;
int sell1 = 0, sell2 = 0, buy1 = INT_MIN, buy2 = INT_MIN;

for(int i =0; i<n; i++)
{
buy1 = max(buy1, -prices[i]);
sell1 = max(sell1, prices[i]+buy1);
buy2 = max(buy2, sell1-prices[i]);
sell2 = max(sell2, prices[i]+buy2);
}
return sell2;
}
};

Title： 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.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

1. k > n/2 时，可归为上述样例二中的情况（即可进行无限次交易);
2. k <= n/2 时， 将每次的买入、卖出利润保存在数组中（与动态规划相结合）。

class Solution {
public:
int maxProfit(int k, vector<int>& prices) {

int n = prices.size();

if(k>n/2)
{
int buy = INT_MIN, sell = 0;
for(int i=0; i<n; i++)
{
buy = max(buy, sell-prices[i]);
sell = max(sell, buy+prices[i]);
}
return sell;
}

vector<int> sell(k+1, 0);
vector<int> buy(k+1, 0);

for(int i=0; i<=k; i++) buy[i] = INT_MIN;

for(int i=0; i<n; i++)
{
for(int j=1; j<k+1; j++)
{

buy[j] = max(buy[j], sell[j-1]-prices[i]);
sell[j] = max(sell[j], buy[j]+prices[i]);

}
}
return sell[k];
}
};

Title： 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:

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(), buy = INT_MIN, sell = 0, sell_pre = 0;
for(int i=0; i<n; i++)
{
int old_sell = sell;
buy = max(buy, sell_pre - prices[i]);
sell = max(old_sell, buy + prices[i]);
sell_pre = old_sell;
}
return sell;
}
};

Title：Best Time to Buy and Sell Stock with Transaction Fee

our 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.

Example 1:
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
Buying at prices = 1
Selling at prices = 8
Buying at prices = 4
Selling at prices = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {

int n = prices.size(), buy = INT_MIN, sell = 0;
for(int i=0; i<n; i++)
{
buy = max(buy, sell-fee-prices[i]);
sell = max(sell, buy+prices[i]);
}
return sell;
}
};