[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 ...
随机推荐
- 会话状态已创建一个会话 ID,但由于响应已被应用程序刷新而无法保存它
解决方法是新建 全局应用程序类 Global.asax 在 Session_Start 函数中 添加 string sessionId = Session.SessionID; protected v ...
- 从入门到精通C++需要学的10本书
学习C++从入门到精通的的十本最经典书籍 文章来源中国IT实验室收集整理作者佚名更新时间2009-5-16 12:27:05 保存本文保存本文推荐给好友推荐给好友收藏本页收藏本页 欢迎进入C/C++ ...
- Dev tdxDBTreeView
可以快速的用tree展示层次结构,无需任何编码;对tree的操作会自动post到数据集:对数据集的操作会 在tree上表现 一.关键 设置 datasource displayField:节点的 ...
- Spark的数据存储
Spark本身是基于内存计算的架构,数据的存储也主要分为内存和磁盘两个路径.Spark本身则根据存储位置.是否可序列化和副本数目这几个要素将数据存储分为多种存储级别.此外还可选择使用Tachyon来管 ...
- public, protected and private inheritance in C++
Get from Stackoverflow. The details can easily understand from the below example. class A { public: ...
- 激活PHPStorm 2016.3
通过互联网激活PHPStorm 2016.3: http://jetbrains.tencent.click http://172.245.22.235:1017 http://idea.imsxm. ...
- FireBird.conf配置文件常用参数
1.RootDirectory "写上Firebird服务器的安装路径" 如果不对FbServer服务是企动会出错的.2.DatabaseAcces 指的是访问Firebird数 ...
- MarkDown初体验
初体验 写在前面 一周前第一次听说了MarkDown这个编辑器,通过它知道了LaTex,正好满足了我多年对网上博客里的公式简陋的表达的需求.起初,只是用到了LaTex公式这一个功能 , 对于主要文字的 ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- IIS7.5 webapi 不支持 Delete、Put 解决方法
在IIS管理界面选择API的项目,选择 “Features View”. 2. 选择 “Handler Mappings” 菜单. 3. 打开“WebDAV” 选项. 4. 点击 “Request ...