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
[
[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.
这个问题分为两种情况,一种不可以修改原数组元素,另一种可以修改数组元素
1、可以修改数组元素
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
for(int i = triangle.size()-; i >= ; --i)
{
for(int j = ; j < i+;++j)
{
if(triangle[i+][j]<triangle[i+][j+])
triangle[i][j] += triangle[i+][j];
else
triangle[i][j] += triangle[i+][j+];
} }
return triangle[][];
}
};
2、不能修改数组元素
LeetCode -- Triangle 路径求最小和( 动态规划问题)的更多相关文章
- [LeetCode] Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- hiho 第116周,最大流最小割定理,求最小割集S,T
小Hi:在上一周的Hiho一下中我们初步讲解了网络流的概念以及常规解法,小Ho你还记得内容么? 小Ho:我记得!网络流就是给定了一张图G=(V,E),以及源点s和汇点t.每一条边e(u,v)具有容量c ...
- [LeetCode] Smallest Good Base 最小的好基数
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- [leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...
- POJ 2253 Frogger【最短路变形——路径上最小的最大权】
链接: http://poj.org/problem?id=2253 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- poj1797(dijstra变形,求最小边的最大值)
题目链接:https://vjudge.net/problem/POJ-1797 题意:n个点,m条带权边,求点1到点n的所有路径中最小边的最大值. 思路: 和poj2253一样,只不过那题n< ...
- Leetcode 不同路径系列
Leetcode不同路径系列题解笔记 62. 不同路径 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 "Start" ). 机器人每次只能向下或者向右移动一 ...
随机推荐
- cocos2d 中判断CGPoint或者CGSize是否相等
cocos2d 中判断CGPoint是否相等 调用CGPointEqualToPoint(point1, point2) 判断CGSize是否相等 调用CGSizeEqualToSize(size1, ...
- TYVJ P1090 母舰 Label:模拟,题目看清就好
背景 广东汕头聿怀初中 Train#3 Problem 1 描述 在小A的星际大战游戏中,一艘强力的母舰往往决定了一场战争的胜负.一艘母舰的攻击力是普通的MA(Mobile Armor)无法比较的.对 ...
- Mybatis_mybatis常用jdbcType数据类型
MyBatis 通过包含的jdbcType类型 BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED ...
- The constructor BASE64Encoder() is not accessible due to restriction on required
在Eclipse中编写Java代码时,用到了BASE64Decoder,import sun.misc.BASE64Decoder;可是Eclipse提示: Access restriction : ...
- Eclipse @override报错解决
第一种解决方案: @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...
- js的url中传递中文参数乱码,如何获取url中参数问题
一:Js的Url中传递中文参数乱码问题,重点:encodeURI编码,decodeURI解码: 1.传参页面Javascript代码: <script type=”text/javascript ...
- cvSave in VS2010 or Linux
cvSave这个函数是OpenCV中用来保存某个数据类型到文件中常用的函数,它原本共有五个参数,但是在VS2010中只需要填前两个,而在Linux必须填满五个,否则会出错,如下: // VS2010 ...
- Tesla-> Fermi (550Ti) -> Kepler(680) -> Maxwell (750Ti) -> Volta(was Pascal)
Pascal GPU Pascal (from French mathematician Blaise Pascal) is Maxwell successor. In this news, we l ...
- Tortoise SVN 不显示 Log Message 具体信息的解决方法
今天加入新项目,在 Tortoise SVN Check out 完项目之后,发现右键 show log 不显示 Log Message 的具体信息: 因为是新加入的项目,问了原来负责这个项目的同事, ...
- Hibernate + proxool 连接数超过最大允许连接数
主要原因是操作完成没有释放连接,在Hibernate中增加设定 <prop key="hibernate.connection.release_mode">after_ ...