forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_120.java
More file actions
22 lines (19 loc) · 685 Bytes
/
_120.java
File metadata and controls
22 lines (19 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.fishercoder.solutions;
import java.util.List;
public class _120 {
public static class Solution1 {
public int minimumTotal(List<List<Integer>> triangle) {
int n = triangle.size();
List<Integer> cache = triangle.get(n - 1);
for (int layer = n - 2; layer >= 0; layer--) {
//for each layer
for (int i = 0; i <= layer; i++) {
//check its very node
int value = Math.min(cache.get(i), cache.get(i + 1)) + triangle.get(layer).get(i);
cache.set(i, value);
}
}
return cache.get(0);
}
}
}