Triangle

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 is 11 (i.e., 2 + 3 + 5 + 1 = 11).
 1 public class Solution {
 2     public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
 3         int row = triangle.size();
 4         if(row<=0) return 0;
 5         int num[] = new int [triangle.get(row-1).size()];
 6         for(int i=row-1;i>=0;i--){
 7             int size = triangle.get(i).size();
 8             for(int j=0;j<size;j++){
 9                 if(i==row-1)
10                     num[j] = triangle.get(i).get(j);
11                 else{
12                     num[j] = Math.min(num[j],num[j+1])+ triangle.get(i).get(j);
13                 }
14             }
15         }
16         return num[0];
17     }
18 }
View Code

 

posted @ 2014-02-22 13:10  krunning  阅读(175)  评论(0编辑  收藏  举报