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. Orcle基本语句(六)

    -- Created on 2017/1/5 by ADMINISTRATOR DECLARE -- Local variables here i ; v_name ) := '张晓风'; v_age ...

  2. 转:MyBean简介

                        (在开始之前,非常感谢 D10.天地弦) 1.1 概述 MyBean是一个用于Delphi应用程序开发的开源.轻量级.可配置插件框架.它通过巧妙的系统架构设计, ...

  3. CSS继承

    不可继承的:display.margin.border.padding.background.height.min-height.max-height.width.min-width.max-widt ...

  4. Android-monkey稳定性测试(多台设备同时进行)

                                       1.目的(原创文章,转载请注明出处-) 主要为指引开展android平台应用的稳定性测试,尽可能地在应用发布前发现crash及an ...

  5. 在sql server中利用with as实现递归功能

    在sqlserver2005之前,要实现递归功能比较麻烦,比如可能会要用到临时表与while语句来循环.自sqlserver2005之后,新增了with as功能语法,即 公用表达式(CTE),让递归 ...

  6. VR发展的最大障碍在于内容?

    VR目前基本处于半死不活的状态,国内基本就是一堆的VR“盒子”在浑水摸鱼,就小米有点自知之明,冠以“玩具”的定位.但是说到VR发展的最大问题,居然说是什么内容没有吸引力,真让人无语啊.另外,还有什么价 ...

  7. 驱动学习---PAE--virtual address to physics address

    PAE是Physical Address Extension的缩写,即物理地址扩展.简单来说,就是把IA-32处理器的寻址能力从原来的4GB扩展到64GB.寻址4GB空间,要求物理地址的宽度为32位. ...

  8. 协同开发中SVN的使用建议

    协同开发中SVN的使用建议 1.  注意个人账户密码安全 各员工需牢记各自的账户和密码,不得向他人透漏,严禁使用他人账户进行SVN各项操作(主要考虑每个SVN账号的使用者的权限范围问题).如有忘记,请 ...

  9. 小白如何进入IOS,答案就在这里

    ***对于进来看过我博客的博友们,请看一下最后面的几道题,觉得可以的可以自己私下做一下,有不懂的我们可以相互交流*** 现在我来说一下我们IOS需要的基础,现在用的比较多的就是swift语言. 首先, ...

  10. 使用git从服务器下载已存在的项目文件

    在项目所在路径下输入: git remote -v 获得项目在服务器下的路径如下: origin ssh://git@ygl-redis:300/home/git/perfectunits-iphon ...