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 ...
随机推荐
- kotlin 安装 使用
插件下载 下载 kotlin 扩展 . 可以 简写 findviewbyid 这些. 比如 id 是 textview 直接 这样赋值 textview.setText("测试文字" ...
- 【Codechef FRBSUM】【FJOI2016】【BZOJ4299】【BZOJ 4408】 可持久化线段树
4408: [Fjoi 2016]神秘数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 475 Solved: 287[Submit][Status ...
- BZOJ 1009 HNOI 2008 GT考试 递推+矩乘
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3679 Solved: 2254[Submit][Statu ...
- Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增
D. Animals and Puzzle 题目连接: http://codeforces.com/contest/713/problem/D Description Owl Sonya gave a ...
- myeclipse优化 Maven
1.禁用myeclipse updating indexes MyEclipse 总是不停的在 Update index,研究发现Update index...是Maven在下载更新,但很是影响mye ...
- 【精选】Jupyter Notebooks里的TensorFlow图可视化
[精选]Jupyter Notebooks里的TensorFlow图可视化 https://mp.weixin.qq.com/s?src=11×tamp=1503060682&a ...
- github入门教程:第一步
[git教程] 以前在网上找过一些,见 http://www.wojilu.com/Forum1/Topic/702 我自己会一边学,一边写教程,过程中有不明白的,会跟大家请教交流. ----- ...
- .net core程序部署
前期将一些程序切换到了.net core,本文这里记录下windows 下.net core程序部署相关的方法.有同样需求的朋友可以参考一下,以免少走一些弯路. .net core程序部署主要工作就是 ...
- spy++使用指南
很多朋友都对窗口句柄比较迷糊,这篇短文就以spy++这个软件为主,介绍下窗体句柄和使用按键插件时,如果对这个句柄发送消息,即所谓的后台挂机.spy++这个软件来自VC++,装好VC后,就可以在工具中看 ...
- win10系统更新不了,总出现错误0xc8000442
来自 win10系统更新不了,总出现错误0xc8000442,SHIZHI333的回答. 首先卸载制有第三方防护软件管管家类软件,再试试下面的方法:清理一下更新临时文件,具体操作如下: 1.右键点击开 ...