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.
链接: http://leetcode.com/problems/triangle/
题解:
自底向上dp。虽然是个简单题, 但自己现在也能写出一些比较简练的代码了,是进步,要肯定。晚上东方串店撸串去!
Time Complexity - O(n),Space Complexity - O(1)。
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if(triangle == null || triangle.size() == 0)
return 0; for(int i = triangle.size() - 2; i >= 0; i--) {
for(int j = 0; j < triangle.get(i).size(); j++) {
triangle.get(i).set(j, triangle.get(i).get(j) + Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1)));
}
} return triangle.get(0).get(0);
}
}
二刷:
一般来说对于题目给出的数据结构比如Tree或者List<>不要轻易修改。我们另外建立一个数组来dp就好了。这里主要使用了自底向上的思路,先初始化dp数组为triangle的最后一行,再用转移方程dp[i] = triangle.get(i).get(j) + Math.min(dp[i], dp[i + 1])就好了。要注意边界条件。
Java:
Time Complexity - O(n),Space Complexity - O(n)。
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) {
return Integer.MIN_VALUE;
}
int rowNum = triangle.size();
List<Integer> lastRow = triangle.get(rowNum - 1);
int[] paths = new int[rowNum]; for (int i = 0; i < lastRow.size(); i++) {
paths[i] = lastRow.get(i);
}
for (int i = rowNum - 2; i >= 0; i--) {
for (int j = 0; j < triangle.get(i).size(); j++) {
paths[j] = triangle.get(i).get(j) + Math.min(paths[j], paths[j + 1]);
}
}
return paths[0];
}
}
三刷:
Java:
in-place:
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int min = Integer.MAX_VALUE;
if (triangle == null || triangle.size() == 0) return Integer.MAX_VALUE;
for (int i = triangle.size() - 2; i >= 0; i--) {
for (int j = 0; j < triangle.get(i).size(); j++) {
triangle.get(i).set(j, triangle.get(i).get(j) + Math.min(triangle.get(i + 1).get(j), triangle.get(i + 1).get(j + 1)));
}
}
return triangle.get(0).get(0);
}
}
Use auxiliary list
public class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int min = Integer.MAX_VALUE;
if (triangle == null || triangle.size() == 0) return Integer.MAX_VALUE; List<Integer> res = new ArrayList<>(triangle.get(triangle.size() - 1)); for (int i = triangle.size() - 2; i >= 0; i--) {
for (int j = 0; j < triangle.get(i).size(); j++) {
res.set(j, triangle.get(i).get(j) + Math.min(res.get(j), res.get(j + 1)));
}
}
return res.get(0);
}
}
Reference:
https://leetcode.com/discuss/5337/dp-solution-for-triangle
https://leetcode.com/discuss/10131/my-java-version-solution-with-o-n-space-accepted
https://leetcode.com/discuss/23544/my-8-line-dp-java-code-4-meaningful-lines-with-o-1-space
https://leetcode.com/discuss/20296/bottom-up-5-line-c-solution
120. Triangle的更多相关文章
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode - 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- 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
原题:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacen ...
- LeetCode OJ 120. Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 120. Triangle(中等)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- uva247 - Calling Circles(传递闭包+DFS)
题意:两人相互打电话(直接或间接),则在一个电话圈.即a给b打电话,b给c打电话,则a给c间接打电话. 注意:1.注意标记.2.注意输出格式. #include<iostream> #in ...
- PerformSelector may cause a leak because its selector is unknown 解决方法
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3801030.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- 结构体的malloc与数组空间
结构体的malloc 如果结构体中有指针,对结构体的malloc 和其指针成员变量的malloc是没有关系的 结构体malloc的是存储自己地址的 忘记了面试常考试的sizeof的几个主要点 ==== ...
- 在20上链接db2
首先 db2 connect to CICMDB user ptqs using ptqs; db2进入,出现db2 prep cperftest_bysqlc.sqC bindfile; 就可以sq ...
- 父节点使用css的transform: translate(0, 0)时position:fixed在chrome浏览器中无效
今天在做移动端的页面,无意间发现了一个Chrome浏览器下的一个bug,在使用CSS3的transform: translate(0, 0)属性对节点A进行位置转化,此时A节点下面有一个字节点B,节点 ...
- C#快速学习笔记(译)续一
6.虚拟和非虚拟函数 下面是一个非虚拟函数 using System; namespace Test2 { class Plane { public double TopSpeed() {return ...
- Python 中的引用和类属性的初步理解
最近对Python 的对象引用机制稍微研究了一下,留下笔记,以供查阅. 首先有一点是明确的:「Python 中一切皆对象」. 那么,这到底意味着什么呢? 如下代码: #!/usr/bin/env py ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- SQL优化的四个方面,缓存,表结构,索引,SQL语句
一,缓存 数据库属于 IO 密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所 ...
- angularApi网站用vue重构
最近在博客园上看到不少关于vue的文章但感觉都是在简单原生写法上,真正vue在实际开发中的优点组件化,spa应用,路由好像都没涉及到,我在学angular1的时候发现没有中文版的api,于是本人不才弄 ...