[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 ...
随机推荐
- JAVA 多线程和并发学习笔记(二)
一.Java中创建线程方法 1. 继承Thread类创建线程类 定义Thread类的子类,重写该类的run()方法.该方法为线程执行体. 创建Thread子类的实例.即线程对象. 调用线程对象的sta ...
- Windows资源管理器 已停止工作
解决方案:http://jingyan.baidu.com/article/5225f26b6aa830e6fa0908a8.html
- nohup输入密码后继续后台运行
Linux/Unix 是真正的多用户,多任务.Linux 提供了 fg 和bg 命令,让你轻松调度正在运行的任务. 假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用 Ctr ...
- mybatis关联查询,一对一,一对多
注:这篇文章的代码有部分删减,不能直接使用,不过关键代码都存在 应用场景: 想用mybatis做关联查询,并且把查询出的数据自动组装成对象可以使用关联查询. 1.一对一实现 例如:一部小说,属于一个 ...
- The Magic only works with total devotion of one's heart
The Magic only works with total devotion of one's heart All tools and equipments are useless without ...
- cin判断读取结束 C++语言
cin是C++的输入流,可以通过>>进行读取. 判断读取结束,一般有两种方法,具体取决于与输入的约定. 1 以特殊值结尾. 如输入整数,以-1结束,那么当读到-1的时候,就确定读取结束了. ...
- MongoDB-MMS使用总结
环境:阿里云 系统:ubuntu 12.04 数据库:MongoDB shell version: 2.0.4 登录MMS,注册相应用户 根据文档开始安装:Install the Monitoring ...
- 创建Android项目时出错——No resource found that matches the given name 'Theme.AppCompat.Light'
创建Android项目时出错,error: Error retrieving parent for item: No resource found that matches the given nam ...
- Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告
1. 关于Karma Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner). 该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuou ...
- gcc相关
linux操作系统上面开发程序, 光有了gcc 是不行的 它还需要一个 build-essential软件包作用是提供编译程序必须软件包的列表信息 也就是说 编译程序有了这个软件包它才知道 头文件 ...