题目

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. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题

    B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...

  2. 无法在web服务器上启动调试,iis未列出与打开的URL匹配的网站

    错误的原因可能是:在iis的网站上绑定的具体的机器的ip地址. 解决方法:可以在网站上绑定ip地址时选择“全部未分配”项.

  3. Redis主从同步分析(转)

    一.Redis主从同步原理 1.1 Redis主从同步的过程 配置好slave服务器连接的master后,slave会建立和master的连接,然后发送sync命令.无论是第一次同步建立的连接还是连接 ...

  4. ZOJ 3765 Lights (伸展树splay)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3765 Lights Time Limit: 8 Seconds ...

  5. SWD Connect/Transfer Source Code

    Serial Wire Debug interface The Serial Wire Debug protocol operates with a synchronous serial interf ...

  6. google打不开怎么办?谷歌打不开的解决方法

    www.ggfwzs.com 我是在这里安装插件,安装后可以打开google http://jingyan.baidu.com/article/b907e627d67ad646e6891c52.htm ...

  7. vue首屏加载优化

    库使用情况 vue vue-router axios muse-ui material-icons vue-baidu-map 未优化前 首先我们在正常情况下build 优化 1. 按需加载 当前流行 ...

  8. 在ASP.NET MVC下通过短信验证码注册

    以前发短信使用过短信猫,现在,更多地是使用第三方API.大致过程是: → 用户在页面输入手机号码→ 用户点击"获取验证码"按钮,把手机号码发送给服务端,服务端产生几位数的随机码,并 ...

  9. arcgis导oracle多步操作产生错误。请检查每一步的状态值。" 如何解决?

    你知你用的什么数据引擎,ADO? 我以前碰过类似的,我有两个方案:   1.升ado到2.7以上      2.不要用microsoft oledb provider for oracle,而要用or ...

  10. Struts2 注解模式

    相信大家一定看到了两个class中定义了一样的action,不过看类的元数据,是不同的命名空间.这里比较重要(对我来说)的是 @Action(value = "/login", r ...