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.e., 2 + 3 + 5 + 1 = 11).

题目解析:

标准DP解法的题, 使用一个数组存结果就可以。

思路是from bottom to top, 先把最后一层存入数组, 然后依次递归上面一层求最小值, 这样最后数组index = 0的位置就是所求. 代码如下:

 1 public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) { //标准DP解法, 使用一个数组来存储结果
 2         int[] res = new int[triangle.size()];
 3         int lastSize = triangle.size() - 1;
 4         for(int i = 0; i < triangle.get(lastSize).size(); i++)
 5             res[i] = triangle.get(lastSize).get(i);
 6
 7         for(int i = triangle.size() - 2; i >= 0; i--){
 8             for(int j = 0; j < triangle.get(i + 1).size() - 1; j++){
 9                 res[j] = triangle.get(i).get(j) + Math.min(res[j], res[j + 1]);
10             }
11         }
12         return res[0];
13     }
时间: 2024-10-05 06:28:18

Leetcode 120 Triangle (Dynamic Programming Method)的相关文章

[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] questions for Dynamic Programming

Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic P

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

LeetCode with Python -&gt; Dynamic Programming

198. House Robber You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected

leetcode || 120、Triangle

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

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

Given two words word1 and word2, find the minimum number of operations required to convert word1to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "ho

leetCode 357. Count Numbers with Unique Digits | Dynamic Programming | Medium

357. Count Numbers with Unique Digits Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example:Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,3