|
| 1 | +## 188. Best Time to Buy and Sell Stock IV |
| 2 | + |
| 3 | +### Question |
| 4 | +Say you have an array for which the ith element is the price of a given stock on day i. |
| 5 | + |
| 6 | +Design an algorithm to find the maximum profit. You may complete at most k transactions. |
| 7 | + |
| 8 | +Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). |
| 9 | + |
| 10 | +``` |
| 11 | +Example 1: |
| 12 | +
|
| 13 | +Input: [2,4,1], k = 2 |
| 14 | +Output: 2 |
| 15 | +Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2. |
| 16 | +
|
| 17 | +Example 2: |
| 18 | +
|
| 19 | +Input: [3,2,6,5,0,3], k = 2 |
| 20 | +Output: 7 |
| 21 | +Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. |
| 22 | + Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. |
| 23 | +``` |
| 24 | + |
| 25 | +### Solution |
| 26 | +* Method 1: dp |
| 27 | + 1. We need to pay attention to the initialization. |
| 28 | + 2. Transaction function is same as [123. Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/). |
| 29 | + 3. If k is larger than prices length, we treat it as question [122. Best Time to Buy and Sell Stock II](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/122.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II.md). |
| 30 | + ```Java |
| 31 | + class Solution { |
| 32 | + public int maxProfit(int k, int[] prices) { |
| 33 | + if(k == 0 || prices == null || prices.length <= 1) return 0; |
| 34 | + int len = prices.length; |
| 35 | + if(k > len) return maxProfit(prices); |
| 36 | + int[][] buys = new int[len + 1][k + 1]; |
| 37 | + int[][] sells = new int[len + 1][k + 1]; |
| 38 | + for(int i = 2; i <= k; i++){ |
| 39 | + for(int j = 0; j < i; j++){ |
| 40 | + buys[j][i] = Integer.MIN_VALUE; |
| 41 | + } |
| 42 | + } |
| 43 | + int sum = 0; |
| 44 | + for(int i = 1; i <= k; i++){ |
| 45 | + sum -= prices[i - 1]; |
| 46 | + buys[i][i] = sum; |
| 47 | + } |
| 48 | + for(int i = 2; i <= len; i++){ |
| 49 | + for(int j = 1; j <= k; j++){ |
| 50 | + buys[i][j] = Math.max(buys[i - 1][j], sells[i - 1][j - 1] - prices[i - 1]); |
| 51 | + sells[i][j] = Math.max(sells[i - 1][j], buys[i - 1][j] + prices[i - 1]); |
| 52 | + } |
| 53 | + } |
| 54 | + int profit = 0; |
| 55 | + for(int i = 0; i <= k; i++){ |
| 56 | + profit = Math.max(profit, sells[len][i]); |
| 57 | + } |
| 58 | + return profit; |
| 59 | + } |
| 60 | + |
| 61 | + private int maxProfit(int[] prices) { |
| 62 | + int len = prices.length; |
| 63 | + int[] buys = new int[len + 1]; |
| 64 | + int[] sells = new int[len + 1]; |
| 65 | + buys[1] = -prices[0]; |
| 66 | + for(int i = 2; i <= len; i++){ |
| 67 | + buys[i] = Math.max(buys[i - 1], sells[i - 1] - prices[i - 1]); |
| 68 | + sells[i] = Math.max(sells[i - 1], buys[i - 1] + prices[i - 1]); |
| 69 | + } |
| 70 | + return sells[len]; |
| 71 | + } |
| 72 | + } |
| 73 | + ``` |
0 commit comments