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.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,我们只要知道i-1层每个数字处的最小路径值,然后相应的选出小的那个加上第i层的对应数值,即可得到最新的最小路径值。注意最左和最右处的最小路径值只有一个来源(无法二选一)。最后得到最底层的所有最短路径值,选出最小的那个即可。

例如,对于示例中的三角,每一处的最小路径值为:

[
[2],
[5,6],
[11,10,13],
[15,11,18,16]
] 实际上,没有必要维护整个三角形(二维矩阵),而只需要维护两层的数据即可。比如如果要知道最底层的最小路径,只需要知道倒数第二层的最小路径。所以只需要两个数组即可。
代码如下:
     public int minimumTotal(List<List<Integer>> triangle) {
int[] dp = new int[triangle.size()];
int re = 0;
for(int i=0;i<triangle.size();i++) {
int[] temp = new int[triangle.size()];
for(int j=0;j<=i;j++) {
if(j==0) {
temp[j] = dp[j]+triangle.get(i).get(j);
re = temp[j];
}
else if(j==i)
temp[j] = dp[j-1]+triangle.get(i).get(j);
else
temp[j] = Math.min(dp[j],dp[j-1])+triangle.get(i).get(j);
re = Math.min(re, temp[j]);
}
dp = temp;
} return re;
}

[Leetcode][JAVA] Triangled的更多相关文章

  1. N-Queens II leetcode java

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

  2. Regular Expression Matching leetcode java

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

  3. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  4. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  5. [LeetCode][Java]Candy@LeetCode

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

  6. [Leetcode][JAVA] Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. 【目录】LeetCode Java实现

    这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...

  8. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  9. Scramble String leetcode java

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

随机推荐

  1. ajax 提交數據

    $.ajax({ type: "get", async: false, url: "/book/UpdateSession", data: { }, dataT ...

  2. easyui datagrid 跨页选择

    $.fn.extend( memberList ,{ quickSearch : function() { var time1 = new Date(); /* this.datagrid.datag ...

  3. python 格式化 json输出

    利用python格式化json 字符串输出. $ echo '{"json":"obj"}' | python -m json.tool 利用python -m ...

  4. CSS3基础 02(2D /3D)

    一.2D转换 概念:就是元素在2D平面上实现移动,旋转,缩放,斜切的操作就称之为2D转换 语法:transform:值 值:移动,旋转,缩放,斜切 (1.1)移动 transform:translat ...

  5. spark1.5.1环境搭建

    今天一个小伙伴找我搞spark,搞了一个中午都没搭建好.心里痒痒的.索性自己重来了一遍. 本来很简单的事情,被这小子搞的老复杂了.究其原因,有以下几点: 下载的软件不对,我用的是这两个软件 spark ...

  6. 利用js对象的特性,去掉数组中的重复项

  7. windows10-桌面图标不见了,资源管理器的桌面中可以看到??

    问题描述: 1. 桌面的图标,在桌面上看不到, 但是在通过资源管理器可以看到, 图标仍然在桌面 2. 桌面仍然可以右击, 就是看不见新建或者拷贝到桌面的所有图标 解决方案: Google 后请参考: ...

  8. Lua __index元方法

    [Lua __index元方法] 当你通过键来访问 table 的时候,如果这个键没有值,那么Lua就会寻找该table的metatable(假定有metatable)中的__index 键.如果__ ...

  9. hibernate学习(设计多对多 关系 映射)

    // package org.crazy.app.domain; import java.util.HashSet; import java.util.Set; import javax.persis ...

  10. 在ASP.NET中上传附件

    前台页面使用ASP控件:<asp:FileUpload ID="FileUpload" runat="server" Style="margin ...