LeetCode120 Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.(Medium)
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
分析:
经典的动态规划题,可以有自顶向下,自底向上的解法,是自己学动态规划的第一道题,就不分析了,写一个自底向上的解法。
代码:
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int sz = triangle.size();
if (sz == ) {
return ;
}
int dp[sz][sz];
for (int i = ; i < sz; ++i) {
dp[sz - ][i] = triangle[sz - ][i];
}
for (int i = sz - ; i >= ; --i) {
for (int j = ; j <= i; ++j) {
dp[i][j] = min(dp[i + ][j], dp[i + ][j + ]) + triangle[i][j];
}
}
return dp[][];
}
};
LeetCode120 Triangle的更多相关文章
- LeetCode120——Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- Leetcode120.Triangle三角形最小路径和
给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和为 11 ...
- [Swift]LeetCode120. 三角形最小路径和 | Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [LeetCode] Pascal's Triangle II 杨辉三角之二
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
随机推荐
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- dl, dt, dd /line-height /loat /vertical-align 问题
刚刚在看张鑫旭大神的个人网站,看到一篇关于“css瓶颈”的深度好文,地址为:http://www.zhangxinxu.com/wordpress/?p=2523 关于张大神在文章里面提到的四个问题: ...
- 9.2专项测试-Android性能测试黑盒分析-1
1. 专项测试 业务测试:面向新需求 回归测试:面向已交付需求 专项测试:面向非功能需求的各类质量唯独特征 表现 用户维度 技术维度 崩溃 crash,弱网 检测崩溃1.某个页面,因为研发处理不合适, ...
- JAVA面试常见问题之设计模式篇
1.常见的设计模式 单例模式.工厂模式.建造模式.观察者模式.适配器模式.代理模式.装饰模式. 参考:https://www.cnblogs.com/cr330326/p/5627658.html 2 ...
- sql作业启停服务器
IF EXISTS(SELECT * FROM msdb.dbo.sysjobs WHERE name='启用pubs数据库') EXEC msdb.dbo.sp_delete_job @job_na ...
- 几个树形dp
1.重建道路 树形dp基础题,f[i][j]表示在i这个点我和我的子树联通块大小为j最少砍几条边. 转移的时候,到下一个子树时上一个子树所有答案先++(此树直接砍掉不贡献答案),再继续dp. 注意更新 ...
- [转]json对象详解
json(javascript object notation)全称是javascript对象表示法,它是一种数据交换的文本格式,而不是一种编程语言,用于读取结构化数据.2001年由Douglas C ...
- 备忘 ubuntu ip 及 dns 的坑
以前都用 ubuntu 16.04 现在用 18.04 遇到几个恶心的事,现在解决了,记录下来. 1. 设置 DNS , DNS 设置老是不对,最后发现问题老版本 ubuntu 17.10以下 ...
- java list转换json格式
/** * 处理返回值(转换json格式和补零) * * @param resultDto5List * @param dateList * @return */ private JSONObject ...
- 【洛谷】P1880 石子合并
P1880 石子合并 题目描述 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计 ...