Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
题意:给定一个数组代表一个三角形,找出其中和最小的路径
解决思路:类似dfs的思路
代码:
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle.size() == 0){
return 0;
}
for(int i = triangle.size() - 2; i >= 0; --i){
for(int j = 0; j <= i; ++j){
List<Integer> next = triangle.get(i + 1);
int sum = Math.min(next.get(j), next.get(j + 1)) + triangle.get(i).get(j);
triangle.get(i).set(j, sum);
}
}
return triangle.get(0).get(0);
}
}