• 代码随想录算法训练营Day50 | 动态规划(11/17) LeetCode 123.买卖股票的最佳时机III 188.买卖股票的最佳时机IV


    继续股票买卖的练习,但是今天难度比较高,LeetCode上也标注了是Hard。

    第一题

    123. Best Time to Buy and Sell Stock III

    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 at most two transactions.

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

    虽然卖股票的次数从一次变成了两次,但比之前难了不少。一天可以选择不卖,也能选择卖一个或是两个,选择更加多样。

    在选择递推公式的时候,达到dp[i][1]状态,有两个具体操作:

    • 操作一:第i天买入股票了,那么dp[i][1] = dp[i-1][0] - prices[i]
    • 操作二:第i天没有操作,而是沿用前一天买入的状态,即:dp[i][1] = dp[i - 1][1]

    那么dp[i][1]究竟选 dp[i-1][0] - prices[i],还是dp[i - 1][1]呢?

    一定是选最大的,所以 dp[i][1] = max(dp[i-1][0] - prices[i], dp[i - 1][1]);

    同理dp[i][2]也有两个操作:

    • 操作一:第i天卖出股票了,那么dp[i][2] = dp[i - 1][1] + prices[i]
    • 操作二:第i天没有操作,沿用前一天卖出股票的状态,即:dp[i][2] = dp[i - 1][2]

    所以dp[i][2] = max(dp[i - 1][1] + prices[i], dp[i - 1][2])

    同理可推出剩下状态部分:

    dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] - prices[i]);

    dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);

    1. class Solution:
    2. def maxProfit(self, prices: List[int]) -> int:
    3. if len(prices) == 0:
    4. return 0
    5. dp = [[0] * 5 for _ in range(len(prices))]
    6. dp[0][1] = -prices[0]
    7. dp[0][3] = -prices[0]
    8. for i in range(1, len(prices)):
    9. dp[i][0] = dp[i-1][0]
    10. dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i])
    11. dp[i][2] = max(dp[i-1][2], dp[i-1][1] + prices[i])
    12. dp[i][3] = max(dp[i-1][3], dp[i-1][2] - prices[i])
    13. dp[i][4] = max(dp[i-1][4], dp[i-1][3] + prices[i])
    14. return dp[-1][4]

    第二题

     188. Best Time to Buy and Sell Stock IV

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

    Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times.

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

    和前面的题相比,限制了交易次数。在二维递推数组中,可以发现,

    除了0以外,偶数就是卖出,奇数就是买入

    题目要求是至多有K笔交易,那么j的范围就定义为 2 * k + 1 就可以了。

    1. class Solution:
    2. def maxProfit(self, k: int, prices: List[int]) -> int:
    3. if len(prices) == 0:
    4. return 0
    5. dp = [[0] * (2*k+1) for _ in range(len(prices))]
    6. for j in range(1, 2*k, 2):
    7. dp[0][j] = -prices[0]
    8. for i in range(1, len(prices)):
    9. for j in range(0, 2*k-1, 2):
    10. dp[i][j+1] = max(dp[i-1][j+1], dp[i-1][j] - prices[i])
    11. dp[i][j+2] = max(dp[i-1][j+2], dp[i-1][j+1] + prices[i])
    12. return dp[-1][2*k]
  • 相关阅读:
    项目风险管理十大黄金法则!高质量项目管理必杀技!
    【spring cloud】(七)消息驱动——springcloud Stream
    Spring依赖注入、循环依赖——三级缓存
    JavaMetaweblogClient,Metaweblog的java实现-从此上传博客实现全平台
    vue3 依赖注入 provide/inject
    VScode 安装插件后依然不能理解lombok注释的问题
    Centos安装postgresql
    PHP8中调换数组中的键值和元素值-PHP8知识详解
    C# 窗体与子线程数据交互
    云原生之K8S------Pod的基础概念
  • 原文地址:https://blog.csdn.net/Hanzq1997/article/details/132948458