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 ...
随机推荐
- BZOJ-3679(数位DP)
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a,b; int k[20]; ll dp[2 ...
- Vim编辑器基础
Vim编辑器基础 vi:Visual Interface vim:VI iMproved Vim模式 1.编辑模式(命令模式) 只能下达命令,不能键入字符 2.输入模式 键入字符 3.末行模式 左下角 ...
- POJ:2411-Mondriaan's Dream(矩形拼接方案)
题目链接:http://poj.org/problem?id=2411 解题心得: 可以说是很经典的一个状压dp了,写dfs遍历肯定是要超时的,这个题的状态转移方程对新手来说有点吃力. 状态转移用的是 ...
- Githun&HEXO建站小记
title: 建站小记 date: 2018-03-04 11:10:54 updated: 2018-03-06 12:00:00 tags: [hexo,next,建站,学习,前端技术,折腾,博客 ...
- LINQ to SQL和Entity Framework对比与关联
LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术.他们之间的本质区别在于EF对数据库架构和我们查询的类型实行了更好的解耦.使用EF,我们查询的对象不再 ...
- xcode8.1 autolayout 找不到 Update Frames 按钮
- LR采用的Sigmoid函数与最大熵(ME) 的关系
LR采用的Sigmoid函数与最大熵(ME) 的关系 从ME到LR 先直接给出最大熵模型的一般形式,后面再给出具体的推导过程. \[\begin{align*} P_w(y|x) &= \df ...
- Get Sauce(状压DP)
描述 In order to celebrate the 8th anniversary of ZOJ, LCLL goes to a sauce factory to "Get Sauce ...
- ASP.NET配置设置-关于web.config各节点的讲解
在msdn中搜索:“ASP.NET配置设置”,可以查看各个节点的配置. httpRuntime 元素:配置 ASP.NET HTTP 运行时设置,以确定如何处理对 ASP.NET 应用程序的请求.
- POJ——3070Fibonacci(矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12329 Accepted: 8748 Descri ...