Triangle
ID:120
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output: 11
Explanation: The triangle looks like:
2
3 4
6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).Idea
Code
public int minimumTotal(List<List<Integer>> triangle) {
int[] dp = new int[triangle.size()];
for(int i = 0; i<triangle.size(); i++){
dp[i] = triangle.get(triangle.size()-1).get(i);
}
for(int i = triangle.size()-2; i>=0; i--){
for(int j = 0; j<=i; j++){
dp[j] = Math.min(dp[j],dp[j+1])+triangle.get(i).get(j);
}
}
return dp[0];
}Last updated