Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 27, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/c9a7172254312a976d8eb14b5c89a833 to your computer and use it in GitHub Desktop.
Save thmain/c9a7172254312a976d8eb14b5c89a833 to your computer and use it in GitHub Desktop.
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