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

121, 123题的延伸版本,123题是只交易2次,本题是交易k次

思路:
虽然整体思路和123题一样,但是不能直接那样做,因为在k很大的时候,比如k=100亿次,这时候声明的dp数组会超出内存,所以需要在123题的基础上做一些优化

需要知道的是一次有收益的交易最少是需要2天完成的,当天买当天卖肯定没有收益。
那么就算是不停地交易,当天买第二天就卖掉,也只需要prices.length/2次

所以当k>=prices.length/2时,不需要真的计算k次,只需要用贪心法算出最大的收益
就是每当第二天价格>当天价格的时候就加到收益里面,直接返回最大收益

然后k < prices.length/2时,就按照123题的dp来做:
第 i 次交易时, j 时刻如果选择不交易,收益是保持第i次,j-1时刻的收益,也就是dp[i][j - 1]

如果选择在j时刻卖掉,因为买入可以是从0到j-1的任何一个,设m=0~j-1
那么收益就是第i-1次交易时,m时刻的收益,减去买入m时刻的价格,加上现在在j时刻卖掉的价格,即
dp[i-1][m] - prices[m] + prices[j], for m = 0~ j - 1
要在m=0~j-1中取最大收益
max(dp[i-1][m] - prices[m] ) + prices[j], for m = 0~ j - 1

不需要在每个j时都遍历0~j-1,只需要保持实时更新最大差值即可

要在交易和不交易两种情况中取较大的值
dp[i][j] = max(dp[i][j - 1], max_diff + prices[j])

    int maxProfit(int k, vector<int>& prices) {
        if (k == 0 || prices.size() == 0) {
            return 0;
        }
        
        int max_profit = 0;
        
        if (k >= prices.size()/2) {
            for (int i = 1; i < prices.size(); i++) {
                if (prices[i] > prices[i - 1]) {
                    max_profit += (prices[i] - prices[i - 1]);
                }
            }
            return max_profit;
        }
        
        vector<vector<int>> dp(k + 1, vector<int>(prices.size(), 0)); 
        
        for (int i = 1; i <= k; i++) {
            int max_diff = -prices[0];
            for (int j = 1; j < prices.size(); j++) {
                dp[i][j] = max(dp[i][j - 1], max_diff + prices[j]);
                max_diff = max(max_diff, dp[i - 1][j] - prices[j]);
            }
        }
        
        return dp[k][prices.size() - 1];
    }

java版本

    public int maxProfit(int k, int[] prices) {
        if(k == 0 || prices.length == 0) {
            return 0;
        }
        
        int n = prices.length;
        //int[][] dp = new int[k+1][n]; //写在这里会MLE
        int result = 0;
        
        //k >= n/2时为greedy
        if(k >= n/2) {
            for(int i = 1; i < n; i++) {
                int val = prices[i] - prices[i - 1];
                if(val > 0) {
                    result += val;
                }
            }
            return result;
        }
        
        int[][] dp = new int[k+1][n];
        //ith transaction:max profit in 0~m, buy at m, sell at j,m:0~j-1
        //for m: dp[i][j] = max(dp[i-1][m] - prices[m]) + prices[j]
        //dp[i][j]:第i次transaction,在j时间点处的profit
        //不交易时保持在第i次transaction,j-1时间点处的profit(第i行必须是第i次transaction)
        for(int i = 1; i <= k; i++) {
            int maxDiff = -prices[0];
            for(int j = 1; j < n; j++) {
                dp[i][j] = Math.max(dp[i][j-1], maxDiff + prices[j]);
                maxDiff = Math.max(maxDiff, dp[i-1][j] - prices[j]);
            }
        }
        
        return dp[k][n-1];
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值