[leetcode.com]算法题目 - 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.
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
}
};
答题模板
思路:假设三角形共有n行,题目中看出第i行共有i个元素。从top到bottom,我们只考虑minimum path的最后一步是停在了这n个元素的哪一个上面。用数组min_sum(k)表示最后一行到达第k个元素的最小路径(min_sum总长度为n),然后找出min_sum(k)中最小的元素即可。注意一下自上而下递推min_sum时候的递推公式,代码如下:
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = triangle.size();
if ( == n) return ;
if ( == n) return triangle[][];
int *min_sum = new int[n];
for(int i=; i<n;i++)
min_sum[i] = ;
min_sum[]=triangle[][];
for(int i=;i<n;i++){
for(int j=triangle[i].size()-; j>=;j--){
if (==j){
min_sum[j] += triangle[i][];
}else if (triangle[i].size()-==j){
min_sum[j] = min_sum[j-]+triangle[i][j];
}else{
min_sum[j] = min(min_sum[j-], min_sum[j])+triangle[i][j];
}
}
}
int minTotal = min_sum[];
for(int i=;i<n;i++){
minTotal = min(minTotal, min_sum[i]);
}
delete[] min_sum;
return minTotal;
}
int min(int a, int b){
return (a>b?b:a);
}
};
Answer
注意:一开始的时候内部j那个循环是正着写,后来发现这样有个问题,就是有可能会使用已经变动的过的值去更新下一列。
[leetcode.com]算法题目 - Triangle的更多相关文章
- [leetcode.com]算法题目 - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. class Solution { public: int sqr ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- mybatis不报错,但是查询结果为0
[转载]https://blog.csdn.net/shenzhenNBA/article/details/46673327 在用MyBatis操作数据库的时候相信很多人都用到,当在判断null, 大 ...
- 拖拽文件实现无刷新上传,支持2G文件
客户端 用HTML5:jQuery File Upload http://blueimp.github.io/jQuery-File-Upload/basic-plus.html API https: ...
- springMVC学习 六 跳转方式
SpringMVC的controller中的方法执行完之后,默认的跳转方式是请求转发 如果想要修改跳转方式,可以设置返回值字符串内容(1) 添加 redirect:资源路径 重定向 "red ...
- Find the location of libmysqlclient.so.X file in Linux environments
I'm putting together a script that has a requirement of knowing libmysqlclient.so.[15|16|18] .so fil ...
- delphi 中如何处理“幽灵”、“熔断”?(转载)
原始连接:http://dannywind.nl/delphi/meltdown-spectre-and-delphi/ Meltdown, Spectre and Delphi Don’t pani ...
- Vue.directive基础,在Vue模块开发中使用
这是从网上找到的一个案例,由于网上的案例有坑,所以我在这里从新上传一次! 首先在main.js里引入两个自定义指令 import {focus, drag} from './components/da ...
- [转]urllib模块urlretrieve方法
直接将远程数据下载到本地 info: urllib.urlretrieve(url[, filename[, reporthook[, data]]])参数说明:url:外部或者本地urlfilena ...
- ORACLE rollup函数
rollup函数应用场景: 主要使用在 分组中,将每个分组求汇总值(就是小计),最后再讲所有值(除去小计)求和(就是合计) 当然,使用union 也可以达到同样的效果.先将需要查询的分组查出来,再un ...
- 微信小程序设置全局字体
微信小程序设置全局css,需要在app.wxss文件中设置page的样式 page { font-family:"PingFangSC-Thin"; font-size:32rpx ...
- maven学习之一:maven安装
1.下载maven: 地址:https://maven.apache.org/ 点击download后; 解压: 解压后放在d:\develop目录下 下面我们去配置环境变量,找到计算机,右键,找到属 ...