您的位置:首页 > 其它

triangle(三角形)——leetcode

2017-05-23 16:51 323 查看
题目如下:

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<ArrayList<Integer>> triangle) {
int []d=new int [1];
d[0]=triangle.get(0).get(0).intValue();
for(int i=1;i<triangle.size();i++) {
d=dp(d,triangle,i);
}
int min=Integer.MAX_VALUE;
for(int i=0;i<d.length;i++) {
if(min>d[i]) {
min=d[i];
}
}
return min;
}
public static int [] dp(int []d,ArrayList<ArrayList<Integer>> 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;j<now;j++) {
int min=d[j-1];
if(d[j]<min) {
min=d[j];
}
p[j]=min+triangle.get(now).get(j).intValue();
}
return p;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 动态规划