Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class CutRod {
- public static void main(String[] args) {
- int n = 5;
- int[] mem = new int[n + 1];
- Arrays.fill(mem, -1);
- mem[0] = 0;
- System.out.println(cut(new int[]{1, 5, 8, 9, 10, 17, 17, 20}, n, mem));
- }
- private static int cut(int[] priceArr, int n, int[] mem) {
- if (n == 0) {
- return 0;
- }
- int maxPrice = 0;
- for (int i = 0; i < n; i++) {
- if (mem[i] == -1) {
- mem[i] = cut(priceArr, i, mem);
- }
- int currentPrice = priceArr[n - i - 1] + mem[i];
- maxPrice = Math.max(currentPrice, maxPrice);
- }
- return maxPrice;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement