LeetCode 120. 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.
题目标签:Array, Dynamic Programming
题目给了我们一个三角的array list, 让我们从中找到最小值的路线。一旦看到这种每一个点可以选择路线的,有不同可能性的,大多都是 DP 问题。一开始自己写了一个 复制一个 三角的array list, 然后从上到下的遍历,把每一个种可能都存到新的三角 array list。但是用了过多的 extra space, 没有满足它的要求。 所以通过后发现速度太慢,想了一会就放弃了,去网上找答案。有时候如果认认真真自己想一道这样类似的题目,可能会用3,4个小时的时间,而且,还不一定想的出正确答案,所以通常花费1个多小时的话,就果断去找答案了。
只用O(n) 空间的方法:
设立一个 n+1 array,然后从最下面一行倒着遍历回第一行,对于n+1 array里面的数字,取它和它后面 两者之中 小的那一个数字,加上 在三角里相对应位置的数字, 存入n+1 array。 基本思想是,利用倒着遍历的方法,可以把每一个数字的最优选择path 存入n+1 array里,当遍历回第一行的时候,因为只有一个数字,那么取得的path 一定是最优的那一个,换句话说,就是minimum path sum。
举例: 1 5 3 2 为最优路线
4 8 对于每一个数字,选下面相邻2个中小的那一个 + 自己 一直遍历回第一行
0 0 0 0 0 设立的1d array 相当于在这个位置
设立n + 1 array: 从三角最下面遍历回最上面, 每次在1d array 里 取相邻两个种小的那一个 + 三角里对应位置的数字
0 0 0 0 0 初始为0,为什么要多一个呢,因为每次都是取当前和后面一个数字比较,所以当遍历到第四个的时候,需要和第五个比较
遍历三角开始, 结合上面三角图形来看
4 1 8 3 0 遍历三角4183 -> 因为1d array里都是0,所以每次取相邻里小的那一个的话,都是0 + 三角里对应位置的数字,相当于 把最后一行复制一下
7 6 10 3 0 遍历三角657 -> 从之前存的里面,挑对应相邻两个中小的那个 + 自己 存入1d array
9 10 10 3 0 遍历三角34
11 10 10 3 0 遍历三角2 -> 得到答案11
Java Solution:
Runtime beats 58.69%
完成日期:08/27/2017
关键词:Array, Dynamic Programming
关键点:从下向上遍历,把最优路线存入1d array
class Solution
{
public int minimumTotal(List<List<Integer>> triangle)
{
// create a k+1 size array
int [] arr = new int[triangle.size() + 1]; // iterate from last row to first row
for(int i=triangle.size()-1; i>=0; i--)
{
// iterate each row from left to right
for(int j=0; j<triangle.get(i).size(); j++)
{
arr[j] = Math.min(arr[j], arr[j+1]) + triangle.get(i).get(j);
}
} /* the answer is the first number because we start from last row back to first row
and first row is just one number */
return arr[0];
}
}
参考资料:
https://discuss.leetcode.com/topic/22254/7-lines-neat-java-solution
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 120. Triangle (三角形)的更多相关文章
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- 120 Triangle 三角形最小路径和
给出一个三角形(数据数组),找出从上往下的最小路径和.每一步只能移动到下一行中的相邻结点上.比如,给你如下三角形:[ [2], [3,4], [6,5,7], [4,1,8,3]] ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [算法]LeetCode 120:三角形最小路径和
题目描述: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3]]自顶向下的最小路径和 ...
- [leetcode]120.Triangle三角矩阵从顶到底的最小路径和
Given a triangle, find the minimum path sum from top to bottom.Each step you may move to adjacent nu ...
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] 120. Triangle _Medium tag: Dynamic Programming
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode 120]triangle 空间O(n)算法
1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
随机推荐
- python读取外部文件
>>> pd.read_excel('c://111.xlsx') 年度排名 历史排名 电影名称 总票房 总人次 总场次 上映年份 操作 0 1 1 美人鱼 NaN -- -- 20 ...
- Jquery第三篇【AJAX 相关的API】
前言 前面我们已经学了讲解了Jquery的选择器,关于DOM 的API还有事件的API.本博文需要讲解Jquery对AJAX的支持- 我们在开始使用JavaScript学习AJAX的时候,创建异步对象 ...
- Servlet第一篇【介绍Servlet、HTTP协议、WEB目录结构、编写入门Servlet程序、Servlet生命周期】
什么是Serlvet? Servlet其实就是一个遵循Servlet开发的java类.Serlvet是由服务器调用的,运行在服务器端. 为什么要用到Serlvet? 我们编写java程序想要在网上实现 ...
- eclipse复制到IDEA中文不匹配,编译失败
今天使用把eclipse的包复制到Intellij Idea下,结果在编译的时候,它说我的数据是GBK,而Idea默认的数据是UTF-8,因此出错了... 解决:在项目中直接把对象的encoding. ...
- Hibernate映射乱码
1.修改数据库字符集:将数据库默认编码设置为UTF-8 CHARSET=utf8 2.配置Hibernate环境时将数据库URL设置成: jdbc:mysql://localhost:3306/dbN ...
- Spring4 customEditors
Spring4.0版本以后customEditors属性为Map<Class<?>, Class<? extends PropertyEditor>>,所以用key ...
- 初学者---AngularJS详解
AngularJS 简介 AngularJs是一个用于设计动态web应用的结构框架.首先,它是一个框架,不是类库,提供一整套方案用于设计web应用.它不仅仅是一个javascript框架,因为它的核心 ...
- OC——多态
书接上文,上文提到继承一个很大用途的是为了更好的实现多态,现在我们就来看看OC的多态. 多态:顾名思义就是好多种状态,以前学C#时候印象最深刻的例子是好多个类共同实现同一个接口,然后把这些类的对象都装 ...
- ACM学习之路___HDU 2066 一个人的旅行
Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...
- 我的python学习笔记一
我的python学习笔记,快速了解python,适合有C语言基础的. http://note.youdao.com/noteshare?id=93b9750a8950c6303467cf33cb1ba ...