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

一开始是按照自顶向下的方式来寻找最小值,边界处理有点麻烦,调了几次。

 1 public class Solution {
 2     public int minimumTotal(List<List<Integer>> triangle) {
 3         int len = triangle.size();
 4         int[] dp = new int[len+1];
 5         Arrays.fill(dp,Integer.MAX_VALUE);
 6         dp[1] = triangle.get(0).get(0);
 7
 8         for(int i = 1; i <len; i++){
 9             for(int j = triangle.get(i).size(); j > 0; j--){
10                 dp[j] = triangle.get(i).get(j-1) + Math.min(dp[j],dp[j-1]);
11             }
12         }
13         int min = Integer.MAX_VALUE;
14         for(int i = 1;i <= triangle.size(); i++){
15             min = Math.min(min,dp[i]);
16         }
17         return min;
18     }
19 }

解法二采用自底向上:

 1 public class Solution {
 2     public int minimumTotal(List<List<Integer>> triangle) {
 3         int len = triangle.size();
 4         int[] dp = new int[len];
 5         for(int i = 0; i< len; i++){
 6             dp[i] = triangle.get(len-1).get(i);
 7         }
 8         for(int i = len-2; i>=0; i--){
 9             for(int j = 0; j < triangle.get(i).size(); j++){
10                 dp[j] = triangle.get(i).get(j) + Math.min(dp[j],dp[j+1]);
11             }
12         }
13         return dp[0];
14     }
15 }
时间: 2024-11-01 10:42:54

120. Triangle java solutions的相关文章

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

119. Pascal&#39;s Triangle II java solutions

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 1 public class Solution { 2 public List<Integer> getRow(int rowIndex) { 3

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

47. Permutations II java solutions

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

63. Unique Paths II java solutions

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

304. Range Sum Query 2D - Immutable java solutions

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, co

54. Spiral Matrix java solutions

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. Subscribe to see which c

只用120行Java代码写一个自己的区块链

区块链是目前最热门的话题,广大读者都听说过比特币,或许还有智能合约,相信大家都非常想了解这一切是如何工作的.这篇文章就是帮助你使用 Java 语言来实现一个简单的区块链,用不到 120 行代码来揭示区块链的原理! “用不到120行 Java 代码就能实现一个自己的区块链!” 听起来不可思议吧?有什么能比开发一个自己的区块链更好的学习实践方法呢?那我们就一起来实践下! 因为我们是一家从事互联网金融的科技公司,所以我们采用虚拟资产金额作为这篇文章中的示例数据.大家可以先为自己想一个数字,后面我们会用

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