Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MaxProduction {
- // HW2 problem 2
- public static void main(String[] args) {
- MaxProduction mp = new MaxProduction();
- int result = mp.maxProduction("21322", 2);
- System.out.println(result);
- }
- public int maxProduction(String input, int k) {
- if (k >= input.length()) {
- return -1;
- }
- int[][] maxP = new int[input.length() + 1][k + 1];
- maxP[0][0] = -1;
- for (int i = 1; i < maxP.length; i++) {
- maxP[i][0] = Integer.valueOf(input.substring(0, i));
- }
- printMatrix(maxP);
- for (int i = 1; i < maxP.length; i++) {
- for (int b = 1; b < i && b < maxP[0].length; b++) {
- for (int j = 1; j < i; j++) {
- System.out.println(j + 1);
- System.out.println(i);
- System.out.println(input.substring(j + 1, i));
- int temp = maxP[j][b - 1] * Integer.valueOf(input.substring(j, i));
- maxP[i][b] = Math.max(maxP[i][b], temp);
- }
- }
- }
- printMatrix(maxP);
- return maxP[maxP.length - 1][maxP[0].length - 1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement