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,寻找峰值 寻找峰值 描述 笔记 ...
随机推荐
- WMAppManifest.xml
大家在编写Windows phone的程序的时候可能并没有关注WMAppManifest.xml,其实这个档案是记录了应用程式的相关属性描述,以及定义应用程式的功能性的..所以还是相当重要的一个文档, ...
- 线程池框架executor
Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...
- UIWindow 介绍:概述、作用、主要属性及方法
概述: UIWindow 类是 UIView 的子类,用于管理.协调应用中显示的窗口,其两个最重要的职能就是 容器,给 view 提供展示的区域: 将事件(例如:点击事件.拖拉事件等)分发给 view ...
- 网易游戏2015年暑期实习生面试经历-游戏研发project师
首先,我还是先介绍一下网易游戏吧.引用别人的一段话 作者:王选易.出处: http://www.cnblogs.com/neverdie/ 欢迎转载 .也请保留这段声明.假设你喜欢这篇文章,请点[推荐 ...
- python测试开发django-25.表单提交之post注册案例
前言 一个网站上新用户注册,会写个注册页面,如果用django写个注册页面的流程呢? 本篇以post请求示例,从html页面上输入用户注册信息,提交到后台处理数据,然后传参数据到User数据库表里面 ...
- Base64 JAVA后台编码与JS前台解码(解决中文乱码问题)
中文通过Java后台进行Base64编码后传到前台,通过JS进行Base64解码时会出现中文乱码的问题,被这个问题也是困扰了几天,使用jquery.base64.js只能转码非中文字符,经过搜集各种方 ...
- mysql查询字段时实现左右补零
右补0:select RPAD(id,8,'0') as pad from tmp; 左补0:select LPAD(id,8,'0') as pad from tmp;
- Android性能检测工具——traceview
之前的几篇文章中介绍了android中常用的一些工具,今天介绍的工具也是比较实用和方便的,它可以用量化的指标告诉我们哪个方法执行的时间最长,被调用的次数最多,有没有重复调用.下面我们就来看看它是怎么为 ...
- 让java从Mysql返回多个ResultSet
首先,JDBC对于SQLSERVER来说默认是支持返回,但对于MySql来说,只默认支持存储过程返回多个ResultSet,那对于手写SQL怎么办. 其实很简单,只要一个在连接字符串中加一个参数:al ...
- 修改VIM恶心的注释自动格式化
我复制一段代码,里面有// 只有一行,但是复制到vim中就下面全部都有//了.我要取消这个功能 vim ~/.vimrc set nu! "set formatoptions=croql 这 ...