LeetCode刷题 Day51

87 阅读1分钟

LeetCode刷题 Day51

714. Best Time to Buy and Sell Stock with Transaction Fee

You are given an array prices where prices[i] is the price of a given stock on the ith day, and an integer fee representing a transaction fee.

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

Note:  You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

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.

Example 2:

Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6

思路: 1 trans = 1 buy + 1 sell, 所以在sell的时候交手续费可以最大化交易

步骤:

  1. dp index和value: index 代表ith day, [i][0]代表持有,[i][1]代表不持有
  2. 递推公式: dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]) dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
  3. 初始化: dp[0][0] = -prices[0]; 第0天持有 要减去股票费用
  4. 遍历顺序: 顺序

代码:

var maxProfit = function(prices, fee) {
    let length = prices.length;
    let dp = Array(length).fill(0).map(item => Array(2).fill(0));
    dp[0][0] -= prices[0];

    for (let i = 1; i < length; i++) {
        dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
        dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
    }

    return Math.max(dp[length - 1][1], dp[length - 1][0]);
};

时间复杂度: O(n) 空间复杂度: O(2 * n);


309. Best Time to Buy and Sell Stock with Cooldown

You are given an array prices where prices[i] is the price of a given stock on the ith day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:

  • After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).

Note:  You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Example 1:

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

Example 2:

Input: prices = [1]
Output: 0

思路: 难点还是在于状态的划分

  1. 状态一:买入股票状态(今天买入股票,或者是之前就买入了股票然后没有操作)
  2. 卖出股票状态,这里就有两种卖出股票状态
    1. 状态二:两天前就卖出了股票,度过了冷冻期,一直没操作,今天保持卖出股票状态
    2. 状态三:今天卖出了股票
  3. 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天

代码:

var maxProfit = function(prices) {
    let length = prices.length;

    if (length < 2) return 0;
    else if (length < 3) return Math.max(0, prices[1] -  prices[0]);

    let dp = Array(length).fill(0).map(item => Array(4).fill(0));


    dp[0][0] -= prices[0];

    for (let i = 1; i < length; i++) {
        dp[i][0] = Math.max(dp[i - 1][0], Math.max(dp[i - 1][1], dp[i - 1][3]) - prices[i]);
        dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
        dp[i][2] = dp[i - 1][0] + prices[i];
        dp[i][3] = dp[i - 1][2];
    }
    return Math.max(dp[length - 1][1], dp[length - 1][2], dp[length - 1][3]);
};

时间复杂度: O(n) 空间复杂度: O(4n)