LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)

题目

题意:给你一个数组代表每天的股价。你有k次买入和卖出的机会,问你最多能赚多少钱。买入之前必须卖出已有股份。同一天是可以先买,再卖,或者先卖再买的。

题解:题目没有说数据范围,但是经过我实际测试 k 最大为10^8 ,n最大为10^4。当然k最多只需要取n/2就好了,因为当天买当天卖是没有意义的。

那这道题的效率就应该控制在O(n*k),再加一点就要超时了,所以两个循环嵌套。第一层是k,第二层是n

状态数组dp[k][i] 表示开始第k次买卖的交易时,第i天口袋里可以赚到的最多的钱。那么状态转移的思路就是,第i天,这一天我要么什么也不做:dp[i]=d[i-1],要么我把手里的股份在今天卖出,dp[i]=m+prices[i],这里的m是之前某一天我买进了股份,剩下的钱。所以,m应该越大越好。那么m怎么算呢?m = Max { dp[k-1][j] - prices[j] } ( j = 0..i-1) dp[k-1][j] 表示k-1次交易后,在第j天赚的钱。

整个状态转移过程,在k维度上,只需要k和k-1转移,所以,状态数组用两个dp数组就可以了,无需k个。

class Solution {
public:
    int dp[10005];
    int bp[10005];
    int maxProfit(int k, vector<int>& prices) {

        if(k==0 || prices.size()<=1)
            return 0;

        for(int i=0;i<k&&i<prices.size()/2;i++)
        {
            int m = -99999999;
            for(int j=1;j<prices.size();j++)
            {
                m = max(m,bp[j-1]-prices[j-1]);

                dp[j] = max(dp[j-1],m+prices[j]);
            }

            for(int j=0;j<prices.size();j++)
            {
                bp[j]=dp[j];
                dp[j]=0;
            }
        }

        return bp[prices.size()-1];

    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12258979.html

时间: 2024-07-30 23:42:08

LeetCode 188. Best Time to Buy and Sell Stock IV (动态规划)的相关文章

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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. 解题思路: 本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下: public i

Leetcode #188 Best Time to Buy and Sell Stock IV

题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 当 k ≥ prices.size() / 2 时:题目等价于 k 无限大的情形. 当 k < prices.size() / 2 时: 用dp[m][n+1]表示在[0,n]区间,进行了m次买卖操作,获得的最大利润. 那么这个利润必为以下几个数据的最大值: dp[m-1][n+1],即在[0,n]区间,进行了m-1次买卖操作,获得的最大利润. dp[m]

【LeetCode】Best Time to Buy and Sell Stock IV

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 t

[LeetCode][Java] 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 (i

188. Best Time to Buy and Sell Stock IV (Array; DP)

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, yo

188. Best Time to Buy and Sell Stock IV leetcode解题笔记

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, yo

188. Best Time to Buy and Sell Stock IV Leetcode Python

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, y

leetcode解题报告:188 Best Time to Buy and Sell Stock IV

问题: 给定一个列表,第i个元素代表股票第i天的价值,最多只允许买入卖出k次,求最大收益 思路:动态规划 输入为列表p1p2...pm 代码:Python

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, yo