一.题目描述
给定一个三角形 triangle
,找出自顶向下的最小路径和。
https://leetcode-cn.com/problems/triangle/
二.代码
public int minimumTotal(List<List<Integer>> triangle) {
int n = triangle.size();
int[] dp = new int[n];
List<Integer> list1 = triangle.get(n - 1);
for (int i = 0; i < n; i++) {
dp[i] = list1.get(i);
}
for (int i = n - 2; i >= 0; i--) {
List<Integer> list = triangle.get(i);
for (int j = 0; j <= i; j++) {
dp[j] = Math.min(dp[j], dp[j + 1]) + list.get(j);
}
}
return dp[0];
}