题目

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.

题解:

一道动态规划的经典题目。需要自底向上求解。

递推公式是: dp[i][j] = dp[i+1][j] + dp[i+1][j+1] ,当前这个点的最小值,由他下面那一行临近的2个点的最小值与当前点的值相加得到。

由于是三角形,且历史数据只在计算最小值时应用一次,所以无需建立二维数组,每次更新1维数组值,最后那个值里存的就是最终结果。

代码如下:

 1 public int minimumTotal(List<List<Integer>> triangle) {
 2     if(triangle.size()==1)
 3         return triangle.get(0).get(0);
 4         
 5     int[] dp = new int[triangle.size()];
 6 
 7     //initial by last row 
 8     for (int i = 0; i < triangle.get(triangle.size() - 1).size(); i++) {
 9         dp[i] = triangle.get(triangle.size() - 1).get(i);
     }
  
     // iterate from last second row
     for (int i = triangle.size() - 2; i >= 0; i--) {
         for (int j = 0; j < triangle.get(i).size(); j++) {
             dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
         }
     }
  
     return dp[0];
 }

Reference:  http://www.programcreek.com/2013/01/leetcode-triangle-java/

Triangle leetcode java的更多相关文章

  1. Pascal's Triangle leetcode java(杨辉三角)

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  2. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  3. LeetCode算法题-Pascal's Triangle(Java实现)

    这是悦乐书的第170次更新,第172篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第29题(顺位题号是118).给定非负整数numRows,生成Pascal三角形的第一个 ...

  4. [LeetCode][Java]Triangle@LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. [Leetcode][JAVA] Pascal's Triangle I, II

    Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  6. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  7. Pascal's Triangle 2(leetcode java)

    问题描述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return ...

  8. Pascal's Triangle II Leetcode java

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  9. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

随机推荐

  1. 洛谷P1220关路灯[区间DP 提前计算代价]

    题目描述 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯. 为了给村 ...

  2. HDU 4709 3-idiots FFT 多项式

    http://acm.hdu.edu.cn/showproblem.php?pid=4609 给一堆边,求这一堆边随便挑三个能组成三角形的概率. 裸fft,被垃圾题解坑了还以为很难. 最长的边的长度小 ...

  3. JDK7新特性<八>异步io/AIO

    概述 JDK7引入了Asynchronous I/O.I/O编程中,常用到两种模式:Reactor 和 Proactor.Reactor就是Java的NIO.当有事件触发时,我们得到通知,进行相应的处 ...

  4. HDU 3979 Monster (贪心排序)

    Monster Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. javascript UI框架

    http://www.jianshu.com/p/709e8d6c03c9 http://www.cnblogs.com/kingboy2008/p/5261771.html jQuery uiboo ...

  6. delphi ribbon使用

    http://blog.csdn.net/davinciyxw/article/details/5604209 1.TextEditor(barEditItem)取文本 string editValu ...

  7. [Winform]关于cefsharp触屏设备长按文本内容,崩溃问题的修复

    摘要 在之前遇到cefsharp,在触屏电脑上,长按文本内容,会崩溃的问题. 相关文章 当时遇到这样的问题,在cefsharp项目下提交了bug.已经修复,可以参考当时我提的bug,以及解决过程,可参 ...

  8. 解决iframe加载的内容有时显示有时不显示

    在ASP.NET MVC项目中遇到了这样的一个问题,假设父页面有一个iframe <iframe id=" width="100%" height="10 ...

  9. 初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化

    序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程.对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过网络发送给另外一台机器上运行的进程.本篇主要包括: ● 使用 ...

  10. Getting OS version with NDK in C c++获得版本号

    http://stackoverflow.com/questions/19355783/getting-os-version-with-ndk-in-c #include <cutils/pro ...