LintCode: Triangle
C++
逆推
class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int> > &triangle) {
// write your code here
if (triangle.size() == )
return ;
vector<int> dp(triangle.size());
dp[] = triangle[][];
for(int i = ; i < triangle.size(); i++)
for(int j = triangle[i].size() - ; j >= ; j--)
if (j == )
dp[j] = dp[j] + triangle[i][j];
else if (j == triangle[i].size() - )
dp[j] = dp[j-] + triangle[i][j];
else
dp[j] = min(dp[j-], dp[j]) + triangle[i][j];
int ret = INT_MAX;
for(int i = ; i < dp.size(); i++)
ret = min(ret, dp[i]);
return ret;
}
};
C++,修改原数组
class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int> > &triangle) {
// write your code here
for (int i = triangle.size() - ; i >= ; --i){
for (int j = ; j < i + ; ++j){
if(triangle[i+][j] > triangle[i+][j+]){
triangle[i][j] += triangle[i+][j+];
}else{
triangle[i][j] += triangle[i+][j];
}
}
}
return triangle[][];
}
};
LintCode: Triangle的更多相关文章
- LintCode "Triangle Count"
Should be "Medium" or even "Easy".. Just with a little Greedy. class Solution { ...
- 【Lintcode】382.Triangle Count
题目: Given an array of integers, how many three numbers can be found in the array, so that we can bui ...
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
随机推荐
- Cannot subclass final class class com.sun.proxy.$Proxy
背景 这个错误是我在使用AOP动态切换数据库,实现数据库的读写分离的时候出现的问题,使用到的系统环境是: <spring.version>3.2.6.RELEASE</spring. ...
- 浴血黑帮第一季/全集Peaky Blinders迅雷下载
本季第一季Peaky Blinders Season 1 (2013)看点:<浴血黑帮>Peaky Blinders是从战后伯明翰地区走出的一个传奇黑帮家族,时间要追溯到1919年,家族成 ...
- 详解UILabel的adjustsFontSizeToFitWidth值
详解UILabel的adjustsFontSizeToFitWidth值 UILabel有一个属性值,叫adjustsFontSizeToFitWidth,看着名字就知道,他是用来让文字自动适应UIL ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
- .Net Core HTML解析利器之HtmlAgilityPack
一 .HtmlAgilityPack简介 这是一个敏捷的HTML解析器,它构建了一个读/写DOM,并支持简单的XPATH或XSLT(实际上,你实际上并不了解XPATH和XSLT来使用它,不必担心).它 ...
- Caffe SSD的resize过程解析
问题描述在windows平台上,本地训练SSD_512得到了对应的权值参数文件,加载模型进行前向测试的时候,发现调用caffe.io.Transformer中的resize处理函数速度太慢,打算用op ...
- Chapter 1 -- UsingAndAvoidingNull
"Null sucks." -Doug Lea "Null 很恶心!" "I call it my billion-dollar mistake.&q ...
- 请简单介绍一下什么是Spring?
Spring的核心是一个轻量级(Lightweight)的容器(Container),它是实现IoC(Inversion of Control)容器和非入侵性(No intrusive)的框架,并提供 ...
- [转]QT QDateTime类、QTimer类
QDateTime类,头文件#include <QDateTime> 可以使用QDateTime类来获得系统时间.通过QDateTime::currentDateTime()来获取本地系统 ...
- 怎么选择软件许可证,Apache, MIT, BSD, GPL, Mozilla, LGPL