Skip to content

Commit 2ead70d

Browse files
dhananjay-ngDhananjay Nagargoje
authored and
Dhananjay Nagargoje
committed
2d dp
1 parent 37b26e1 commit 2ead70d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package problems.onRecursionAndDp.dp;
2+
3+
import java.util.Arrays;
4+
5+
public class WineProblem {
6+
int[] wines = {2, 4, 6, 2, 5};
7+
int N = wines.length;
8+
int dp[][] = new int [N+1][N+1];
9+
public int profit(int be, int en){
10+
11+
if (be > en) return 0;
12+
if (this.dp[be][en]!=-1) return this.dp[be][en];
13+
14+
int year = N - (en - be + 1) + 1;
15+
16+
this.dp[be][en] = Math.max( profit(be+1, en) + wines[be-1]*year,
17+
profit(be, en - 1) + wines[en-1]*year);
18+
19+
20+
return this.dp[be][en];
21+
}
22+
23+
public static void main(String[] args) {
24+
WineProblem wineProblem = new WineProblem();
25+
for (int i = 0; i < wineProblem.N; i++){
26+
wineProblem.dp[i] = new int[wineProblem.N+1];
27+
Arrays.fill(wineProblem.dp[i], -1);
28+
}
29+
30+
System.out.println(wineProblem.profit(1, 5));
31+
32+
}
33+
34+
}

0 commit comments

Comments
 (0)