[LeetCode][Java]Triangle@LeetCode
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.
Runtime: 444 ms
动态规划思想: 从上到下一行一行的扫描并加入总体,dp[i]记录每扫描完一行,并途过本行i元素的最短路径,故dp[]记录到目前为止所有路径的长度。O(n) space
public class Solution {
public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
int[] dp=new int[triangle.size()];
if(triangle.size()==0) return 0;
if(triangle.size()==1) return triangle.get(0).get(0);
dp[0]=triangle.get(0).get(0);
for(int i=1;i<triangle.size();i++){
for(int j=i;j>=0;j--){
if(j==0) dp[0]+=triangle.get(i).get(0);
else if(j<i) dp[j]=triangle.get(i).get(j)+ Math.min(dp[j],dp[j-1]);
else dp[j]=dp[j-1]+triangle.get(i).get(j);
}
}
int ret=Integer.MAX_VALUE;
for(int i=0;i<dp.length;i++){
if(dp[i]<ret) ret=dp[i];
}
return ret;
}
}
[LeetCode][Java]Triangle@LeetCode的更多相关文章
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- switch能使用的数据类型有6种
byte.short.char.int.String.枚举
- js的原型模式
以下内容来自<JavaScript高级程序设计>第三版 我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个指针,指向一个对象,而这个对象的用途是包含可以由特定类型的所 ...
- Lua 5.2 编译 For Windows
body { font-family: 微软雅黑; font-size: 11pt; line-height: 1.5; } html, body { color: #000000; backgrou ...
- 通过sqoop来传输mysql/oracle/vertica数据至HBASE
首先要注意将连接用的jar包,放到sqoop目录下,我的是/var/lib/sqoop 如果没有主键,则要加上-m 1 export正确的jdk目录 当做key的列必须唯一存在,不然报错 --mysq ...
- WCF 编程实验室
最近由于项目需要,简单研究了一下.NET WCF编程. 首先,简单说下WCF是什么,WCF 本质上,是一种开发框架.它用来开发类似COM+ .WEB SERVICE 这样“远程方法调用” 功能. 普通 ...
- information_schema系列五(表,触发器,视图,存储过程和函数)
这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益. 1:TABLES TABLES这张表毫无疑问了,就是记录的数据库中表 ...
- php生成json或者xml数据
, ,'数据返回成功',$arr);echo $xml;?>
- Owin SelfHost Asp.net WebApi 遇到 No type was found that matches the controller named 'ControllerName' 异常的解决方案
问题背景:在使用普通的SelfHost时,调用其它工程的dll(其实就是把WebApi写到一个单独的工程方便管理),通过加载其他工程的dll然后再访问webapi是没有问题的. 但是在使用Owin S ...
- vc++ 判断文件或是文件夹是否存在,比较好的做法
#include <windows.h> void main() { //文件或文件夹都可以判断,最后的\\号有无都没关系 !=GetFileAttributes("D:\\My ...
- log4net 记录日志到sqlserver
参考:http://blog.csdn.net/niuyongjie/article/details/5777625 demo