能够用递归简洁的写出,可是会超时。

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的更多相关文章

  1. leetcode第一刷_Simplify Path

    这道题的思路还是比較清晰的,用栈嘛,麻烦是麻烦在这些层次的细节上.主要有以下几个: ./和/:当前路径,遇到这样的,应该将后面的文件夹或文件入栈. ../:上一层路径.遇到这样的.应该做一次出栈操作, ...

  2. leetcode第一刷_Minimum Window Substring

    好题.字符串.线性时间. 我认为第一次拿到这个题的人应该不会知道该怎么做吧,要么就是我太弱了..先搞清楚这个题要求的是什么.从一个长字符串中找一个字串,这个字串中的字符全然包括了另一个给定目标串中的字 ...

  3. leetcode第一刷_Minimum Depth of Binary Tree

    非常easy的题目.只是还是认为要说一下. 最小深度.非常快想到bfs,层序遍历嘛.本科的时候实在是没写过多少代码,一開始竟然想不到怎么保存一层的信息.后来想到能够压入一个特殊的对象,每次到达这个对象 ...

  4. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  5. 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 ...

  6. [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. ...

  7. 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 ...

  8. 【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 ...

  9. C++ leetcode Binary Tree Maximum Path Sum

    偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...

随机推荐

  1. SESC中的热量模拟器

    SESC安装见前文 配置sesc支持热量模拟 ../sesc/configure --enable-power --enable-therm make 遇到问题: 1 找不到 liblevmar.a ...

  2. MD5加密算法的实现

    //////////////////////////////////////////////////////////////////// /*                 md5.h        ...

  3. 〖Groovy〗语言使用贴士(Tips)(转)

    [Groovy]是一门运行在[JVM]之上的动态语言.由[James Strachan]和[Bob McWhirter]于2003年启动开发,之后于2004年3月成为[JSR 241](Java Sp ...

  4. 成为JAVA软件开发工程师要学哪些东西

    2010-04-22 15:34 提问者采纳 Java EE(旧称j2ee)   第一阶段:Java基础,包括java语法,面向对象特征,常见API,集合框架: *第二阶段:java界面编程,包括AW ...

  5. IntelliJ IDEA Groovy(转)

    更新环境变量: source /etc/profile 验证是否成功: # groovy -version Groovy Version: 2.3.6 JVM: 1.7.0_67 Vendor: Or ...

  6. A Game of Thrones(1) - Bran

    The morning had dawned clear and cold, with a crispness(易碎:清新) that hinted(暗示:示意) at the end of summ ...

  7. SVM-SVM概述

    (一)SVM背景资料简介 支持向量机(Support Vector Machine)这是Cortes和Vapnik至1995首次提出,样本.非线性及高维模式识别中表现出很多特有的优势,并可以推广应用到 ...

  8. Android 动态显示和隐藏软键盘

    ** * 动态设置软盘的显示和隐藏 * @author JPH */ public class MainActivity extends Activity implements OnClickList ...

  9. 使用NSCondition实现多线程同步

    iOS中实现多线程技术有非常多方法. 这里说说使用NSCondition实现多线程同步的问题,也就是解决生产者消费者问题(如收发同步等等). 问题流程例如以下: 消费者取得锁,取产品,假设没有,则wa ...

  10. Oracle学习(十四):管理用户安全性

    --用户(user) SQL> --创建一个名为 grace password是password 的用户,新用户没有不论什么权限 SQL> create user grace identi ...