leetcode 买卖股票【六题集合】

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

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 (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& prices) {
        int buy;
        int sell;
        if (prices.size() <= 1) {
            return 0;
        }
        sell = 0;
        buy = -prices[0];
        for (int i = 1; i < prices.size(); i ++) {
            buy = max(buy, sell-prices[i]);
            sell = max(sell, buy+prices[i]);
        }
        return sell;
    }
};

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.

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:
    int maxProfit(vector& prices) {
        vectordp[4];
        int len = prices.size();
        if (len <= 1) {
            return 0;
        }
        dp[0].push_back(-prices[0]);
        dp[1].push_back(-0x3f3f3f);
        dp[2].push_back(-0x3f3f3f);
        dp[3].push_back(-0x3f3f3f);
        for (int i = 1; i < len; i ++) {
            dp[0].push_back(max(dp[0][i-1], -prices[i]));
            dp[1].push_back(max(dp[0][i-1] + prices[i], dp[1][i-1]));
            dp[2].push_back(max(dp[1][i-1] - prices[i], dp[2][i-1]));
            dp[3].push_back(max(dp[2][i-1] + prices[i], dp[3][i-1]));
        }
        return max(0,max(dp[3][len-1],dp[1][len-1]));
    }
};

188. Best Time to Buy and Sell Stock IV

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.

k大于天数的时候就是随便次数了!!!

class Solution {
public:
    vectordp;
    int maxProfit(int k, vector& prices) {
        int len = prices.size();
        if (len <= 1) {
            return 0;
        }
        if (len / 2 <= k) {
            int buy = -prices[0];
            int sell = 0;
            for (int i = 1; i < prices.size(); i ++) {
                buy = max(buy, sell-prices[i]);
                sell = max(sell, buy+prices[i]);
            }
            return sell;
        }
        dp.clear();
        dp.push_back(-prices[0]);
        for (int i = 1; i < k * 2; i ++) {
            dp.push_back(-0x3f3f3f3f);
        }
        for (int i = 1; i < len; i ++) {
            dp[0] = max(dp[0], -prices[i]);
            for (int j = 1; j < k*2; j ++) {
                if(j%2) {
                    dp[j] = max(dp[j-1] + prices[i], dp[j]);
                } else {
                    dp[j] = max(dp[j-1] - prices[i], dp[j]);
                }
            }
        }
        int tot = 0;
        for (int j = 0; j < k; j ++) {
            tot = max(tot, dp[j*2+1]);
        }
        return tot;
    }
};

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:

  • 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:

Input: [1,2,3,0,2]
Output: 3 
Explanation: transactions = [buy, sell, cooldown, buy, sell]

也可以减小数组大小,还没改QAQ

class Solution {
public:
    int maxProfit(vector& prices) {
        vectorbuy;
        vectorsell;
        if (prices.size() <= 1) {
            return 0;
        }
        sell.push_back(0);
        buy.push_back(-prices[0]);
        for (int i = 1; i < prices.size(); i ++) {
            if (i > 1)
                buy.push_back(max(buy[i-1], sell[i-2]-prices[i]));
            else
                buy.push_back(max(-prices[0],-prices[1]));
            sell.push_back(max(sell[i-1], buy[i-1]+prices[i]));
        }
        return sell[prices.size()-1];
    }
};

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.

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[0] = 1
  • Selling at prices[3] = 8
  • Buying at prices[4] = 4
  • Selling at prices[5] = 9
  • The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

     

    Note:

  • 0 < prices.length <= 50000.
  • 0 < prices[i] < 50000.
  • 0 <= fee < 50000.
class Solution {
public:
    int maxProfit(vector& prices, int fee) {
        vectorbuy;
        vectorsell;
        if (prices.size() <= 1) {
            return 0;
        }
        sell.push_back(0);
        buy.push_back(-prices[0]-fee);
        for (int i = 1; i < prices.size(); i ++) {
            buy.push_back(max(buy[i-1], sell[i-1]-prices[i]-fee));
            sell.push_back(max(sell[i-1], buy[i-1]+prices[i]));
        }
        return sell[prices.size()-1];
    }
};

 

你可能感兴趣的:(leetcode)