[Leetcode][JAVA] Triangled
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的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
- Scramble String leetcode java
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
随机推荐
- sql 简单事务例子
BEGIN TRY BEGIN TRAN ) BEGIN UPDATE table SET ... END ELSE BEGIN UPDATE table SET ... UPDATE table S ...
- Jmeter组件1. CSV Data Set Config
位置:Test Plan | Add | Config Element | CSV Data Set Config 意义: 脚本参数化 节省CPU跟内存(可以准备好数据文件去代替动态生成数据,节约CP ...
- operator 的两种用法
C++,有时它的确是个耐玩的东东,就比如operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换).1.操作符 ...
- cin判断读取结束 C++语言
cin是C++的输入流,可以通过>>进行读取. 判断读取结束,一般有两种方法,具体取决于与输入的约定. 1 以特殊值结尾. 如输入整数,以-1结束,那么当读到-1的时候,就确定读取结束了. ...
- Scala伴生类和伴生对象
单例对象与类同名时,这个单例对象被称为这个类的伴生对象,而这个类被称为这个单例对象的伴生类.伴生类和伴生对象要在同一个源文件中定义,伴生对象和伴生类可以互相访问其私有成员.不与伴生类同名的单例对象称为 ...
- GBDT基本理论及利用GBDT组合特征的具体方法(收集的资料)
最近两天在学习GBDT,看了一些资料,了解到GBDT由很多回归树构成,每一棵新回归树都是建立在上一棵回归树的损失函数梯度降低的方向. 以下为自己的理解,以及收集到的觉着特别好的学习资料. 1.GBDT ...
- BASE64 官方方法,我自己用的,注意记住换行问题。
TBase64Encoding.Base64.Encode(str) TBase64Encoding.Base64.Decode(str) 注意如果str很长,base64后的结果是每76个字符自动加 ...
- 自定义控件EditText
public class defineEditText extends EditText { Context context; Drawable d; public defineEditText(Co ...
- CSS3常用选择器(一)
在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素.比如最常用到的#id,.class,标签选择器. 随着CSS3到来,增加了很多新型选择器,这里就常用的做一个总结. 1.属性选择器. 在c ...
- Spring JDBC常用方法详细示例
Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...