import java.util.List;

/**
 * 120. 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).
 * 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.
 * Tags: Array Dynamic Programming
 * Analysis: DP
 * @author chenshuna
 */ 

public class Triangle {
    public static int minimumTotal(List<List<Integer>> triangle) {
        int minSum = Integer.MAX_VALUE;
        int[] sum = new int[triangle.size()];
        sum[0] = triangle.get(0).get(0);
        for(int i = 1; i < triangle.size(); i++){  // from top to bottom
            List<Integer> line = triangle.get(i);
            for(int j = i; j >= 0; j--){
                if(j == 0){
                    sum[j] += line.get(j);
                }
                else if(j == i){
                    sum[j] = sum[j - 1] + line.get(j);
                }
                else{
                    sum[j] = Math.min(sum[j], sum[j - 1]) + line.get(j);
                }
            }
        }
        for(int cnt : sum){
            minSum = Math.min(minSum, cnt);
        }
        return minSum;
    }

    public static void main(String[] args) {
        List<List<Integer>> test1 = new ArrayList<>();
        List<Integer> test3 = new ArrayList<Integer>();
        test3.add(4);
        test1.add(test3);
        List<Integer> test4 = new ArrayList<Integer>();
        test4.add(5);
        test4.add(7);
        test1.add(test4);
        List<Integer> test5 = new ArrayList<Integer>();
        test5.add(1);
        test5.add(8);
        test5.add(3);
        test1.add(test5);
        System.out.print(minimumTotal(test1));   
    }
}
@@ -582,16 +582,366 @@
 angle %7B%0A
+    public static int minimumTotalBottonUp(List%3CList%3CInteger%3E%3E triangle) %7B%0A        int%5B%5D dp = new int%5Btriangle.size() + 1%5D;%0A        for (int i = triangle.size() - 1; i %3E= 0; i--)%0A            for (int j = 0; j %3C triangle.get(i).size(); j++)%0A                dp%5Bj%5D = triangle.get(i).get(j) + Math.min(dp%5Bj%5D, dp%5Bj + 1%5D);%0A        return dp%5B0%5D;%0A    %7D%0A    %0A
     publ

add botton up solution