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,寻找峰值 寻找峰值 描述 笔记 ...
随机推荐
- 在ASP.NET Web API中使用OData的单例模式
从OData v4开始增加了对单例模式的支持,我们不用每次根据主键等来获取某个EDM,就像在C#中使用单例模式一样.实现方式大致需要两步: 1.在需要实现单例模式的导航属性上加上[Singleton] ...
- 在NDK C++线程中如何调用JAVA API
from://http://www.eoeandroid.com/thread-150995-1-1.html 在NDK中创建的线程中, 只允许调用静态的Java API. 当在线程中调用env-&g ...
- MEF在运行时替换插件
利用AppDomain的ShadowCopy特性. var setup = new AppDomainSetup { CachePath = cachePath, ShadowCopyFiles = ...
- MySql和相关驱动的安装方式
下载mySql for java驱动的地址:http://www.mysql.com/products/connector/ (可下可不下,因为安装mySql的时候就会包含了各种驱动) MySQL下载 ...
- .Net Core中文编码问题整理
1..Net Core Console控制台程序 在.Net Core中默认System.Text中不支持CodePagesEncodingProvider.Instance, System.Text ...
- Chapter 1 -- UsingAndAvoidingNull
"Null sucks." -Doug Lea "Null 很恶心!" "I call it my billion-dollar mistake.&q ...
- MVC详解(转)
原文链接:MVC详解 MVC与模板概念的理解 MVC(Model View Controller)模型-视图-控制器 MVC本来是存在于Deskt op程序中的,M是指数据模型,V是指用户界面,C ...
- C语言中将0到1000的浮点数用强制指针类型转换的方式生成一幅图像
搞过计算机图像的人都知道,图像中的每一个像素通常为一个整型数,它可以分成4个无符号的char类型,以表示其RGBA四个分量.一幅图像可以看做是一个二维整型数组.这里我会生成一个float数组,其数组大 ...
- 里诺全系列注册机+暗桩patch
一直有坛友私信更新里诺,今天花了一天时间,将里诺全系列更新完毕,权当送给坛友们的新年礼物吧! 全系列开放至元旦假期结束,后面就随机开放了. <ignore_js_op> 使用说明: 1.选 ...
- dcm4chee 修改默认(0002,0013) ImplementationVersionName
dcm4chee-2.17.3-psql\server\default\lib\dcm4che.jar ----org\dcm4che\Implementation.properties dcm4ch ...