Skip to content

Commit ac2c88c

Browse files
authored
Add tests, add IllegalArgumentException, remove main in `WineProb… (#5662)
1 parent c18dbc4 commit ac2c88c

File tree

3 files changed

+120
-23
lines changed

3 files changed

+120
-23
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@
833833
* [UniquePathsTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/UniquePathsTests.java)
834834
* [UniqueSubsequencesCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/UniqueSubsequencesCountTest.java)
835835
* [WildcardMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WildcardMatchingTest.java)
836+
* [WineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WineProblemTest.java)
836837
* geometry
837838
* [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
838839
* greedyalgorithms

src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
package com.thealgorithms.dynamicprogramming;
22

33
/**
4-
* Imagine you have a collection of N wines placed next to each other on the
5-
* shelf. The price of ith wine is pi(Prices of different wines are different).
6-
* Because wine gets better every year supposing today is year 1, on year y the
7-
* price would be y*pi i.e y times the value of the initial year. You want to
8-
* sell all wines but you have to sell one wine per year. One more constraint on
9-
* each year you are allowed to sell either leftmost or rightmost wine on the
10-
* shelf. You are not allowed to reorder. You have to find the maximum profit
4+
* The WineProblem class provides a solution to the wine selling problem.
5+
* Given a collection of N wines with different prices, the objective is to maximize profit by selling
6+
* one wine each year, considering the constraint that only the leftmost or rightmost wine can be sold
7+
* at any given time.
118
*
9+
* The price of the ith wine is pi, and the selling price increases by a factor of the year in which
10+
* it is sold. This class implements three approaches to solve the problem:
11+
*
12+
* 1. **Recursion**: A straightforward recursive method that computes the maximum profit.
13+
* - Time Complexity: O(2^N)
14+
* - Space Complexity: O(N) due to recursive calls.
15+
*
16+
* 2. **Top-Down Dynamic Programming (Memoization)**: This approach caches the results of subproblems
17+
* to avoid redundant computations.
18+
* - Time Complexity: O(N^2)
19+
* - Space Complexity: O(N^2) for the storage of results and O(N) for recursion stack.
20+
*
21+
* 3. **Bottom-Up Dynamic Programming (Tabulation)**: This method builds a table iteratively to
22+
* compute the maximum profit for all possible subproblems.
23+
* - Time Complexity: O(N^2)
24+
* - Space Complexity: O(N^2) for the table.
1225
*/
1326
public final class WineProblem {
1427
private WineProblem() {
1528
}
1629

17-
// Method 1: Using Recursion
18-
// Time Complexity=0(2^N) Space Complexity=Recursion extra space
30+
/**
31+
* Calculate maximum profit using recursion.
32+
*
33+
* @param arr Array of wine prices.
34+
* @param si Start index of the wine to consider.
35+
* @param ei End index of the wine to consider.
36+
* @return Maximum profit obtainable by selling the wines.
37+
*/
1938
public static int wpRecursion(int[] arr, int si, int ei) {
2039
int n = arr.length;
2140
int year = (n - (ei - si + 1)) + 1;
@@ -29,8 +48,15 @@ public static int wpRecursion(int[] arr, int si, int ei) {
2948
return Math.max(start, end);
3049
}
3150

32-
// Method 2: Top-Down DP(Memoization)
33-
// Time Complexity=0(N*N) Space Complexity=0(N*N)+Recursion extra space
51+
/**
52+
* Calculate maximum profit using top-down dynamic programming with memoization.
53+
*
54+
* @param arr Array of wine prices.
55+
* @param si Start index of the wine to consider.
56+
* @param ei End index of the wine to consider.
57+
* @param strg 2D array to store results of subproblems.
58+
* @return Maximum profit obtainable by selling the wines.
59+
*/
3460
public static int wptd(int[] arr, int si, int ei, int[][] strg) {
3561
int n = arr.length;
3662
int year = (n - (ei - si + 1)) + 1;
@@ -45,15 +71,22 @@ public static int wptd(int[] arr, int si, int ei, int[][] strg) {
4571
int end = wptd(arr, si, ei - 1, strg) + arr[ei] * year;
4672

4773
int ans = Math.max(start, end);
48-
4974
strg[si][ei] = ans;
5075

5176
return ans;
5277
}
5378

54-
// Method 3: Bottom-Up DP(Tabulation)
55-
// Time Complexity=0(N*N/2)->0(N*N) Space Complexity=0(N*N)
79+
/**
80+
* Calculate maximum profit using bottom-up dynamic programming with tabulation.
81+
*
82+
* @param arr Array of wine prices.
83+
* @throws IllegalArgumentException if the input array is null or empty.
84+
* @return Maximum profit obtainable by selling the wines.
85+
*/
5686
public static int wpbu(int[] arr) {
87+
if (arr == null || arr.length == 0) {
88+
throw new IllegalArgumentException("Input array cannot be null or empty.");
89+
}
5790
int n = arr.length;
5891
int[][] strg = new int[n][n];
5992

@@ -73,13 +106,4 @@ public static int wpbu(int[] arr) {
73106
}
74107
return strg[0][n - 1];
75108
}
76-
77-
public static void main(String[] args) {
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));
82-
}
83109
}
84-
// Memoization vs Tabulation : https://www.geeksforgeeks.org/tabulation-vs-memoization/
85-
// Question Link : https://www.geeksforgeeks.org/maximum-profit-sale-wines/
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the WineProblem class.
10+
* This test class verifies the correctness of the wine selling problem solutions.
11+
*/
12+
class WineProblemTest {
13+
14+
/**
15+
* Test for wpRecursion method.
16+
*/
17+
@Test
18+
void testWpRecursion() {
19+
int[] wines = {2, 3, 5, 1, 4}; // Prices of wines
20+
int expectedProfit = 50; // The expected maximum profit
21+
assertEquals(expectedProfit, WineProblem.wpRecursion(wines, 0, wines.length - 1), "The maximum profit using recursion should be 50.");
22+
}
23+
24+
/**
25+
* Test for wptd method (Top-Down DP with Memoization).
26+
*/
27+
@Test
28+
void testWptd() {
29+
int[] wines = {2, 3, 5, 1, 4}; // Prices of wines
30+
int expectedProfit = 50; // The expected maximum profit
31+
assertEquals(expectedProfit, WineProblem.wptd(wines, 0, wines.length - 1, new int[wines.length][wines.length]), "The maximum profit using top-down DP should be 50.");
32+
}
33+
34+
/**
35+
* Test for wpbu method (Bottom-Up DP with Tabulation).
36+
*/
37+
@Test
38+
void testWpbu() {
39+
int[] wines = {2, 3, 5, 1, 4}; // Prices of wines
40+
int expectedProfit = 50; // The expected maximum profit
41+
assertEquals(expectedProfit, WineProblem.wpbu(wines), "The maximum profit using bottom-up DP should be 50.");
42+
}
43+
44+
/**
45+
* Test with a single wine.
46+
*/
47+
@Test
48+
void testSingleWine() {
49+
int[] wines = {10}; // Only one wine
50+
int expectedProfit = 10; // Selling the only wine at year 1
51+
assertEquals(expectedProfit, WineProblem.wpbu(wines), "The maximum profit for a single wine should be 10.");
52+
}
53+
54+
/**
55+
* Test with multiple wines of the same price.
56+
*/
57+
@Test
58+
void testSamePriceWines() {
59+
int[] wines = {5, 5, 5}; // All wines have the same price
60+
int expectedProfit = 30; // Profit is 5 * (1 + 2 + 3)
61+
assertEquals(expectedProfit, WineProblem.wpbu(wines), "The maximum profit with same price wines should be 30.");
62+
}
63+
64+
/**
65+
* Test with no wines.
66+
*/
67+
@Test
68+
void testNoWines() {
69+
int[] wines = {};
70+
assertThrows(IllegalArgumentException.class, () -> WineProblem.wpbu(wines), "The maximum profit for no wines should throw an IllegalArgumentException.");
71+
}
72+
}

0 commit comments

Comments
 (0)