Advertisement
sweet1cris

Untitled

Jan 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. public class MaxProduction {
  2.     // HW2 problem 2
  3.     public static void main(String[] args) {
  4.         MaxProduction mp = new MaxProduction();
  5.         int result = mp.maxProduction("21322", 2);
  6.         System.out.println(result);
  7.     }
  8.  
  9.     public int maxProduction(String input, int k) {
  10.         if (k >= input.length()) {
  11.             return -1;
  12.         }
  13.         int[][] maxP = new int[input.length() + 1][k + 1];
  14.         maxP[0][0] = -1;
  15.         for (int i = 1; i < maxP.length; i++) {
  16.             maxP[i][0] = Integer.valueOf(input.substring(0, i));
  17.         }
  18.         printMatrix(maxP);
  19.         for (int i = 1; i < maxP.length; i++) {
  20.             for (int b = 1; b < i && b < maxP[0].length; b++) {
  21.                 for (int j = 1; j < i; j++) {
  22.                     System.out.println(j + 1);
  23.                     System.out.println(i);
  24.                     System.out.println(input.substring(j + 1, i));
  25.                     int temp = maxP[j][b - 1] * Integer.valueOf(input.substring(j, i));
  26.                     maxP[i][b] = Math.max(maxP[i][b], temp);
  27.                 }
  28.             }
  29.         }
  30.         printMatrix(maxP);
  31.         return maxP[maxP.length - 1][maxP[0].length - 1];
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement