https://oj.leetcode.com/problems/triangle/

一个三角形,类似于杨辉三角的形状,求从上到下最小的路径和,但走每一步都得是相邻的。

动态规划,从下到上一层层来。

sum[i] = min{sum[i]+triangle[i][j],sum[i+1]+triangle[i][j]}

有一个保存每一个点数值的过程。

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
int row = triangle.size();
if(row == )
return ;
if(row == )
return triangle[][]; //initialize
vector<int> sum;
sum.resize(row);
for(int i = ;i<triangle[row-].size();i++)
sum[i] = triangle[row-][i]; for(int i = row-;i>=;i--)
{
for(int j = ;j<=i;j++)
{
int min1 = sum[j]<sum[j+]?sum[j]:sum[j+];
sum[j] = min1 + triangle[i][j];
}
}
return sum[];
}
}; int main()
{
class Solution myS;
vector<int> v1;
v1.push_back(-);
vector<int> v2;
v2.push_back(-);
v2.push_back(-);
vector<int> v3;
v3.push_back();
v3.push_back();
v3.push_back();
vector<int> v4;
v4.push_back();
v4.push_back();
v4.push_back();
v4.push_back(); vector<vector<int> > num;
num.push_back(v1);
num.push_back(v2);
num.push_back(v3);
num.push_back(v4);
cout<<myS.minimumTotal(num); return ;
}

LeetCode OJ--Triangle **的更多相关文章

  1. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  8. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  9. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  10. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

  2. pandas关联mysql并读写数据库

    1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...

  3. linux :没有找到 ifconfig netstat

    linux :没有找到 ifconfig netstat ubuntu sudo apt install net-tools -y centos yum install net-tools

  4. linux 上安装配置l2tp的客户端

    有些时候我们外网linux服务器需要访问内网的服务器,这时候就需要在外网服务器上配置l2tp的客户端,连接到VPN访问内网服务器. 安装: yum -y install xl2tpd ppp 安装成功 ...

  5. 18.Yii2.0框架模型修改记录 和 修改点击量

    目录 修改数据 修改点击量 修改数据 上面要 use app\models\Article; //修改 //http://yii.com/?r=home/Edit public function ac ...

  6. 如何把list当成栈或者队列来用

    在python里,list和在C.java里的数组差不多,但是python里的list是可变长的,而且python语言也支持倒叙读取,如list[-1]可以读取最后一个元素.但这还不是最厉害的,lis ...

  7. BZOJ 4919: [Lydsy1706月赛]大根堆

    F[x][i]表示x的子树中取的数字<=i的最大值,线段树合并优化DP 写得很难看,并不知道好看的写法 #include<cstdio> #include<algorithm& ...

  8. loj2028 「SHOI2016」随机序列

    定义区间是内部只含有乘号的区间. 对于区间左端点是 \(l \geq 2\) 的情况,左端点前头是加号的情况和前头是减号的情况的个数是相同的.因此这些区间不对答案产生贡献. 所以区间左端点必定是 \( ...

  9. 模拟 - BZOJ 1510 [POI2006] Kra-The Disks

    BZOJ 1510 [POI2006] Kra-The Disks 描述 Johnny 在生日时收到了一件特殊的礼物,这件礼物由一个奇形怪状的管子和一些盘子组成. 这个管子是由许多不同直径的圆筒(直径 ...

  10. 大数据学习——akka自定义RPC

    实现 package cn.itcast.akka import akka.actor.{Actor, ActorSystem, Props} import akka.actor.Actor.Rece ...