zoukankan      html  css  js  c++  java
  • Best Time to Buy and Sell Stock

    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.
    
     

    Approach #1: C++. [one pass]

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int min_price = INT_MAX;
            int max_profit = 0;
            
            for (int i = 0; i < prices.size(); ++i) {
                if (min_price > prices[i])
                    min_price = prices[i];
                else if (prices[i] - min_price > max_profit)
                    max_profit = prices[i] - min_price;
            }
            
            return max_profit;
        }
    };
    

      

    Approach #2: C++. [Kadane's Algorithm]

        public int maxProfit(int[] prices) {
            int maxCur = 0, maxSoFar = 0;
            for(int i = 1; i < prices.length; i++) {
                maxCur = Math.max(0, maxCur += prices[i] - prices[i-1]);
                maxSoFar = Math.max(maxCur, maxSoFar);
            }
            return maxSoFar;
        }
    

      

    Approach #3: C++. [DP]

    public int maxProfit(int[] prices) {
        int T_i10 = 0, T_i11 = Integer.MIN_VALUE;
            
        for (int price : prices) {
            T_i10 = Math.max(T_i10, T_i11 + price);
            T_i11 = Math.max(T_i11, -price);
        }
            
        return T_i10;
    }
    

      

      

    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.

    Approach #1: C++. 

    class Solution {
        public int maxProfit(int[] prices) {
            int i = 0;
            int valley = prices[0];
            int peak = prices[0];
            int maxProfit = 0;
            while (i < prices.length - 1) {
                while (i < prices.length - 1 && prices[i] >= prices[i+1])
                    ++i;
                valley = prices[i];
                while (i < prices.length - 1 && prices[i] <= prices[i+1])
                    ++i;
                peak = prices[i];
                maxProfit += peak - valley;
            }
            return maxProfit;
        }
    }
    

      

    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.

    Approach #1: C++. [DP]

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int T_i10 = 0, T_i11 = INT_MIN;
            int T_i20 = 0, T_i21 = INT_MIN;
            
            for (int p : prices) {
                T_i20 = max(T_i20, T_i21 + p);
                T_i21 = max(T_i21, T_i10 - p);
                T_i10 = max(T_i10, T_i11 + p);
                T_i11 = max(T_i11, -p);
            }
            
            return T_i20;
        }
    };
    

      

    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.

    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.

    Approach #2: C++. [DP]

    class Solution {
    public:
        int maxProfit(int k, vector<int>& prices) {
            int size = prices.size();
            if (k >= size >> 1) {
                int T_ik0 = 0, T_ik1 = INT_MIN;
                for (int p : prices) {
                    int old_T_ik0 = T_ik0;
                    T_ik0 = max(T_ik0, T_ik1 + p);
                    T_ik1 = max(T_ik1, old_T_ik0 - p);
                }
                
                return T_ik0;
            }
            
            vector<int> T_ik0(k+1, 0);
            vector<int> T_ik1(k+1, INT_MIN);
            for (int p : prices) {
                for (int j = k; j > 0; --j) {
                    T_ik0[j] = max(T_ik0[j], T_ik1[j] + p);
                    T_ik1[j] = max(T_ik1[j], T_ik0[j-1] - p);
                }
            }
            
            return T_ik0[k];
        }
    };
    

      

    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]

    Approach #1: C++. [DP]

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int T_ik0 = 0, T_ik1 = INT_MIN, T_ik0_pre = 0;
            
            for (int p : prices) {
                int old_T_ik0 = T_ik0;
                T_ik0 = max(T_ik0, T_ik1 + p);
                T_ik1 = max(T_ik1, T_ik0_pre - p);
                T_ik0_pre = old_T_ik0;
            }
            
            return T_ik0;
        }
    };
    

      

    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.

    Approach #1: C++.

    class Solution {
    public:
        int maxProfit(vector<int>& prices, int fee) {
            long T_ik0 = 0, T_ik1 = INT_MIN;
            
            for (int p : prices) {
                int old_T_ik0 = T_ik0;
                T_ik0 = max(T_ik0, T_ik1 + p - fee);
                T_ik1 = max(T_ik1, old_T_ik0 - p);
            }
            
            return T_ik1;
        }
    };
    

      

    Analysis:

    https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    layui 3种导航栏
    SQL语句内做除法得出百分比
    JS 日期比较方法
    JDK 13 的 12 个新特性,真心涨姿势了
    怎么实现单点登录?面试必问!
    厉害了,Apache架构师们遵循的 30 条设计原则
    面试官问线程安全的List,看完再也不怕了!
    Java 类在 Tomcat 中是如何加载的?
    Redis 21问,你接得住不?
    String hashCode 这个数字,很多人不知道!
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10226593.html
Copyright © 2011-2022 走看看