Triangle leetcode 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.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的更多相关文章
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Pascal's Triangle(Java实现)
这是悦乐书的第170次更新,第172篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第29题(顺位题号是118).给定非负整数numRows,生成Pascal三角形的第一个 ...
- [LeetCode][Java]Triangle@LeetCode
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [Leetcode][JAVA] Pascal's Triangle I, II
Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...
- 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, ...
- 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 ...
- 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 ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
随机推荐
- 深入理解指针—>结构体里的成员数组和指针
单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个结论,相信这篇文章会对你理解C语言有帮助.这篇文章产生的背景是在微博上,看到@Laruence同学出了一个关于C语言的题,微博链接.微博截图如 ...
- .babelrc文件的一些简单的配置
首先现在根目录先生存.babelrc文件 这个文件是用来设置转码的规则和插件的 如果想使用es6语法,必须安装插件 npm install babel-preset-es2015 { "pr ...
- 接口开发-集成数据库操作(mybatis)
关于数据存储,最常用的方式就是存到数据库,此篇以MySQL数据库为例,以mybatis框架完成数据库的操作. 一.添加对应依赖 <!-- 数据库:MySQL --> <depende ...
- 聚币网API使用教程 demo
原文 http://30daydo.com/article/181 目前还在完善,等功能完善了,就更新到csdn. 更新 2017-05-27 官方有API的文档,可是这个文档就像一个草稿一样,两个基 ...
- TextAppearance.Material.Widget.Button.Inverse,Widget.Material.Button.Colored
编译xamarin android项目报错: android:TextAppearance.Material.Widget.Button.Inverse android:Widget.Material ...
- 在ASP.NET MVC控制器中获取链接中的路由数据
在ASP.NET MVC中,在链接中附加路由数据有2种方式.一种是把路由数据放在匿名对象中传递: <a href="@Url.Action("GetRouteData&quo ...
- 在ASP.NET MVC中使用Knockout实践09,自定义绑定
Knockout真正强大之处在于绑定机制,通过data-bind属性值体现绑定,不仅可以绑定值,还可以绑定事件,甚至可以自定义绑定. 从一个例子看Knockou的绑定机制 假设想给一个button元素 ...
- 在ASP.NET MVC中使用Knockout实践01,绑定Json对象
本篇体验在ASP.NET MVC下使用Knockout,将使用EF Code First创建数据库.最后让Knockout绑定一个Json对象. 创建一个领域模型. namespace MvcAppl ...
- redis哈希缓存数据表
redis哈希缓存数据表 REDIS HASH可以用来缓存数据表的数据,以后可以从REDIS内存数据库中读取数据. 从内存中取数,无疑是很快的. var FRedis: IRedisClient; F ...
- C#编程小结----集合的小小总结
集合的小结 以上文章介绍了如何处理不同类型的集合,数组的大小是固定的,但可以使用列表作为动态增长的集合.队列以先进先出的方式访问元素.栈以后进先出的方式访问元素.链表可以快速的插入和删除元素,但搜索操 ...