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,寻找峰值 寻找峰值 描述 笔记 ...
随机推荐
- lodash用法系列(2),处理对象
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- 【k8s】基础概念 + 工作原理
工作原理: 原理图 工作原理描述: 1>用户通过kubectl或者API server的REST API接口,提交需要运行的docker容器(创建pod请求): 2>api server将 ...
- HDU 4888 Redraw Beautiful Drawings 网络流 建图
题意: 给定n, m, k 以下n个整数 a[n] 以下m个整数 b[n] 用数字[0,k]构造一个n*m的矩阵 若有唯一解则输出这个矩阵.若有多解输出Not Unique,若无解输出Impossib ...
- jQuery Ajax 上传文件夹及文件
我们先来看一下文件夹结构 这是上传处理的: 看一下系统日志: 升级 HTML5文件实现拖拽上传提示效果改进(支持三种状态提示) 拖拽过程详解: 1:文件未拖出文件选择框的时候提示:将要上传的文件或文件 ...
- POST 和 PUT 方法区别
Http定义了与 服务器的交互方法,其中除了一般我们用的最多的GET,POST 其实还有PUT和DELETE 根据RFC2616标准(现行的HTTP/1.1)其实还有OPTIONS,GET,H ...
- go 用的不多的命令
8.go doc 文档注释相关,可以搭建本地GO文档服务器,包含自己的项目注释,更多细节请参考:https://github.com/hyper-carrot/go_command_tutorial/ ...
- mybatis大于号,小于号,去地址符,单引号,双引号转义说明
在mybatis中,使用到大于号,小于号,与在SQL编辑器中是不一样的. SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DAT ...
- window.opener()方法
<!DOCTYPE html><html><head><meta charset="GBK"><title>菜鸟教程(r ...
- [Web 前端 ] ES6 == ES 2015
cp from : https://www.cnblogs.com/ricoliu/p/5996149.html 遇到了一个困惑 原来称作es6的现在突然变成es2015 了 原因是这个事ecma ...
- 100base-T
100Base-T是一种以100Mbps速率工作的局域网(LAN)标准,它通常被称为快速以太网标准,并使用两对UTP(非屏蔽双绞线)铜质电缆. 快速以太网 : 与10BASE-T的区别在于网络速率是1 ...