LeetCode - 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.
思路:
1) 递归,代码很简单,但超时了
package recursion; import java.util.ArrayList;
import java.util.List; public class Triangle { public int minimumTotal(List<List<Integer>> triangle) {
int m = triangle.size();
return minPath(triangle, , , m, );
} private int minPath(List<List<Integer>> triangle, int row, int col, int m, int prevTotal) {
if (row >= m) return prevTotal;
prevTotal += triangle.get(row).get(col);
return Math.min(minPath(triangle, row + , col, m, prevTotal), minPath(triangle, row + , col + , m, prevTotal));
} }
2) 从下往上进行扫描
package recursion; import java.util.ArrayList;
import java.util.List; public class Triangle { public int minimumTotal(List<List<Integer>> triangle) {
int m = triangle.size();
int n = triangle.get(m - 1).size();
int[] res = new int[n + 1];
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < triangle.get(i).size(); ++j) {
res[j] = Math.min(res[j], res[j + 1]) + triangle.get(i).get(j);
}
}
return res[0];
} }
LeetCode - Triangle的更多相关文章
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode — triangle
/** * Source : https://oj.leetcode.com/problems/triangle/ * * * Given a triangle, find the minimum p ...
- [leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...
- LeetCode -- Triangle 路径求最小和( 动态规划问题)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode Triangle及其思考
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- LeetCode Triangle 三角形(最短路)
题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...
- leetcode—triangle
1.题目描述 Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adj ...
- LeetCode: Triangle 解题报告
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- 【C语言学习】《C Primer Plus》第8章 字符输入/输出和输入确认
学习总结 1.缓冲区分为完全缓冲区(fully buffered)I/O和行缓冲区(line-buffered)I/O.对完全缓冲输入来说,当缓冲区满的时候会被清空(缓冲区内容发送至其目的地).这类型 ...
- IE浏览器不能自动显示PDF文件的解决办法
今天更新了Adobe的PDF Reader,更新后发现在网页上无法预览PDF文件了,点击PDF的连接,浏览器就会提示下载或者打开,感觉很不爽,经过一番百度,找到了解决办法,在这里分享一下. 打开IE浏 ...
- Unity 热更新实例一、C#Light 和UI系统使用实例
接下来我会运用热更新的机制,逐步制作一些例子来阐释脚本系统如何和Unity结合. 脚本不限于使用C#Lite,但是C#Lite会有一些便利之处,请往下看. 结合机制也不限于这一种,但是C#Lite的设 ...
- Window Ghosting
最近工作中遇到Window Ghosting这个问题, 感觉挺有意思,这里简单记录下. 在XP时代我们的程序没有响应后只能通过任务管理器强制杀掉,但是Vista之后情况变了, 我们仍然可以拖动失去响应 ...
- [nRF51822] 1、一个简单的nRF51822驱动的天马4线SPI-1.77寸LCD彩屏DEMO
最近用nRF51822写了个天马4线SPI的1.77寸LCD彩屏驱动,效果如下: 屏幕的规格资料为:http://pan.baidu.com/s/1gdfkr5L 屏幕的驱动资料为:http://pa ...
- AWS助理架构师认证考经
上周考了亚马逊的解决方案架构师-助理级别的认证考试并顺利通过.这也算是对自己AWS服务熟悉程度的一种检验.在准备考试的过程中,把自己学习到的AWS知识都梳理了一遍,也算是收获颇丰.这次特意分享了该认证 ...
- Atitit 视频编码与动画原理attilax总结
Atitit 视频编码与动画原理attilax总结 1.1. 第一步:实现有损图像压缩和解压1 1.2. 接着将其量化,所谓量化,就是信号采样的步长,1 1.3. 第二步:实现宏块误差计算2 1.4. ...
- QQ表情动图,增加写博客的乐趣
QQ表情动图,增加写博客的乐趣 body{margin:0px;}
- Linux常用命令02
显示当前目录 pwd (print working directory) 显示当前目录 创建目录 mkdir (make directory) 创建目录(注意不是创建文 ...
- 理论经典:TCP协议的3次握手与4次挥手过程详解
1.前言 尽管TCP和UDP都使用相同的网络层(IP),TCP却向应用层提供与UDP完全不同的服务.TCP提供一种面向连接的.可靠的字节流服务. 面向连接意味着两个使用TCP的应用(通常是一个客户和一 ...