[LeetCode][Java] Minimum Path Sum

题目:

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.

题意:

给定一个m x n
大小的网格结构,网格中包含非负数组。现从网格的左上角移动到右下角,找出一条路径,使得路径中的数字和最小。

注意:每次移动只能向下或者向右。

算法分析:

方法一:递归

采用递归策略,从右下角终点开始向前递归,也是利用了问题的最优解包含子问题的最优解这一思想,层层递归,直到起点。

但是在Leetcode 网站上测试超时了(囧)

代码如下:

</pre><pre name="code" class="java" style="font-size: 14px;">public class Solution
{
    /*递归超时了
    public int minPathSum(int[][] grid)
    {
        int res=0;
        int legh=grid.length;
        int legl=grid[0].length;
        if(legh==0) return 0;
        if(legh==1&&legl==1) return grid[0][0];
        res=MinRecursive(legh-1,legl-1,grid);
    	return res;
    }
    private static int  MinRecursive(int i,int j,int[][] grid)
    {
    	if( i==0 && j==0)
    		return grid[0][0];
    	if(i==-1||j==-1) return Integer.MAX_VALUE;
    	return Math.min(MinRecursive(i-1, j,grid),MinRecursive(i,j-1,grid))+grid[i][j];
   	}
}

方法二:动态规划

*首先可以找出递推关系,递推要比递归快很多,因此在大规模数据上比递归更具优势。

*比如设存放起点到每个格子 i,j 的最小路径和的二维数组为 res[i][j],那么递推公式为:

*res[i][j] = Min(res[i-1][j],res[i][j-1])+ val[i][j];

*也就是其左侧格子( i,j-1) 或者其上侧格子 (i-1,j)这两个来源的较小路径值,再加上当前格子的值 val[i][j] 即为结果。

*提前算好为左侧格子和上侧格子的最短路径结果,下面每次计算某个格子时,利用算好的阶段
、状态来计算当前格子的结果

*这也是动态规划比递归要快的原因。

代码如下

  //方法二,动态规划
   public int minPathSum(int[][] grid)
    {
        if(grid.length==0)
            return 0;
        int res[][] = new int[grid.length][grid[0].length];
        res=grid;
        int i, j;
        for( j=1; j<res[0].length; ++j)
            res[0][j] += res[0][j-1];
        for(i=1; i<res.length; ++i)
            res[i][0] += res[i-1][0];
        for(i=1; i<res.length; ++i)
        {
            for(j=1; j<res[i].length; ++j)
            {
                res[i][j] = Math.min(res[i-1][j], res[i][j-1])+grid[i][j];
            }
        }
        return res[grid.length-1][grid[0].length-1];    //注意行列的size不一定一样
    }
时间: 2024-11-08 11:11:36

[LeetCode][Java] Minimum Path Sum的相关文章

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 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. 这道题的要求是在m*n

【Leetcode】Minimum Path Sum

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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

Leetcode 动态规划 Minimum Path Sum

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Minimum Path Sum Total Accepted: 15789 Total Submissions: 50645My Submissions Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum

Java for LeetCode 064 Minimum Path Sum

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. 解题思路: dp问题,和上一题一样,JAVA实现如下: stati

LeetCode 64. Minimum Path Sum Java

题目: 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. 题意及分析:给出一个m*n大的网格,每个网格中的数字都为非

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

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. 题目标签:Array 这道题目和前面两题差不多,基本思想都是一样的

【LeetCode】Minimum Path Sum 解题报告

[题目] 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. [思路] 求从左上角到右下角的最小路径值,典型的动态规划

LeetCode 64. Minimum Path Sum 20170515

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. 题目大意:给定一个一个m*n格的方针,每个方格都放一个非负数,找到

LeetCode 63 Minimum Path Sum

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. 思路 :求grid[0][0]到grid[i][j]的最小和,就必