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, you must sell the stock before you buy again).

思路:用状态存储至当前日为止第jth buy/sell的最大利润。到了第二天,我们可以按j从大到小(因为j大的新状态依赖于之前j小的状态),修改这个状态。

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        int dates = prices.size();
        if(dates <= 1 || k == 0) return 0;
        if (k >= prices.size()) return maxProfit2(prices); //unlimited transaction

        vector<int> release(k,0); //sell stock
        vector<int> hold(k,INT_MIN); //buy stock

        for(int i = 0; i < dates; i++){
            for(int j = k-1; j > 0; j--){
                release[j] = max(release[j], hold[j]+prices[i]); //jth sell happen at ith day
                hold[j]=max(hold[j], release[j-1]-prices[i]); //jth buy happen at ith day
            }
            release[0] = max(release[0], hold[0]+prices[i]);
            hold[0] = max(hold[0],-prices[i]);
        }
        return release[k-1];
    }

    int maxProfit2(vector<int> &prices) {
        int profit = 0;
        for (int i=0; i<(int)prices.size()-1; i++) {
            if (prices[i+1] > prices[i])
                profit += prices[i+1] - prices[i];
        }
        return profit;
    }
};
时间: 2024-10-18 03:39:19

188. Best Time to Buy and Sell Stock IV (Array; DP)的相关文章

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

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

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

题目链接: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解题报告:188 Best Time to Buy and Sell Stock IV

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

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天口袋里可以赚到的最多的钱.那么状态

123. Best Time to Buy and Sell Stock III (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 two transactions. Note:You may not engage in multiple transactions at the same time (ie,

LeetCode 122, 123, 188. Best Time to Buy and Sell Stock II+III+IV

Best Time to Buy and Sell Stock II class Solution { public: int maxProfit(vector<int>& prices) { int res=0; for (int i=1;i<prices.size();++i){ if (prices[i]>prices[i-1]){ res += prices[i]-prices[i-1]; } } return res; } }; Best Time to Buy