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 ...
随机推荐
- 前端插件--swiper.js
使用swiper.js还要注意引入它的同时也要引入swiper.css样式文件: swiper官方文档:http://www.swiper.com.cn/api/effects/193.html 实例 ...
- JPinyin繁体相互转换
// 用正则表达式"[\u4e00-\u9fa5]"匹配 字符串 Scanner sc =new Scanner(System.in);System.out.println(&qu ...
- jeecms 强大的采集功能优化 转载 https://blog.csdn.net/jeff06143132/article/details/7099003
========================================================= 没办法附件上传不了,AcquisitionSvcImpl.java类: //---- ...
- Caused by: java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
学习htmlutil发现报错 完整的引入 <!-- 引入htmlunit --> <dependency> <groupId>net.sourceforge.htm ...
- ubuntu 12.4,搞定apt源
http://wiki.ubuntu.org.cn/Template:12.04source deb http://cn.archive.ubuntu.com/ubuntu/ precise main ...
- drf模块及源码
drf中的APIView请求生命周期 APIView的as_view(局部禁用csrf) => 调用父类view中的as_view返回view()方法 => 自己的类调用自己的dispat ...
- python基础--函数的命名空间and作用域
函数对象:函数是第一类对象,函数名指向的值是可以被当作参数进行传递的 1.函数名可以被传递 2.函数名可以被当作参数传递给其它函数 3.函数名可以被当作函数的返回值 4.函数名可以被当作容器类型的参数 ...
- JS 获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...
- Hdu 1025(LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- LintCode 删除排序链表中的重复元素
给定一个排序链表,删除所有重复的元素每个元素只留下一个. 样例 给出 1->1->2->null,返回 1->2->null 给出 1->1->2->3 ...