forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxProfit4.java
More file actions
35 lines (32 loc) · 1007 Bytes
/
MaxProfit4.java
File metadata and controls
35 lines (32 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package dynamic;
public class MaxProfit4 {
public int maxProfit(int k,int[] prices){
int n = prices.length;
if (n <= 1) return 0;
if (k >= n/2){
int maxPro = 0;
for (int i =1;i<n;i++){
if (prices[i] > prices[i-1]){
maxPro += prices[i] - prices[i-1];
}
}
return maxPro;
}
int[][] dp = new int[k+1][n];
for (int i = 1; i <= k ; i++) {
int localMax = dp[i-1][0] - prices[0];
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j-1],prices[j] + localMax);
localMax = Math.max(localMax,dp[i-1][j] - prices[j]);
}
}
return dp[k][n-1];
}
public static void main(String[] args) {
int[] nums = {2,4,1};
int k = 2;
MaxProfit4 maxProfit4 = new MaxProfit4();
int r = maxProfit4.maxProfit(k,nums);
System.out.println(r);
}
}