Add tests, add IllegalArgumentException in RodCutting (#5661)

This commit is contained in:
Hardik Pawar
2024-10-10 23:45:35 +05:30
committed by GitHub
parent 41f7e6aa9d
commit c18dbc43b5
3 changed files with 101 additions and 0 deletions

View File

@ -15,9 +15,13 @@ public final class RodCutting {
* @param price An array representing the prices of different pieces, where price[i-1]
* represents the price of a piece of length i.
* @param n The length of the rod to be cut.
* @throws IllegalArgumentException if the price array is null or empty, or if n is less than 0.
* @return The maximum obtainable value.
*/
public static int cutRod(int[] price, int n) {
if (price == null || price.length == 0) {
throw new IllegalArgumentException("Price array cannot be null or empty.");
}
// Create an array to store the maximum obtainable values for each rod length.
int[] val = new int[n + 1];
val[0] = 0;