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. 解决mysql出现的问题#1055 - Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this i

    最近在学flask, 在访问主页时,一直出现1055错误,在网上找的解决方法是删除ONLY_FULL_GROUP_BY,当时是删除了,但是退出在进行select @@sql_mode时,仍出现ONLY ...

  2. linux文件属性文文件类型知识

    文件类型分别介绍: 1.普通文件:我们通过用ls  -l来查看xxx.sql的属性,可以看到第一列内容为-rw-r--r--,值得注意的是第一个符号是-(英文字符减号),在Linux中,以这样的字符开 ...

  3. laravel5 使用try catch

    在laravel5中使用以下代码并没有捕获异常 try{ var_dump($val); }catch (Exception $e){ var_dump($e); }  Laravel 5 时代控制器 ...

  4. windows server2008R2 64位 配置 mysql-8.0.15-winx64

    下载mysql: 1 https://dev.mysql.com/downloads/mysql/ 如图: 然后在解压的文件夹里面新建 my.ini文件,内容如下 按 Ctrl+C 复制代码 按 Ct ...

  5. Kafka 基础实战 :消费者和生产者实例

    学习地址: http://www.jikexueyuan.com/course/2036.html

  6. Django ORM (三) 查询,删除,更新操作

    ORM 查询操作 修改 views.py 文件 from django.shortcuts import render, HttpResponse from app01 import models f ...

  7. poj 2531 分权问题 dfs算法

    题意:一个集合(矩阵) m[i][j]=m[j][i]权值,分成两个集合,使其权值最大.注:在同一个集合中权值只能算一个. 思路:dfs 假设都在集合0 遍历 id 的时候拿到集合1 如果与 id 相 ...

  8. Linux学习-用 make 进行宏编译

    为什么要用 make 先来想象一个案例,假设我的执行档里面包含了四个原始码文件,分别是 main.c haha.c sin_value.c cos_value.c 这四个文件,这四个文件的目的是: m ...

  9. LayaAir环境TypeScript

    1.1 Node.js下载 ​ TypeScript开发HTML5需要Node.js环境,如果没有安装过的,请前往官方下载(建议LTS版本),如图1,URL地址为:https://nodejs.org ...

  10. day01_06.比较运算符

    >  >=  =  <  <=  != == ===  !== 凡运算,必有运算结果,比较运算符的运算结果是布尔值 ==和===的区别 <?php $c = ( 3 == ...