[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 ...
随机推荐
- 小学了一下css hack
实例讲解: Hack应用情境(一) 适用范围:IE:6.0,IE7.0,IE8.0之间的兼容 实例说明: 使用了渐进识别的方式,从总体中逐渐排除局部.首先,巧妙的使用“\9”这一标记,将IE游览器从所 ...
- C#中的 正则表达式
String 类包括许多字符串搜索和替换方法,当你要在较大字符串中定位文本字符串时,可以使用这些方法. 当你希望在较大字符串中定位若干子字符串之一时,或者当你希望在字符串中标识模式时,正则表达式最有用 ...
- Spring MVC入门知识总结
2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...
- margin:0 auto在IE中失效的解决方案
转自:http://www.cnblogs.com/hongchenok/archive/2012/11/29/2795041.html 最近在开发项目的时候,发现在火狐浏览器中设置外容器margin ...
- 【EF学习笔记10】----------主从表级联操作
主从表 级联新增 Console.WriteLine("=========主从表 级联新增=========="); using (var db = new Entities()) ...
- delphi XE Berlin ReadProcessMemory WriteProcessMemory
delphi XE,Berlin [dcc32 Error] Unit9.pas(93): E2033 Types of actual and formal var parameters must ...
- 修改Django的默认打印时间
环境 Django版本:1.10 前言 默认情况下,Django会把日期按照“月份 天数, 年”的格式打印,比如2003年2月4日会打印成“Feb. 4, 2003”,这种格式对于西方人来说很好看,但 ...
- redis:hash 数据类型
简介 Redis Hashes是字符串字段和字符串值之间的映射,所以它们是完美的表示对象(eg:一个有名,姓,年龄等属性的用户)的数据类型.新建一个hash对象时开始是用zipmap(又称为small ...
- linux查看访问windows共享目录NT_STATUS_DUPLICATE_NAME问题解决
linux查看访问windows共享目录NT_STATUS_DUPLICATE_NAME问题解决 [jason@superfreak ~]$ smbclient //powerhouse-smb.my ...
- file access , argc, argv[ ]
_____main函数含有 两个参数 ,argc ,argv[] 这两个参数用以指示命令行输入的参数信息. argc 的值是输入的参数的数量.argv是一个数组,每个数组元素指向一个string字符串 ...