@@ -16,22 +16,22 @@ private WineProblem() {
16
16
17
17
// Method 1: Using Recursion
18
18
// Time Complexity=0(2^N) Space Complexity=Recursion extra space
19
- public static int WPRecursion (int [] arr , int si , int ei ) {
19
+ public static int wpRecursion (int [] arr , int si , int ei ) {
20
20
int n = arr .length ;
21
21
int year = (n - (ei - si + 1 )) + 1 ;
22
22
if (si == ei ) {
23
23
return arr [si ] * year ;
24
24
}
25
25
26
- int start = WPRecursion (arr , si + 1 , ei ) + arr [si ] * year ;
27
- int end = WPRecursion (arr , si , ei - 1 ) + arr [ei ] * year ;
26
+ int start = wpRecursion (arr , si + 1 , ei ) + arr [si ] * year ;
27
+ int end = wpRecursion (arr , si , ei - 1 ) + arr [ei ] * year ;
28
28
29
29
return Math .max (start , end );
30
30
}
31
31
32
32
// Method 2: Top-Down DP(Memoization)
33
33
// Time Complexity=0(N*N) Space Complexity=0(N*N)+Recursion extra space
34
- public static int WPTD (int [] arr , int si , int ei , int [][] strg ) {
34
+ public static int wptd (int [] arr , int si , int ei , int [][] strg ) {
35
35
int n = arr .length ;
36
36
int year = (n - (ei - si + 1 )) + 1 ;
37
37
if (si == ei ) {
@@ -41,8 +41,8 @@ public static int WPTD(int[] arr, int si, int ei, int[][] strg) {
41
41
if (strg [si ][ei ] != 0 ) {
42
42
return strg [si ][ei ];
43
43
}
44
- int start = WPTD (arr , si + 1 , ei , strg ) + arr [si ] * year ;
45
- int end = WPTD (arr , si , ei - 1 , strg ) + arr [ei ] * year ;
44
+ int start = wptd (arr , si + 1 , ei , strg ) + arr [si ] * year ;
45
+ int end = wptd (arr , si , ei - 1 , strg ) + arr [ei ] * year ;
46
46
47
47
int ans = Math .max (start , end );
48
48
@@ -53,7 +53,7 @@ public static int WPTD(int[] arr, int si, int ei, int[][] strg) {
53
53
54
54
// Method 3: Bottom-Up DP(Tabulation)
55
55
// Time Complexity=0(N*N/2)->0(N*N) Space Complexity=0(N*N)
56
- public static int WPBU (int [] arr ) {
56
+ public static int wpbu (int [] arr ) {
57
57
int n = arr .length ;
58
58
int [][] strg = new int [n ][n ];
59
59
@@ -76,9 +76,9 @@ public static int WPBU(int[] arr) {
76
76
77
77
public static void main (String [] args ) {
78
78
int [] arr = {2 , 3 , 5 , 1 , 4 };
79
- System .out .println ("Method 1: " + WPRecursion (arr , 0 , arr .length - 1 ));
80
- System .out .println ("Method 2: " + WPTD (arr , 0 , arr .length - 1 , new int [arr .length ][arr .length ]));
81
- System .out .println ("Method 3: " + WPBU (arr ));
79
+ System .out .println ("Method 1: " + wpRecursion (arr , 0 , arr .length - 1 ));
80
+ System .out .println ("Method 2: " + wptd (arr , 0 , arr .length - 1 , new int [arr .length ][arr .length ]));
81
+ System .out .println ("Method 3: " + wpbu (arr ));
82
82
}
83
83
}
84
84
// Memoization vs Tabulation : https://www.geeksforgeeks.org/tabulation-vs-memoization/
0 commit comments