【力扣】股票问题通用模板

股票问题通用模板

考虑交易次数、冷冻期和手续费。

	// 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {
        int n = prices.length;
        if(n <= 0){
            return 0;
        }
        if (max_k > n / 2) {
            return maxProfit_k_inf(prices, cooldown, fee);
        }
        int[][][] dp = new int[n][max_k + 1][2];

        for (int i = 0; i < n; i++) {
            for (int k = 0; k <= max_k; k++) {
                if (i == 0) {
                    dp[i][k][0] = 0;
                    dp[i][k][1] = -prices[i] - fee;
                    continue;
                }
                if (k == 0) {
                    dp[i][k][0] = 0;
                    dp[i][k][1] = Integer.MIN_VALUE;
                    continue;
                }
                if (i - 1 - cooldown < 0) {
                    dp[i][k][0] = Math.max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]);
                    dp[i][k][1] = Math.max(dp[i - 1][k][1], -prices[i] - fee);
                }
                dp[i][k][0] = Math.max(dp[i - 1][k][0], dp[i - 1][k][1] + prices[i]);
                dp[i][k][1] = Math.max(dp[i - 1][k][1], dp[i - 1 - cooldown][k - 1][0] - prices[i] - fee);
            }
        }

        return dp[n - 1][max_k][0];
    }

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {
        int n = prices.length;
        int[][] dp = new int[n][2];

        for (int i = 0; i < n; i++) {
            if (i == 0) {
                dp[i][0] = 0;
                dp[i][1] = -prices[i] - fee;
                continue;
            }
            if (i - 1 - cooldown < 0) {
                dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
                dp[i][1] = Math.max(dp[i - 1][1], -prices[i] - fee);
                continue;
            }
            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 - cooldown][0] - prices[i] - fee);
        }

        return dp[n - 1][0];
    }

121. 买卖股票的最佳时机

class Solution {
    public int maxProfit(int[] prices) {
        return maxProfit_all_in_one(prices, 1, 0, 0);
    }

    // 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {...}

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {...}
}

122. 买卖股票的最佳时机 II

class Solution {
    public int maxProfit(int[] prices) {
        return maxProfit_all_in_one(prices, Integer.MAX_VALUE, 0, 0);
    }

    // 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {...}

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {...}
}

123. 买卖股票的最佳时机 III

class Solution {
    public int maxProfit(int[] prices) {
        return maxProfit_all_in_one(prices, 2, 0, 0);
    }

    // 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {...}

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {...}
}

188. 买卖股票的最佳时机 IV

class Solution {
    public int maxProfit(int k, int[] prices) {
        return maxProfit_all_in_one(prices, k, 0, 0);
    }

    // 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {...}

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {...}
}

309. 最佳买卖股票时机含冷冻期

class Solution {
    public int maxProfit(int[] prices) {
        return maxProfit_all_in_one(prices, Integer.MAX_VALUE, 1, 0);
    }

    // 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {...}

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {...}
}

714. 买卖股票的最佳时机含手续费

class Solution {
    public int maxProfit(int[] prices, int fee) {
        return maxProfit_all_in_one(prices, Integer.MAX_VALUE, 0, fee);
    }

    // 股票问题通用模板
    private int maxProfit_all_in_one(int[] prices, int max_k, int cooldown, int fee) {...}

    private int maxProfit_k_inf(int[] prices, int cooldown, int fee) {...}
}

参考资料

一个方法团灭 LEETCODE 股票买卖问题

posted @ 2022-08-18 10:08  dotJunz  阅读(211)  评论(0编辑  收藏  举报