[LeetCode刷题笔记]188 - 买卖股票的最佳时机 IV(C++/Python3/Java/动态规划)

一、题目描述

  • 给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。
  • 设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。
  • 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例:

输入输出解释
k = 2, prices = [2,4,1]2在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4 - 2 = 2 。
k = 2, prices = [3,2,6,5,0,3]7在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6 - 2 = 4 。随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3 - 0 = 3 。

提示:

  • 0 < = k < = 100 0 <= k <= 100 0<=k<=100
  • 0 < = p r i c e s . l e n g t h < = 1000 0 <= prices.length <= 1000 0<=prices.length<=1000
  • 0 < = p r i c e s [ i ] < = 1000 0 <= prices[i] <= 1000 0<=prices[i]<=1000

二、求解思路:动态规划

C++代码

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        int n = prices.size();
        if(n == 0) {
            return 0;
        }

        int dp[n][2][k+1];
        for(int k_idx = 0; k_idx < k+1; k_idx++) {
            dp[0][0][k_idx] = 0; dp[0][1][k_idx] = -prices[0];
        }

        for(int n_idx = 1;n_idx < n; n_idx++) {
            dp[n_idx][0][0] = 0;
            dp[n_idx][1][0] = max(dp[n_idx-1][0][0] - prices[n_idx], dp[n_idx-1][1][0]); 
            for(int k_idx = 1; k_idx < k+1; k_idx++) {
                dp[n_idx][0][k_idx] = max(dp[n_idx-1][0][k_idx], dp[n_idx-1][1][k_idx-1] + prices[n_idx]); 
                dp[n_idx][1][k_idx] = max(dp[n_idx-1][0][k_idx] - prices[n_idx], dp[n_idx-1][1][k_idx]); 
            }
        }
        return dp[n-1][0][k];
    }
};

在这里插入图片描述
Python3代码

class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        n = len(prices)
        if(n == 0):
            return 0

        dp = [ [[0]*(k+1), [0]*(k+1)] for i in range(n) ]
        for k_idx in range(k+1):
            dp[0][0][k_idx] = 0
            dp[0][1][k_idx] = -prices[0]

        for n_idx in range(1,n):
            dp[n_idx][0][0] = 0
            dp[n_idx][1][0] = max(dp[n_idx-1][0][0] - prices[n_idx], dp[n_idx-1][1][0])
            for k_idx in range(1,k+1):
                dp[n_idx][0][k_idx] = max(dp[n_idx-1][0][k_idx], dp[n_idx-1][1][k_idx-1] + prices[n_idx])
                dp[n_idx][1][k_idx] = max(dp[n_idx-1][0][k_idx] - prices[n_idx], dp[n_idx-1][1][k_idx])
        return dp[n-1][0][k]

在这里插入图片描述
Java代码

class Solution {
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        if(n == 0) {
            return 0;
        }

        int[][][] dp = new int[n][2][k+1];
        for(int k_idx = 0; k_idx < k+1; k_idx++) {
            dp[0][0][k_idx] = 0; dp[0][1][k_idx] = -prices[0];
        }

        for(int n_idx = 1;n_idx < n; n_idx++) {
            dp[n_idx][0][0] = 0;
            dp[n_idx][1][0] = Math.max(dp[n_idx-1][0][0] - prices[n_idx], dp[n_idx-1][1][0]); 
            for(int k_idx = 1; k_idx < k+1; k_idx++) {
                dp[n_idx][0][k_idx] = Math.max(dp[n_idx-1][0][k_idx], dp[n_idx-1][1][k_idx-1] + prices[n_idx]); 
                dp[n_idx][1][k_idx] = Math.max(dp[n_idx-1][0][k_idx] - prices[n_idx], dp[n_idx-1][1][k_idx]); 
            }
        }
        return dp[n-1][0][k];
    }
}

在这里插入图片描述

复杂度分析

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

三、参考文章

[1] https://blog.csdn.net/qq_40430360/article/details/124760973

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

PanyCG_pc

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

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

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

打赏作者

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

抵扣说明:

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

余额充值