-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path120.Triangle.java
41 lines (36 loc) · 1.09 KB
/
120.Triangle.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
// https://leetcode.com/problems/triangle/
//
// algorithms
// Medium (39.28%)
// Total Accepted: 180,880
// Total Submissions: 460,446
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int len = triangle.size();
if (len == 0) {
return 0;
}
if (len == 1) {
return triangle.get(0).get(0);
}
for (int i = 1; i < len; i++) {
List<Integer> l = triangle.get(i);
for (int j = 0; j <= i; j++) {
int value = -1;
if (j == 0) {
value = l.get(j) + triangle.get(i - 1).get(0);
} else if (j == i) {
value = l.get(j) + triangle.get(i - 1).get(j - 1);
} else {
value = l.get(j) + Math.min(triangle.get(i - 1).get(j - 1), triangle.get(i - 1).get(j));
}
l.set(j, value);
}
}
int res = Integer.MAX_VALUE;
for (int n : triangle.get(len - 1)) {
res = Math.min(res, n);
}
return res;
}
}