triangle(三角形)——leetcode

2791 ワード

タイトルは以下の通りです:Given a triangle,find the minimum path sum from top to bottom.Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ]
The minimum path sum from top to bottom is11(i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
問題は以下の通りです.
public static int minimumTotal(ArrayList> triangle) {
    int []d=new int [1];
    d[0]=triangle.get(0).get(0).intValue();
    for(int i=1;iint min=Integer.MAX_VALUE;
    for(int i=0;iif(min>d[i]) {
            min=d[i];
        }
    }
    return min;
}
public static int [] dp(int []d,ArrayList> triangle,int now) {
    int []p=new int [d.length+1];
    p[0]=d[0]+triangle.get(now).get(0).intValue();
    p[now]=d[now-1]+triangle.get(now).get(now).intValue();
    for(int j=1;jint min=d[j-1];
        if(d[j]get(now).get(j).intValue();
    }
    return p;
}