My bottom up method in java, how can I improve it?


  • 0
    L
     public int minimumTotal(List<List<Integer>> triangle) {
            int[] cur = new int[triangle.size()];
            //initial cur
            for(int i = 0; i < triangle.size(); i++) 
                cur[i] = triangle.get(triangle.size() - 1).get(i);
            
            for(int i = triangle.size() - 2; i >= 0; i--) {
                for(int j = 0; j <= i; j++) {
                   //update cur[] for each row, cur[i] is the minimum path value from bottom to node i;
                   if(cur[j] > cur[j+1]) {
                        cur[j] = cur[j+1] + triangle.get(i).get(j);
                   }else{
                        cur[j] += triangle.get(i).get(j);
                   }
                }
            }
            return cur[0];
        }

Log in to reply
 

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