Lintcode. 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 transactions.

You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example

Given prices = [4,4,6,1,1,4,2,5], and k = 2, return 6.

Java
Time complexity: O(K*N^2), N – number of days, k – number of transactions
Space complexity: O(N*K), N – number of days, k – number of transactions

class Solution {
    /**
     * @param k: An integer
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    public int maxProfit(int k, int[] prices) {
        // write your code here
        if (prices == null || prices.length == 0){
            return 0;
        }
        
        if (k >= prices.length) {
    		int profit = 0;
    		for (int i = 1; i < prices.length; i++) { if (prices[i] > prices[i - 1]) {
    				profit += prices[i] - prices[i - 1];
    			}
    		}
    		return profit;
    	}
        
        int[][] dp = new int[k+1][prices.length];
        
        for (int i = 1; i < dp.length; ++i){
            int profit = 0;
            for (int j = 1; j < dp[0].length; ++j){
                for (int m = 0; m < j; ++m){
                    profit = Math.max(profit, prices[j] - prices[m] + dp[i-1][m]);
                }
                dp[i][j] = Math.max(dp[i][j-1], profit);       
            } 
            
        }
        
        return dp[k][prices.length - 1];
    }
}

Java (optimized)
Time complexity: O(K*N), N – number of days, k – number of transactions
Space complexity: O(N*K), N – number of days, k – number of transactions

class Solution {
    /**
     * @param k: An integer
     * @param prices: Given an integer array
     * @return: Maximum profit
     */
    public int maxProfit(int k, int[] prices) {
        // write your code here
        if (prices == null || prices.length == 0){
            return 0;
        }
        
        if (k >= prices.length) {
    		int profit = 0;
    		for (int i = 1; i < prices.length; i++) {
    			if (prices[i] > prices[i - 1]) {
    				profit += prices[i] - prices[i - 1];
    			}
    		}
    		return profit;
    	}
        
        int[][] dp = new int[k+1][prices.length];
        
        for (int i = 1; i < dp.length; ++i){
            int profit = 0;
            int maxDiff = -prices[0];
            for (int j = 1; j < dp[0].length; ++j){
                dp[i][j] = Math.max(dp[i][j-1], prices[j] + maxDiff); 
                maxDiff = Math.max(maxDiff, dp[i-1][j] - prices[j]);
            } 
            
        }
        
        return dp[k][prices.length - 1];
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s