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];
    }
};
posted @ 2017-05-15 09:45  蓝色地中海  阅读(152)  评论(0编辑  收藏  举报