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 ...
随机推荐
- Windows Phone 8.1 页面导航
1. Windows Phone 8.1 的应用框架 一个应用拥有 1 个 Window,一个 Window 包含 1 个 Frame,一个 Frame 包含 多个 Page. 获取 Frame 的方 ...
- javascript之高级函数应用思想
1.级联函数:应用对象方法调用的连写 function A(){ this.a = ''; this.b = ''; this.c = ''; } //改造一下 A.prototype = { A.p ...
- DTCMS自定义标签:面包屑导航,栏目中通过栏目调用名称获得栏目名称
DTcms.Web.UI\Label\category.cs中增加标签 /// <summary> /// 自定义:通过类别name获得类别title /// </summary&g ...
- C# Json数据反序列化为Dictionary并根据关键字获取指定值1
Json数据: { "dataSet": { "header": { "returnCode": "0", " ...
- PHP json_encode中日语问题
<?php header('Content-type:text/html;charset=utf-8'); $s = array('message'=>'4月以降.遺体の捜索活動が続けられ ...
- python学习之html从0开始(二)
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- IE11下ASP.NET Forms身份认证无法保存Cookie的问题
IE11下ASP.NET Forms身份认证无法保存Cookie的问题 折腾了三四天,今天才找到资料,解决了. 以下会转贴,还没来得及深究,先放着,有空再学习下. ASP.NET中使用Forms身份认 ...
- Random类(java.util)
转自 Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要的随机数字. 相同种子数的Rand ...
- WPF学习笔记4——Layout之2
下面简单介绍常见的面板. 一.Grid 1.Grid关于调整行列距离有三种方法:绝对大小,自动大小,比例大小.如下: <ColumnDefinition Width="100" ...
- oracle查询和设置过期时间
第一步:找到oracle 打开enterprise Manager Console如下图: 第二步,找到概要文件: sys 用户进入,找到你的数据库(如:ora8)-“安全性”-"用户&qu ...