188. Best Time to Buy and Sell Stock IV

Problem statement:

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

Solution one: DP(TLE)

This problem is the generalized model for buy/sell stock i, ii, iii. Given array including some days, we can do at most k transactions.

There is a video from YouTube is highly rated: Buy/Sell Stock With K transactions To Maximize Profit Dynamic Programming. Tushar delivered a very excellent explanation. 

I also used his idea and try to summary by my own words.

DP formula:

dp[i][j] means at day j, we can do at most i transactions.

The key word is "at most", it is similar with 0-1 knapsack. That means it is not necessary to do i transactions(0, 1, 2 ... i - 1 are all possible solution).

In day j, we can choose do transaction or not.

dp[i][j] comes from two sources. 

If we do a transaction at day j and it is transaction i.

dp[i][j] = max(prices[j] - prices[m] + dp[i - 1][m]) (m ranges in [0 ... j - 1]) ---> we should find the max profit. We bought the stock at day m and sell it at day j, the profit for this transaction is prices[j] - prices[m], also should plus the profit of first i - 1 transactions(dp[i - 1][m]).

If we do not do any transaction at day j. 

dp[i][j] = dp[i][j - 1]

The final answer of dp[i][j] is the max value between do transactionr or not.

Initialization:

dp[0][j] = 0: no any transaction, all profit is 0

dp[i][0] = 0: if there is only 1 day, we can not get a profit from any transactions.

Return dp[k][day_cnt - 1]

For this solution, the time complexity is O(n * n * k). n is the number of days and k is the number of transactions.

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        if(prices.empty()){
            return 0;
        }
        int day_cnt = prices.size();
        // dp[i][j] : at most i transactions at day j
        vector<vector<int>> dp(k + 1, vector<int>(day_cnt, 0));
        for(int i = 1; i <= k; i++){
            for(int j = 1; j < day_cnt; j++){
                for(int m = 0; m < j; m++){
                    // make a transaction at day j
                    dp[i][j] = max(dp[i][j], prices[j] - prices[m] + dp[i - 1][m]);
                }
                // update dp[i][j] between make a transaction or not
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
        }
        return dp[k][day_cnt - 1];
    }
};

Solution two:

Solution one can not pass OJ as the high time complexity.

We have to optimize it, I check the leetcod discussion. One key idea is that if the transactions is grater than or equal to the half of the array size. It becomes 122. Best Time to Buy and Sell Stock II. You can do as many transactions as you like. Finally, it passed OJ.

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        if(prices.empty()){
            return 0;
        }
        int day_cnt = prices.size();
        int max_profit = 0;
        if(k >= day_cnt / 2){
            for(int i = 1; i < day_cnt; i++){
                max_profit += (prices[i] > prices[i - 1] ? prices[i] - prices[i - 1] : 0);
            }
            return max_profit;
        }
        // dp[i][j] : at most i transactions at day j
        vector<vector<int>> dp(k + 1, vector<int>(day_cnt, 0));
        for(int i = 1; i <= k; i++){
            for(int j = 1; j < day_cnt; j++){
                for(int m = 0; m < j; m++){
                    // make a transaction at day j
                    dp[i][j] = max(dp[i][j], prices[j] - prices[m] + dp[i - 1][m]);
                }
                // update dp[i][j] between make a transaction and do not make a transaction
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
        }
        return dp[k][day_cnt - 1];
    }
};

转载于:https://www.cnblogs.com/wdw828/p/6855023.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值