Skip to content

Instantly share code, notes, and snippets.

@thmain
Created May 27, 2018 06:50
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/7917229b4394f110189e3dc427089aae to your computer and use it in GitHub Desktop.
Save thmain/7917229b4394f110189e3dc427089aae to your computer and use it in GitHub Desktop.
public class RodCuttingRecursion {
public static int profit(int[] value, int length) {
if (length <= 0)
return 0;
// either we will cut it or don't cut it
int max = -1;
for(int i=0;i<length;i++)
max = Math.max(max, value[i] + profit(value, length-(i+1)));
return max;
}
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 + ":"
+ profit(value, len));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment