Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
input:
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
这道题解法比较巧妙 从下往上走 直到汇聚到一个点
而且也要明白这个三角形
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int level = triangle.size();
int[] dp = new int[level];
for (int i = 0; i < level; i++) {
dp[i] = triangle.get(level - 1).get(i);
}
for (int i = level - 2; i >= 0; i--) { //starting from the second last level
for (int j = 0; j <= i; j++) {
dp[j] = Math.min(dp[j], dp[j+1]) + triangle.get(i).get(j);
}
}
return dp[0];
}
}