leetcode 120 Triangle ----- java

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.

求出最小的路径。

第一次做使用递归,超时了。

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {int len = triangle.size();

        int result = triangle.get(0).get(0);

        result = getResult(result,0,0,triangle);

        return result;

    }

    public static int getResult(int result,int pos,int num,List<List<Integer>> triangle){

        if( num == triangle.size()-1 )
            return result;
        int num1 = triangle.get(num+1).get(pos);
        int ans = result;
        ans += num1;
        ans = getResult(ans,pos,num+1,triangle);

        num1 = triangle.get(num+1).get(pos+1);
        result += num1;
        result = getResult(result,pos+1,num+1,triangle);
        return ans>result?result:ans;

    }
}

所以还是需要用DP。

比较简单的DP应用。

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {

        int height = triangle.size();

        int[] dp = new int[height];
        dp[0] = dp[0]+triangle.get(0).get(0);
        for( int i = 1;i<height;i++){
            int a = dp[0],b = dp[1];
            dp[0] = dp[0]+triangle.get(i).get(0);
            for( int j = 1;j<i;j++){
                dp[j] = Math.min(a,b)+triangle.get(i).get(j);
                a = b;
                b = dp[j+1];
            }
            dp[i] = a+triangle.get(i).get(i);
        }
        int result = dp[0];
        for( int i = 1;i<height;i++)
            result = Math.min(result,dp[i]);

        return result;

    }
}
时间: 2024-10-10 20:44:45

leetcode 120 Triangle ----- java的相关文章

[leetcode 120]triangle 空间O(n)算法

1 题目 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 

LeetCode 120. Triangle 20170706 部分之前做了没写的题目

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

[leetcode] 120 Triangle

感觉acm做过之后,这种题太基本了.... 没啥好说的,最简单的动态规划,找出状态转移方程就可以了.采用由下到上的思想(这样最后只需要取出dp[0][0]就是答案),本层每个结点的结果根据下面一行的路基累计和而计算,要么取左边的,要么取右边的,两者取最小的即可. 状态转移方程:triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]) class Solution { public: int minimumTotal(vec

Leetcode 120 Triangle (Dynamic Programming Method)

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

120. Triangle java solutions

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

leetcode 118 Pascal&#39;s Triangle ----- java

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 用的比价暴力的方法, 也是最快的. public class Solution { List list = new ArrayList<List<Integer>>(); public

LeetCode: 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

【LeetCode】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

Leetcode: Wildcard Matching. java

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p