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.

Runtime: 444 ms

动态规划思想: 从上到下一行一行的扫描并加入总体,dp[i]记录每扫描完一行,并途过本行i元素的最短路径,故dp[]记录到目前为止所有路径的长度。O(n) space

public class Solution {

public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {

int[] dp=new int[triangle.size()];

if(triangle.size()==0) return 0;

if(triangle.size()==1) return triangle.get(0).get(0);

dp[0]=triangle.get(0).get(0);

for(int i=1;i<triangle.size();i++){

for(int j=i;j>=0;j--){

if(j==0) dp[0]+=triangle.get(i).get(0);

else if(j<i) dp[j]=triangle.get(i).get(j)+ Math.min(dp[j],dp[j-1]);

else dp[j]=dp[j-1]+triangle.get(i).get(j);

}

}

int ret=Integer.MAX_VALUE;

for(int i=0;i<dp.length;i++){

if(dp[i]<ret) ret=dp[i];

}

return ret;

}

}

[LeetCode][Java]Triangle@LeetCode的更多相关文章

  1. [LeetCode][Java]Candy@LeetCode

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Image 抠图,背景透明处理

    import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.i ...

  2. Python算法-冒泡排序

    #coding:utf-8 """ 冒泡排序 原理:依次重复访问每一个需要排序的元素,每次比较相邻的两个元素是否符合顺序,若不符合就交换,直到没有不符合顺序的为止. &q ...

  3. {POJ}{3925}{Minimal Ratio Tree}{最小生成树}

    题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ G++里面需要cstr ...

  4. Node.js 中MongoDB的基本接口操作

    Node.js 中MongoDB的基本接口操作 连接数据库 安装mongodb模块 导入mongodb模块 调用connect方法 文档的增删改查操作 插入文档 方法: db.collection(& ...

  5. mssql2000 身份证号码验证

    CREATE VIEW thisDate --返回当前日期,因为自定义函数中不能使用GETDATE() AS ),) AS aDate )) /*mssql2000 返回值=0,身份证校验正确 1:位 ...

  6. MVC Razor基础

    @ 可以编写一条C#语句@{} 可以编写一组C#语句@: 将文字内容直接输出到页面上去@() 在一句中将一段C#代码包括起来,证明这一句完整的C#代码 Razor解决路径问题: Html.Action ...

  7. ubuntu 16 mysql安装包安装 (推荐在线安装)

    /etc/init.d/中的文件命名为mysql cp好各种文件后 ./mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/my ...

  8. 查看已安装的CentOS版本信息:

    如何查看已安装的CentOS版本信息: 1)[root@localhost ~]# cat /proc/version Linux version 2.6.18-194.el5 (mockbuild@ ...

  9. array_filter,匿名函数

    static function get_categoryII() { return array( array('id' => 1, 'c1id' => 1, 'name' => '1 ...

  10. js event 事件冒泡和事件捕获详细介绍

    . 参考: http://www.jb51.net/article/42492.htm 图: 假设一个元素div,它有一个下级元素p.<div> <p>元素</p> ...