-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
41 lines (33 loc) · 1.06 KB
/
Triangle.java
File metadata and controls
41 lines (33 loc) · 1.06 KB
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
package codingInterview;
import java.util.ArrayList;
import java.util.Arrays;
public class Triangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
triangle.add(new ArrayList<>(Arrays.asList(2)));
triangle.add(new ArrayList<>(Arrays.asList(8, 5)));
triangle.add(new ArrayList<>(Arrays.asList(3, 7, 1)));
triangle.add(new ArrayList<>(Arrays.asList(5, 9, 2, 6)));
System.out.println(triangle);
int minSum = minSum(triangle);
System.out.println(minSum);
}
// bottom up
private static int minSum(ArrayList<ArrayList<Integer>> triangle) {
// TODO Auto-generated method stub
int size = triangle.size();
int[] total = new int[size];
int l = size - 1;
for (int i = 0; i < triangle.get(l).size(); i++) {
total[i] = triangle.get(l).get(i);
}
for (int i = l - 1; i >= 0; i--) {
for (int j = 0; j < triangle.get(i).size(); j++) {
total[j] = triangle.get(i).get(j)
+ Math.min(total[j], total[j + 1]);
}
}
return total[0];
}
}