[Leetcode Week8]Triangle
Triangle 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/triangle/description/
Description
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).
Solution
class Solution {
public:
int min(int a, int b) {
return a < b ? a : b;
}
int minimumTotal(vector< vector<int> >& triangle) {
int size = triangle.size();
if (size == 0)
return 0;
if (size == 1)
return triangle[0][0];
int** result = new int*[size];
int i, j;
for (i = 0; i < size; i++)
result[i] = new int[size];
for (i = 0; i < size; i++)
result[size - 1][i] = triangle[size - 1][i];
for (i = size - 2; i >= 0; i--) {
for (j = 0; j <= i; j++) {
result[i][j] = min(result[i + 1][j], result[i + 1][j + 1]) + triangle[i][j];
}
}
j = result[0][0];
for (i = 0; i < size; i++)
delete [] result[i];
delete [] result;
return j;
}
};
解题描述
这道题是典型的动态规划问题。从最底层开始向上推导,每一步都是求当前的点应该选择什么后续路径才能保证最终的路径权值之和最小。上面是我最开始的解答,时间复杂度为O(n2),空间复杂度为O(n2)。后面重新想了一下,发现其实记录后续路径之和只需要用一维数组就可以了,于是加以修改得到空间复杂度为O(n)的新解:
class Solution {
public:
int min(int a, int b) {
return a < b ? a : b;
}
int minimumTotal(vector< vector<int> >& triangle) {
int size = triangle.size();
if (size == 0)
return 0;
if (size == 1)
return triangle[0][0];
int *result = new int[size];
int i, j;
for (i = 0; i < size; i++)
result[i] = triangle[size - 1][i];
for (i = size - 2; i >= 0; i--) {
for (j = 0; j <= i; j++)
result[j] = min(result[j], result[j + 1]) + triangle[i][j];
}
j = result[0];
delete [] result;
return j;
}
};
[Leetcode Week8]Triangle的更多相关文章
- LeetCode 120. Triangle (三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- [LeetCode] Largest Triangle Area 最大的三角区域
You have a list of points in the plane. Return the area of the largest triangle that can be formed b ...
- LeetCode Valid Triangle Number
原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...
- 【leetcode】Triangle (#120)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode][Java]Triangle@LeetCode
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 ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- python发起请求提示UnicodeEncodeError
具体错误: UnicodeEncodeError: 'latin-1' codec can't encode characters in position 73-74: Body ('测试') is ...
- JQuery Ajax执行过程AOP拦截
JQuery Ajax过程AOP:用于在Ajax请求发送过程中执行必备操作,比如加载数据访问令牌. $.ajaxSetup({ type: "POST", error: funct ...
- HDU 4782 Beautiful Soup (模拟+注意细节)
思路就是用栈模拟,不用开实体的栈,直接记一个top指针就行. 说说这题的细节: 1.tag标签里的内容不要动,原样输出.比如<p aa bb cc>,就这样输出就行,不要删空格.题目中说了 ...
- STL应用——hdu1412(set)
set函数的应用 超级水题 #include <iostream> #include <cstdio> #include <algorithm> #include ...
- Android插件化框架
1. dynamic-load-apk/DL动态加载框架 是基于代理的方式实现插件框架,对 App 的表层做了处理,通过在 Manifest 中注册代理组件,当启动插件组件时,首先启动一个代理组件 ...
- mysql原理以及相关优化
说起MySQL的查询优化,相信大家积累一堆技巧:不能使用SELECT *.不使用NULL字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解其背后的工作原理?在 ...
- 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治
题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...
- P3457 [POI2007]POW-The Flood
题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是否是该市的一个组成部 ...
- something about Parameter Estimation (参数估计)
点估计 Point Estimation 最大似然估计(Maximum Likelihood Estimate —— MLE):视θ为固定的参数,假设存在一个最佳的参数(或参数的真实值是存在的),目的 ...
- 【题解】NOIP2016换教室
哇好开心啊!写的时候真的全然对于这个加法没有把握,但还是大着胆子试着写了一下——竟然过了样例?于是又调了一下就过啦. 不过想想也觉得是正确的吧,互相独立的事件对于期望的影响自然也是相互独立的,可以把所 ...