From e4a942d53d599f79e56682fa5c068c295d7aaf3e Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Mon, 9 Oct 2023 16:41:49 +0530 Subject: [PATCH 1/2] Added Javadoc comments: I added Javadoc-style comments to provide documentation for the class and the cutRod method. These comments explain the purpose of the class and method and provide details about their parameters and return values, improving code readability and understandability. Clarified parameter names: I changed the parameter name from arr to price to make it clear that this array represents the prices of different pieces of the rod. This change improves the clarity of the code. Improved variable names: I renamed max_val to maxVal to follow Java naming conventions, where variable names use camelCase. This makes the code more consistent and readable. Added comments within the method: I added comments within the cutRod method to explain its logic. These comments clarify the purpose of the loops and the array val, making it easier for others to understand the code. --- .../dynamicprogramming/RodCutting.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 28ff41d1a2d1..9ff2dafdbd16 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -1,29 +1,45 @@ package com.thealgorithms.dynamicprogramming; /** - * A DynamicProgramming solution for Rod cutting problem Returns the best - * obtainable price for a rod of length n and price[] as prices of different - * pieces + * A Dynamic Programming solution for the Rod cutting problem. + * Returns the best obtainable price for a rod of length n and price[] as prices of different pieces. */ public class RodCutting { - private static int cutRod(int[] price, int n) { + /** + * This method calculates the maximum obtainable value for cutting a rod of length n + * into different pieces, given the prices for each possible piece length. + * + * @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. + * @return The maximum obtainable value. + */ + public static int cutRod(int[] price, int n) { + // Create an array to store the maximum obtainable values for each rod length. int[] val = new int[n + 1]; val[0] = 0; + // Calculate the maximum value for each rod length from 1 to n. for (int i = 1; i <= n; i++) { - int max_val = Integer.MIN_VALUE; - for (int j = 0; j < i; j++) { - max_val = Math.max(max_val, price[j] + val[i - j - 1]); + int maxVal = Integer.MIN_VALUE; + // Try all possible ways to cut the rod and find the maximum value. + for (int j = 1; j <= i; j++) { + maxVal = Math.max(maxVal, price[j - 1] + val[i - j]); } - - val[i] = max_val; + // Store the maximum value for the current rod length. + val[i] = maxVal; } + // The final element of 'val' contains the maximum obtainable value for a rod of length 'n'. return val[n]; } - // main function to test + /** + * The main method to test the RodCutting class. + * + * @param args Command line arguments (not used). + */ public static void main(String[] args) { int[] arr = new int[] {2, 5, 13, 19, 20}; int result = cutRod(arr, arr.length); From c7535bc793dd628f4f570b5cc8756a286723942e Mon Sep 17 00:00:00 2001 From: "D.Sunil" Date: Wed, 11 Oct 2023 17:12:00 +0000 Subject: [PATCH 2/2] Removed main method --- .../thealgorithms/dynamicprogramming/RodCutting.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java index 9ff2dafdbd16..4583aec2e1b4 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/RodCutting.java @@ -34,15 +34,4 @@ public static int cutRod(int[] price, int n) { // The final element of 'val' contains the maximum obtainable value for a rod of length 'n'. return val[n]; } - - /** - * The main method to test the RodCutting class. - * - * @param args Command line arguments (not used). - */ - public static void main(String[] args) { - int[] arr = new int[] {2, 5, 13, 19, 20}; - int result = cutRod(arr, arr.length); - System.out.println("Maximum Obtainable Value is " + result); - } }