LeetCode之“动态规划”: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.
For example, given the following triangle
[
[],
[,],
[,,],
[,,,]
]
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 rows = triangle.size();
if(rows == )
return ;
int * dp = new int[rows];
int szOfLastRow = triangle[rows - ].size();
for(int i = ; i < szOfLastRow; i++)
dp[i] = triangle[rows - ][i];
for(int i = rows - ; i > -; i--)
{
int cols = triangle[i].size();
for(int j = ; j < cols; j++)
dp[j] = triangle[i][j] + min(dp[j], dp[j + ]);
}
return dp[];
}
};
这个程序中最核心的地方在:
dp[j] = triangle[i][j] + min(dp[j], dp[j + ]);
可以用图表示如下:

其中一个例子就是:

需要注意的就是这个例子中只改变的是dp[0],dp[1]并没有改变。
LeetCode之“动态规划”:Triangle的更多相关文章
- leetcode面试准备:Triangle
leetcode面试准备:Triangle 1 题目 Given a triangle, find the minimum path sum from top to bottom. Each step ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- leetcode笔记 动态规划在字符串匹配中的应用
目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)
Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
随机推荐
- android:shape属性详解
这一类的shape定义在xml中 file location: res/drawable/filename.xml The filename is used as the resource ID.(这 ...
- Microsoft Dynamics CRM2011 更换Logo
之前操作过但没做过记录,这里记录下以防以后有需要时记不起来还有迹可循 IE收藏栏的图标,在网站根目录下的的/favicon.ico CRM网页中的Logo,替换/_imgs/crmmastheadlo ...
- 【一天一道LeetCode】#223. Rectangle Area
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Find th ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 2015-2016机器人操作系统(ROS)及其应用暑期学校资料汇总 ROS Summer School 持续更新
综合信息:2015 2016 课程资料:2015 2016 其他重要机器人.ROS相关学习活动 知乎关于ROS的话题 1 ROS的开发流程?http://www.zhihu.com/qu ...
- 文件夹或者文件比对工具 Beyond Compare
文件夹或者文件比对工具 Beyond Compare 之前有同事离职了. 没有工作交接. 同事的代码有一部分也没有提交版本库. 结果就是线上的代码和版本库中的文件数 ...
- (国内)完美下载Android源码Ubuntu版
今天写的文章莫名奇妙的没了,所以再重新写一篇. 首先,为了方便起见,我已经将系统更换成里Ubuntu,因为官方推荐使用这个Linux发行版.先来一张系统的截图: Ubuntu的版本是16.04(推荐用 ...
- 【OpenCV文档】用于角点检测的Fast算法
原文地址:http://docs.opencv.org/trunk/doc/py_tutorials/py_feature2d/py_fast/py_fast.html#fast-algorithm- ...
- ROS_Kinetic_01 在ubuntu 16.04安装ROS Kinetic 2017.01更新
ROS_Kinetic系列学习(一),在ubuntu 16.04安装ROS Kinetic. Celebrating 9 Years of ROS! ubuntu16.04已经发布半年多了,ROS的K ...
- 【Unity Shaders】Reflecting Your World —— Unity3D中简单的Cubemap反射
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...