题目

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.

题解

这道题跟Unique Path系列是思路一样的。具体思路看代码就很清楚了

代码如下:

 1     public int minPathSum(int[][] grid) {
 2         int m = grid.length;
 3         int n = grid[0].length;
 4         
 5         if(m==0||n==0)
 6             return 0;
 7             
 8         int[][] dp = new int[m][n];
 9         
         dp[0][0]=grid[0][0];
         
         //a row
         for (int i = 1; i < n; i++) 
             dp[0][i] = dp[0][i - 1] + grid[0][i];  
 
         //a column
         for (int j = 1; j < m; j++)   
             dp[j][0] = dp[j - 1][0] + grid[j][0];  
         
         for (int i=1; i<m; i++){  
                 for (int j=1; j<n; j++){  
                     if(dp[i-1][j]<dp[i][j-1])
                         dp[i][j]=dp[i-1][j]+grid[i][j];
                     else
                         dp[i][j]=dp[i][j-1]+grid[i][j];
                 }  
             }  
             return dp[m-1][n-1];  
     }

Minimum Path Sum leetcode java的更多相关文章

  1. 【LeetCode】Path Sum ---------LeetCode java 小结

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  3. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  4. Minimum Path Sum——LeetCode

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  5. Path Sum leetcode java

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  6. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

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

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

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

随机推荐

  1. css实现自适应正方形

    这里介绍7种方法,仅供参考. 1.vm单位 <div class="square-shape">这是一个可以自适应的正方形,此法适用于移动端web页面.</div ...

  2. Codeforces.838D.Airplane Arrangements(思路)

    题目链接 \(Description\) 飞机上有n个位置.有m个乘客入座,每个人会从前门(1)或后门(n)先走到其票上写的位置.若该位置没人,则在这坐下:若该位置有人,则按原方向向前走直到找到空座坐 ...

  3. CTSC被虐记

    退役前写写破事乐呵乐呵..(雾 Day0 愉快的没有分到另一个宾馆...但是是个单间...而且居然是大床房...难以置信, 试机向BeiYe学习了一发Gedit的外部工具, 试到一般好像都走了..只剩 ...

  4. Oracle初始化参数之memory_target

    一.引言: Oracle 9i引入pga_aggregate_target,可以自动对PGA进行调整: Oracle 10g引入sga_target,可以自动对SGA进行调整: Oracle 11g则 ...

  5. Android 开源库获取途径整理

    介绍眼下收藏 Android 开源库比較多的 GitHub 项目.站点.Twitter.App 及怎样获取最新的 Android 开源库. 微信号: 1. GitHub Android 开源项目汇总 ...

  6. Sublime Text 2搭建Go开发环境,代码提示+补全+调试

    本文在已安装Go环境的前提下继续. 1.安装Sublime Text 2 2.安装Package Control. 运行Sublime,按下 Ctrl+`(`在Tab键上边),然后输入以下内容: im ...

  7. EZ1105

    http://www.godexintl.com/cn/product/type/model/EZ1105

  8. hdu 4643 GSM 计算几何 - 点线关系

    /* hdu 4643 GSM 计算几何 - 点线关系 N个城市,任意两个城市之间都有沿他们之间直线的铁路 M个基站 问从城市A到城市B需要切换几次基站 当从基站a切换到基站b时,切换的地点就是ab的 ...

  9. Revit API创建几何实体Solid并找到与之相交的元素

    几何实体的创建方法之一:构成封闭底面,指定拉伸方向与拉伸高度.GeometryCreationUtilities ;         , pt.Y - dBoxLength / , pt.Z);    ...

  10. [Winform]WebKit.Net使用

    摘要 在项目中使用了cefsharp,最后发现在触屏电脑上面,如果长按文本内容,会经常性的崩溃,发现是cefsharp的问题,最后也等不及了.然后就换了webkit.net这个开源的浏览器内核. 关于 ...