[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 ...
随机推荐
- 「个人训练」Radar Installation(POJ-1328)
这条题目A了十次...emmmmm 其实不难就是一个贪心.... 先说下算法(之前的和现在的) 之前考虑的其实很简单.用平面几何即可将雷达可以放置的区域转化为区间(顺便判断是否无解.问题就比较简单了: ...
- result returns more than one elements此种错误,解决
场景:公司产品开发完成后,接入第三方厂商,在进行接口联调的时候出现此问题.此接口报文中的每一个数据都要进行校验,有些是与已经存入产品数据库中的数据进行对比,看是否存在. 问题:在测试中,有些测试没有问 ...
- AcCoder Contest-115 D - Christmas
D - Christmas Time limit : 2sec / Memory limit : 1024MB Score : 400 points Problem Statement In some ...
- 使用HashOperations操作redis
方法 c参数 s说明 Long delete(H key, Object... hashKeys); H key:集合key Object... hashKeys:key对应hashkey 删除ma ...
- javascript实现自动切换焦点功能学习
当用户在表单中填写完当前字段后,能否自动将焦点跳转到下一个字段以方便用户输入? 为了增强易用性,加快数据输入的速度,可以在前一个文本框中的字符达到一定的设置的字符长度后(比如电话号码,身份证号等),用 ...
- HDU 2175 汉诺塔IX
http://acm.hdu.edu.cn/showproblem.php?pid=2175 Problem Description 1,2,...,n表示n个盘子.数字大盘子就大.n个盘子放在第1根 ...
- CSS设计指南之ID属性
1.用于页内导航的ID ID也可以用在页内导航连接中.下面就是一个链接,其目标是同一页的另一个位置. <a href="#bio">Biography</a> ...
- BZOJ4524 CQOI2016伪光滑数(堆)
对于每个质数求出其作为最大质因子时最多能有几个质因子,开始时将这些ak1~akmaxk扔进堆.考虑构造方案,使得每次取出最大值后,最大质因子.质因子数均与其相同且恰好比它小的数都在堆里.类似暴搜,对于 ...
- P4109 [HEOI2015]定价
题目描述 在市场上有很多商品的定价类似于 999 元.4999 元.8999 元这样.它们和 1000 元.5000 元和 9000 元并没有什么本质区别,但是在心理学上会让人感觉便宜很多,因此也是商 ...
- 12.25模拟赛T2
https://www.luogu.org/blog/a23333/post-xing-xuan-mu-ni-sai-path-ji-wang-zui-duan-lu 如果设f[i]表示从i到n的期望 ...