Add tests, add IllegalArgumentException, remove main in `WineProb… (#5662)

This commit is contained in:
Hardik Pawar
2024-10-10 23:49:37 +05:30
committed by GitHub
parent c18dbc43b5
commit ac2c88ca9f
3 changed files with 120 additions and 23 deletions

View File

@ -1,21 +1,40 @@
package com.thealgorithms.dynamicprogramming;
/**
* Imagine you have a collection of N wines placed next to each other on the
* shelf. The price of ith wine is pi(Prices of different wines are different).
* Because wine gets better every year supposing today is year 1, on year y the
* price would be y*pi i.e y times the value of the initial year. You want to
* sell all wines but you have to sell one wine per year. One more constraint on
* each year you are allowed to sell either leftmost or rightmost wine on the
* shelf. You are not allowed to reorder. You have to find the maximum profit
* The WineProblem class provides a solution to the wine selling problem.
* Given a collection of N wines with different prices, the objective is to maximize profit by selling
* one wine each year, considering the constraint that only the leftmost or rightmost wine can be sold
* at any given time.
*
* The price of the ith wine is pi, and the selling price increases by a factor of the year in which
* it is sold. This class implements three approaches to solve the problem:
*
* 1. **Recursion**: A straightforward recursive method that computes the maximum profit.
* - Time Complexity: O(2^N)
* - Space Complexity: O(N) due to recursive calls.
*
* 2. **Top-Down Dynamic Programming (Memoization)**: This approach caches the results of subproblems
* to avoid redundant computations.
* - Time Complexity: O(N^2)
* - Space Complexity: O(N^2) for the storage of results and O(N) for recursion stack.
*
* 3. **Bottom-Up Dynamic Programming (Tabulation)**: This method builds a table iteratively to
* compute the maximum profit for all possible subproblems.
* - Time Complexity: O(N^2)
* - Space Complexity: O(N^2) for the table.
*/
public final class WineProblem {
private WineProblem() {
}
// Method 1: Using Recursion
// Time Complexity=0(2^N) Space Complexity=Recursion extra space
/**
* Calculate maximum profit using recursion.
*
* @param arr Array of wine prices.
* @param si Start index of the wine to consider.
* @param ei End index of the wine to consider.
* @return Maximum profit obtainable by selling the wines.
*/
public static int wpRecursion(int[] arr, int si, int ei) {
int n = arr.length;
int year = (n - (ei - si + 1)) + 1;
@ -29,8 +48,15 @@ public final class WineProblem {
return Math.max(start, end);
}
// Method 2: Top-Down DP(Memoization)
// Time Complexity=0(N*N) Space Complexity=0(N*N)+Recursion extra space
/**
* Calculate maximum profit using top-down dynamic programming with memoization.
*
* @param arr Array of wine prices.
* @param si Start index of the wine to consider.
* @param ei End index of the wine to consider.
* @param strg 2D array to store results of subproblems.
* @return Maximum profit obtainable by selling the wines.
*/
public static int wptd(int[] arr, int si, int ei, int[][] strg) {
int n = arr.length;
int year = (n - (ei - si + 1)) + 1;
@ -45,15 +71,22 @@ public final class WineProblem {
int end = wptd(arr, si, ei - 1, strg) + arr[ei] * year;
int ans = Math.max(start, end);
strg[si][ei] = ans;
return ans;
}
// Method 3: Bottom-Up DP(Tabulation)
// Time Complexity=0(N*N/2)->0(N*N) Space Complexity=0(N*N)
/**
* Calculate maximum profit using bottom-up dynamic programming with tabulation.
*
* @param arr Array of wine prices.
* @throws IllegalArgumentException if the input array is null or empty.
* @return Maximum profit obtainable by selling the wines.
*/
public static int wpbu(int[] arr) {
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("Input array cannot be null or empty.");
}
int n = arr.length;
int[][] strg = new int[n][n];
@ -73,13 +106,4 @@ public final class WineProblem {
}
return strg[0][n - 1];
}
public static void main(String[] args) {
int[] arr = {2, 3, 5, 1, 4};
System.out.println("Method 1: " + wpRecursion(arr, 0, arr.length - 1));
System.out.println("Method 2: " + wptd(arr, 0, arr.length - 1, new int[arr.length][arr.length]));
System.out.println("Method 3: " + wpbu(arr));
}
}
// Memoization vs Tabulation : https://www.geeksforgeeks.org/tabulation-vs-memoization/
// Question Link : https://www.geeksforgeeks.org/maximum-profit-sale-wines/