-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumTotal.java
More file actions
66 lines (57 loc) · 1.6 KB
/
MinimumTotal.java
File metadata and controls
66 lines (57 loc) · 1.6 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.ArrayList;
public class MinimumTotal {
public static int minimumTotal(ArrayList<ArrayList<Integer>> triangle){ //not correct
if(triangle.size()==0){
return 0;
}
int record = 0;
int min = 0;
int sum = triangle.get(0).get(0);
for(int i=1;i<triangle.size();i++){
min = Math.min(triangle.get(i).get(record), triangle.get(i).get(record+1));
record = min==triangle.get(i).get(record)? record:record+1;
sum+= min;
}
return sum;
}
public static int minimumTotalTwo(ArrayList<ArrayList<Integer>> triangle) {
if(triangle.size()==0){
return 0;
}
int[] pathSum = new int[triangle.size()];
int row = triangle.size()-1;
for(int i=row;i>-1;i--){
for(int j=0;j<triangle.get(i).size();j++){
if(i==row){
pathSum[j] = triangle.get(row).get(j); //populate the base
}else{
pathSum[j] = Math.min(pathSum[j],pathSum[j+1])+triangle.get(i).get(j); //compares base and update to new col
}
}
}
return pathSum[0];
}
public static void main(String args[]){
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(2);
ArrayList<Integer> b = new ArrayList<Integer>();
b.add(3);
b.add(4);
ArrayList<Integer> c = new ArrayList<Integer>();
c.add(6);
c.add(5);
c.add(7);
ArrayList<Integer> d = new ArrayList<Integer>();
d.add(4);
d.add(1);
d.add(8);
d.add(3);
triangle.add(a);
triangle.add(b);
triangle.add(c);
triangle.add(d);
System.out.println(minimumTotal(triangle));
System.out.println(minimumTotalTwo(triangle));
}
}