-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (25 loc) · 744 Bytes
/
Copy pathSolution.java
File metadata and controls
28 lines (25 loc) · 744 Bytes
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
import java.util.ArrayList;
import java.util.List;
public class Solution
{
public int minimumTotal(List<List<Integer>> triangle)
{
int n = triangle.size();
List<List<Integer>> dp = new ArrayList<>();
dp.add(triangle.get(n - 1));
int k = 0;
for (int i = n - 2; i >= 0; i--)
{
List<Integer> list = triangle.get(i);
List<Integer> localMax = new ArrayList<>();
for (int j = 0; j < list.size(); j++)
{
int min = Math.min(dp.get(k).get(j), dp.get(k).get(j + 1));
localMax.add(min + list.get(j));
}
dp.add(localMax);
k++;
}
return dp.get(n - 1).get(0);
}
}