Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

  1. int minPathSum(vector<vector<int> > &grid) {
  2. // Start typing your C/C++ solution below
  3. // DO NOT write int main() function
  4. int row = grid.size();
  5. int col = grid[0].size();
  6.  
  7. int**dp = new int*[row];
  8. for(int i=0; i< row; i++)
  9. dp[i] = new int[col];
  10.  
  11. dp[0][0] = grid[0][0];
  12. for(int j =1; j < col; j++)
  13. dp[0][j] = dp[0][j-1] + grid[0][j];
  14.  
  15. for(int i = 1; i < row; i++)
  16. for(int j =0; j < col; j++)
  17. if(j == 0)dp[i][0] = dp[i-1][0] + grid[i][0];
  18. else{
  19. int tmp = dp[i-1][j] < dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
  20. dp[i][j] = tmp + grid[i][j];
  21. }
  22.  
  23. int tmp = dp[row-1][col-1];
  24. for(int i = 0; i < row; i++)
  25. delete[] dp[i];
  26. delete[] dp;
  27. return tmp;
  28. }

leetcode_question_64 Minimum Path Sum的更多相关文章

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

  2. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

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

  4. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

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

  6. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  7. 【LeetCode】64. 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 ...

  8. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  9. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

随机推荐

  1. ajax_异步交互-get/post方式

    Ajax的异步交互: 客户端向服务器端发送请求,直到服务器端进行响应,这个过程中,用户可以做任何其他事情(不等). 实现Ajax的异步交互步骤(举例说明): get方式: 1.创建XMLHttpReq ...

  2. RAC ORA-12170 ora-12535/tns-12535

    现象:开发人员抱怨RAC数据库出现了时连得上时连不上的情况,用SQLPLUS一试,果然有这样的情况: SQL> conn system/*******@bjyd 已连接. SQL> con ...

  3. A Byte of Python 笔记(10)输入/输出:文件和储存器

    第12章  输入/输出 大多数情况下,我们需要程序与用户交互.从用户得到输入,然后打印一些结果. 可以分别使用 raw_input 和 print 语句来完成这些功能.对于输出,可以使用多种多样的 s ...

  4. https tomcat 证书搭建

    首先生成证书说明 keytool -genkey -alias castest -keyalg RSA -keystore c:/keys/caskey 先让输入密码,密码必须记住,下面会用到 其中“ ...

  5. AngularJS的启动引导过程

    原文:http://www.angularjs.cn/A137?utm_source=ourjs.com 目录: 引导之前 自动引导启动框架 手工引导启动框架 引导第1步:创建注入器 引导第2步:创建 ...

  6. 哈夫曼树(Huffman)的JS实现

    我本身并不懂哈夫曼树也不知道有什么用,GOOGLE了下,也只是一知半解,只是刚好看到有JAVA实现版,又看了下生成原理,感觉挺有意思,就写了一下 有些地方可以优化,效率不怎么样的,纯好玩,也不保证一定 ...

  7. Android平台APK分析工具包androguard的部署使用和原理分析

    原创文章,转载请注明出处,谢谢. Android应用程序分析主要有静态分析和动态分析两种,常见的静态分析工具是Apktool.dex2jar以及jdgui.今天突然主要到Google code上有个叫 ...

  8. 高频(工作频率为13.56MHz)

    在该频率的感应器不再需要线圈进行绕制,可以通过腐蚀活着印刷的方式制作天线.感应器一般通过负载调制的方式 的方式进行工作.也就是通过感应器上的负载电阻的接通和断开促使读写器天线上的电压发生变化,实现用远 ...

  9. 完成端口(Completion Port)详解(超级长,超级清楚)

    http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html

  10. 过目不忘JS正则表达式(转)

    正则表达式,有木有人像我一样,学了好几遍却还是很懵圈,学的时候老明白了,学完了忘光了.好吧,其实还是练的不够,所谓温故而知新,可以为师矣,今天就随我来复习一下这傲娇的正则表达式吧. 为啥要有正则表达式 ...