Easy To Understand Java Solution( go down stairs)


  • 0
    C
    import java.util.*;
    public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        int row = triangle.size();
        int[][] result = new int[row][row];
       
        result[0][0] = triangle.get(0).get(0).intValue();
        for(int i = 1; i < row; i++){
            result[i][0] = result[i-1][0]+triangle.get(i).get(0).intValue();
            result[i][i] = result[i-1][i-1]+triangle.get(i).get(i).intValue();
        }
        
        for(int i = 2; i < row; i++){
            for(int j = 1; j < triangle.get(i).size()-1;j++){
                result[i][j] = Math.min(result[i-1][j-1],result[i-1][j]) + triangle.get(i).get(j).intValue();
            }
        }
    
        int min = result[row-1][0];
        for(int i = 0; i < row; i++){
            if(result[row-1][i]<min){
                min = result[row-1][i];
            }
        }
        
        return min;
        
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.