120. Triangle
2016-05-19 16:08
176 查看
题目:
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
The minimum path sum from top to bottom is
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.
题意:
给定一个三角形,找到从顶端到低端最短的路径和。每一步只能移动到下一行的相邻格子中。
note:
加分点:只使用大小为n的额外内存空间来辅助,n的大小为三角形中的所有元素个数。
思路一:
DP问题,动态规划问题。除了三角形两边的各个元素之外,中间的各个元素都有上一层中相邻的两两个元素可供选择,按题意,两个元素中选择最小的。从第二层开始,三角形的两边都是直接累加的,中间的元素累加选择上一层中与该元素相邻的两个元素的最小值,最后求出的从顶端到低端每一条路径的最小值均存储在最后一层中。遍历循环结束后只需要找出最后一层的最小值即为三角形的最小值路径。
代码:8ms
思路二:
同样采用动态规划,从底向上轮训,这样就可以不用区分三角形的边与其他元素之间的差别,代码也更为精简。从倒数第二层到第一层,每个元素与下一层中与其相邻的两个元素中的最小值进行累加,轮训结束,三角形顶端的元素即为最小路径和。
代码:8ms
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.
题意:
给定一个三角形,找到从顶端到低端最短的路径和。每一步只能移动到下一行的相邻格子中。
note:
加分点:只使用大小为n的额外内存空间来辅助,n的大小为三角形中的所有元素个数。
思路一:
DP问题,动态规划问题。除了三角形两边的各个元素之外,中间的各个元素都有上一层中相邻的两两个元素可供选择,按题意,两个元素中选择最小的。从第二层开始,三角形的两边都是直接累加的,中间的元素累加选择上一层中与该元素相邻的两个元素的最小值,最后求出的从顶端到低端每一条路径的最小值均存储在最后一层中。遍历循环结束后只需要找出最后一层的最小值即为三角形的最小值路径。
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
代码:8ms
class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { int rowm = triangle.size(); //找出三角形层数 for(int i=1; i<rowm; i++){ //从第二层开始循环累加 int prem = triangle[i-1].size(); //找到当前层的上一层大小 int colm = triangle[i].size(); //当前层的大小 triangle[i][0] += triangle[i-1][0]; //三角形左边线的累加 triangle[i][colm-1] += triangle[i-1][prem-1]; //三角形右边线的累加 for(int j=1; j<colm-1; j++){ //三角形中间元素的累加 triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j]); //上一层与该元素相邻的两元素中的较小值与当前元素累加 } } int size = triangle[rowm-1].size(); //三角形最后一层大小 int minTotal = triangle[rowm-1][0]; //假定第一个元素是最小值 for(int i=1; i<size; i++){ //循环遍历三角形最底层,找到最小值即为所求最小值 minTotal = min(minTotal, triangle[rowm-1][i]); } return minTotal; } };
思路二:
同样采用动态规划,从底向上轮训,这样就可以不用区分三角形的边与其他元素之间的差别,代码也更为精简。从倒数第二层到第一层,每个元素与下一层中与其相邻的两个元素中的最小值进行累加,轮训结束,三角形顶端的元素即为最小路径和。
[ [2], [3,4], [6,5,7], [4,1,8,3] ]
代码:8ms
class Solution { public: int minimumTotal(vector<vector<int>>& triangle) { int rowm = triangle.size(); //找出三角形层数 for(int i=rowm-2; i>=0; i--){ //对每一层轮训 int colm = triangle[i].size(); //每一层的大小 for(int j=0; j<colm; j++){ triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]); //该层中每个元素与下一层中两个相邻子元素相累加。 } } return triangle[0][0]; } };
相关文章推荐
- iOS中assign、copy 、retain关键字
- VCS版本控制工具svn和git的小结
- sqlserver 分离和附加
- 自定义事务
- Android 多线程及线程通信
- FTP使用 ---python
- 惹怒程序员的十件事!需求变更居然不被排第一!
- nginx 下 bootstrap fa 字体异常问题
- LeetCode 274 H-Index
- POJ 2349 Arctic Network (最小生成树)
- post 请求 application/x-www-form-urlencoded。android 中文 乱码
- ff文字省略号
- 透视校正插值(Perspective-Correct Interpolation)
- Win7系统启动失败出现错误提示0XC000000F的解决方法
- 常用sql语句记录--sqlserver
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- 软件质量
- openstack-ansible Chapter 4. Deployment configuration
- mac 终端下的命令大全
- 超图对接NSDTF-DEM数据