LeetCode OJ--Triangle **
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 **的更多相关文章
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- MySQL 如何将Id相同的字段合并,并且以逗号隔开
数据库存的数据 sql: SELECT Id,GROUP_CONCAT(`Name` SEPARATOR ',') NAMES FROM `stu` GROUP BY Id;
- Voyager的Roles和Pemissions
以Page为例讲解: 取消admin的roles下Pages的Browse Pages权限: 打开web.php文件,添加: Route::get('pages', function(){ retur ...
- Leetcode6--->Zigzag Conversion(将给定字符串按照Z字排列,输出结果)
题目:给定一个字符串s,一个整数numRows, 将字符串s按照竖Z的方式排列,然后输出结果: 举例:String s = "PAYPALISHIRING"; 排列后为: P A ...
- python学习-- django 2.1.7 ajax 请求
#--------------views.py---------------------- def add(request): a = request.GET['a'] print(a) b = re ...
- PHP 教父鸟哥 Yar 的原理分析
模块越来越多,业务越来越复杂,RPC 就上场了,在 PHP 的世界里,鸟哥的作品一直备受广大网友的青睐.下面一起学习下鸟哥的 PRC 框架 Yar . 揭开 Yar 神秘面纱 RPC 采用客户端/服务 ...
- python自动安装python2.7
#coding = utf-8 import os import sys if(os.getuid() == 0): pass else: print ("you are not root ...
- 【bzoj4247】挂饰 背包dp
题目描述 JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直接挂在手机上, ...
- 【Luogu】P2536病毒检测(Trie上DP)
题目链接 这道题我写了个01DP,f[i][j]表示跑到Trie上第i个节点,匹配到字符串第j位行不行 然后重点在*号无限匹配怎么处理 经过一番脑洞我们可以发现*号无限匹配可以拆成两种情况: 1:匹配 ...
- kb-07线段树-05-区间整体修改查询;(水)
/* */ #include<iostream> #include<cstring> #include<cstdio> using namespace std; s ...
- iOS-OAuth认证
OAuth授权 OAuth授权分四步: 第一步,应用向服务提供方申请请求令牌(Request Token),服务提供方验证通过后将令牌返回.这个步骤由于涉及到应用帐号密码,在应用的服务端发起,所以这个 ...