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 ...
随机推荐
- 致vi老大 2011.1
文/安然 亲爱的,你即将离去 飞机起飞的一刻, 请珍藏起我们2010的回忆 桃源仙谷,曾留下我们踏青的足迹 难忘,石头上小憩 小北门外,我们在大排档里尽情欢喜 见证,杯盘狼藉 饺子店,是冬日里四面八方 ...
- linux下进入root
baoyu@ubuntu:~$ sudo password root sudo: password: command not found baoyu@ubuntu:~$ sudo passwd roo ...
- CICS日志---内存问题
Level 9 COCITOOL_XA: Connected! [2014-01-09 19:46:24.296834][22347888] Level 0 TestPerormence: GDAO ...
- C++对象的JSON序列化与反序列化探索续-复杂对象的序列化与反序列化
本文是基本上一篇博文进行改进而成,上一篇请见: C++对象的JSON序列化与反序列化探索 此处就不多说了,直接上代码. 1. 序列化基类 #pragma once #include <strin ...
- PHP文件操作
1.递归获取目录下文件的个数 function getFileCount($dir){ if(!is_dir($dir)) return false; //打开目录 $handle = opendir ...
- style、currentStyle、getComputedStyle区别介绍
style.currentStyle.getComputedStyle区别介绍 来自:蓝色天空 样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有 ...
- laravel扩展图片处理Intervention Image
github地址:https://github.com/Intervention/image
- Web开发——Http协议剖析
Http,即超文本传输协议,是建立在TCP/IP协议的基础上.在Web开发的过程中,Http协议是十分重要的,浏览器与服务器之间的交互就是基于Http协议的.Http协议如果展开全面讲解会有很多内容, ...
- (转载)SQLServer存储过程返回值总结
1. 存储过程没有返回值的情况(即存储过程语句中没有return之类的语句) 用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况 (1)假如通过查询 ...
- WPF学习笔记4——Layout之2
下面简单介绍常见的面板. 一.Grid 1.Grid关于调整行列距离有三种方法:绝对大小,自动大小,比例大小.如下: <ColumnDefinition Width="100" ...