-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob2.java
35 lines (28 loc) · 875 Bytes
/
prob2.java
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
import java.util.Scanner;
class program{
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int prices[] = new int[n];
for (int i=0 ;i < n ;i++ ){
prices[i] = Integer.parseInt(sc.nextLine());
}
int k = Integer.parseInt(sc.nextLine());
System.out.println(maxProfitWithKTransactions(prices,k));
}
public static int maxProfitWithKTransactions(int[] prices, int k){
if(prices.length == 0){
return 0;
}
int [][] profits = new int[k + 1][prices.length];
for (int t = 1;t<k+1 ;t++ ){
int maxThusFar = Integer.MIN_VALUE;
for (int d =1;d< prices.length ;d++ ){
maxThusFar = Math.max(maxThusFar,profits[t - 1][d - 1] - prices[d - 1]);
profits[t][d] = Math.max(profits[t][d-1],maxThusFar + prices[d]);
}
}
return profits[k][prices.length-1];
}
}
//program();