Created
May 27, 2018 06:50
-
-
Save thmain/7917229b4394f110189e3dc427089aae 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 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