forked from RasPat1/practice-codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScratch.java
45 lines (40 loc) · 1.38 KB
/
Scratch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
public class Scratch {
/** You are given a list of matricies
* compute the optimal parenthesization
* Use dynamic Programming
* returns locations of parentheses... >=0 for open, <0 for close
**/
public static int parenDP(List<Integer[][]> matricies) {
// 1) Define subproblem
// solve using substrings
// i, j for all i, j
// 2)
if (matricies.size() > 2) {
// take min
int minCost = Integer.MAX_VALUE;
int minCostIndex = -1;
for (int i = 1; i < matricies.size(); i++) {
int firstMult = parenDP(matricies.subList(0, i));
int secondMult = parenDP(matricies.subList(i, matricies.size()));
int mCost = 1; // getMultCost(0,0); // what goes in here?
int val = firstMult + secondMult + mCost;
if (val < minCost) {
minCost = val;
minCostIndex = i;
}
}
return minCost;
} else if (matricies.size() == 2) {
return getMultCost(matricies.get(0), matricies.get(1));
} else {
return 0;
}
}
// n by 1 times 1 by n = n by n -> cost is nsq
// 1 by n times n by 1 = 1 by 1 -> cost is n
// n by m times m by p = n by p -> cost is
public static int getMultCost(Integer[][] m1, Integer[][] m2) {
return m1.length * m1[0].length * m2[0].length;
}
}