120. Triangle
Last updated
Last updated
/**
* Time complexity :
* Space complexity : O(N) where N is the number of rows.
*/
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0) return 0;
int n = triangle.size();
int[] dp = new int[n+1];
for(int i = n-1; i >= 0; i--) {
List<Integer> row = triangle.get(i);
for(int j = 0; j < row.size(); j++) {
dp[j] = row.get(j) + Math.min(dp[j], dp[j+1]);
}
}
return dp[0];
}
}