Created
May 27, 2018 06:49
-
-
Save thmain/c9a7172254312a976d8eb14b5c89a833 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RodCutting { | |
public static int profitDP(int[] value, int length) { | |
int[] solution = new int[length + 1]; | |
solution[0] = 0; | |
for (int i = 1; i <= length; i++) { | |
int max = -1; | |
for (int j = 0; j < i; j++) { | |
max = Math.max(max, value[j] + solution[i - (j + 1)]); | |
solution[i] = max; | |
} | |
} | |
return solution[length]; | |
} | |
public static void main(String[] args) { | |
int[] value = { 2, 3, 7, 8, 9 }; | |
int len = 5; | |
System.out.println("Max profit for length is " + len + ":" | |
+ profitDP(value, len)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment