leetcode第一刷_Minimum Path Sum
能够用递归简洁的写出,可是会超时。
dp嘛。这个问题须要从后往前算,最右下角的小规模是已知的,边界也非常明显,是最后一行和最后一列,行走方向的限制决定了这些位置的走法是唯一的,能够先算出来。然后不断的往前推算。
用distance[i][j]保存从当前位置走到最右下角所需的最短距离,状态转移方程是从distance[i+1][j]和distance[i][j+1]中选一个小的,然后再加上自身的。
代码非常easy理解,这就是dp的魅力。空间上是能够优化的,由于当前状态仅仅与后一行和后一列有关系。
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int erow = grid.size();
if(erow<=0) return 0;
int ecolumn = grid[0].size();
if(ecolumn<=0) return 0;
if(erow==1&&ecolumn==1) return grid[0][0];
vector<int> tpres(ecolumn, 0);
vector<vector<int> > distance(erow, tpres);
distance[erow-1][ecolumn-1] = grid[erow-1][ecolumn-1];
for(int i=erow-2;i>=0;i--)
distance[i][ecolumn-1] = distance[i+1][ecolumn-1] + grid[i][ecolumn-1];
for(int i=ecolumn-2;i>=0;i--)
distance[erow-1][i] = distance[erow-1][i+1] + grid[erow-1][i];
for(int i=erow-2;i>=0;i--){
for(int j=ecolumn-2;j>=0;j--){
distance[i][j] = min(distance[i+1][j], distance[i][j+1])+grid[i][j];
}
}
return distance[0][0];
}
};
leetcode第一刷_Minimum Path Sum的更多相关文章
- leetcode第一刷_Simplify Path
这道题的思路还是比較清晰的,用栈嘛,麻烦是麻烦在这些层次的细节上.主要有以下几个: ./和/:当前路径,遇到这样的,应该将后面的文件夹或文件入栈. ../:上一层路径.遇到这样的.应该做一次出栈操作, ...
- leetcode第一刷_Minimum Window Substring
好题.字符串.线性时间. 我认为第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符全然包括了另一个给定目标串中的字 ...
- leetcode第一刷_Minimum Depth of Binary Tree
非常easy的题目.只是还是认为要说一下. 最小深度.非常快想到bfs,层序遍历嘛.本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息.后来想到能够压入一个特殊的对象,每次到达这个对象 ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- leetcode–Binary Tree Maximum Path Sum
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
- 【LeetCode练习题】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
随机推荐
- C++ Primer中文版(第5版)
<C++ Primer中文版(第5版)> 基本信息 作者: (美)Stanley B. Lippman(斯坦利 李普曼) Josee Lajoie(约瑟 拉乔伊) Barbar ...
- HDU1087:Super Jumping! Jumping! Jumping!(DP)
Problem Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very ...
- OCP-1Z0-051-题目解析-第30题
30. Evaluate the following CREATE TABLE commands: CREATE TABLE orders (ord_no NUMBER(2) CONSTRAINT o ...
- 如何实现MySQL随机查询数据与MySQL随机更新数据?
以下的文章主要介绍的是MySQL随机选取数据,对实现MySQ随机查询数据与MySQ随机更新数据的实际操作步骤的描述,以及对其实际操作中所要用到的语句的描述,以下就是对其具体操作步骤的描述. MySQL ...
- ecshop 后台添加 成本价 利润
ecshop后台admin中的商品操作php文件,goods.php替换为下面的代码, 还要在数据库商品本店售价后门添加 cost 字段 为 商品成本价 ecs_goods表中添加 cost ...
- Redis 的性能幻想与残酷现实(转)
2011 年,当初选择 Redis 作为主要的内存数据存储,主要吸引我的是它提供多样的基础数据结构可以很方便的实现业务需求.另一方面又比较担心它的性能是否足以支撑,毕竟当时 Redis 还属于比较新的 ...
- NET Core控制反转(IoC)
ASP.NET Core中的依赖注入(1):控制反转(IoC) ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制, ...
- 《Javascript高级程序设计》读书笔记之继承
1.原型链继承 让构造函数的原型对象等于另一个类型的实例,利用原型让一个引用类型继承另一个引用类型的属性和方法 function SuperType() { this.property=true; } ...
- Socket开发
Socket开发框架之消息的回调处理 伍华聪 2016-03-31 20:16 阅读:152 评论:0 Socket开发框架之数据加密及完整性检查 伍华聪 2016-03-29 22:39 阅 ...
- python学习之list
list: 创建:list = [5,7,9] 取值和改值:list[1] = list[1] * 5 列表尾插入:list.append(4) 去掉第0个值并返回第0个值的数值:list.pop(0 ...