Java O(n) space solution?


  • 0
    N
    public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        int [] res = new int [triangle.size()];
        for(int i=0; i<res.length;i++)
        {
            res[i] = triangle.get(triangle.size()-1).get(i);
        }
        int len = triangle.size();
        for(int i = len-2; i>=0;i--)
        {
            for(int j=0;j<triangle.get(i).size();j++)
            {
               
                res[j] = Math.min(res[j],res[j+1])+triangle.get(i).get(j);
            }
        }
        return res[0];
    }
    

    }

    Is this a O(n) space solution? we just need to store the sum in one level down.


Log in to reply
 

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