Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class CurRodIterative {
- public static void main(String[] args) {
- int n = 5;
- int[] tab = new int[n + 1];
- int[] priceArr = new int[]{1, 5, 8, 9, 10, 17, 17, 20};
- Arrays.fill(tab, -1);
- tab[0] = 0;
- int maxPrice;
- for (int i = 0; i <= n; i++) {
- if (i == 0) {
- continue;
- }
- maxPrice = -1;
- for (int j = 1; j <= i; j++) {
- // example priceArr[1]+mem[0].
- int currentPrice = priceArr[j - 1] + tab[i - j];
- maxPrice = Math.max(currentPrice, maxPrice);
- }
- System.out.println("max price of rod of size " + i + " is " + maxPrice);
- tab[i] = maxPrice;
- }
- System.out.format("Maximum price by selling pieces of %d size rod is %d", n, tab[n]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement