Another accepted Java solution


  • 2

    The idea is do a bottom up comparison.

    public class Solution {
        public int minimumTotal(List<List<Integer>> triangle) {
            int n = triangle.size();
            
            // initialize arr with the last row
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) 
                arr[i] = triangle.get(n - 1).get(i);
            
            // bottom up comparion
            for (int i = n - 2; i >= 0; i--) 
                for (int j = 0; j <= i; j++) 
                    arr[j] = Math.min(arr[j], arr[j + 1]) + triangle.get(i).get(j);
            
            return arr[0];
        }
    }

Log in to reply
 

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