[Leetcode] [Java] Minimum Path Sum

Source: Internet
Author: User

Title:

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.

Test Instructions:

Given a grid of m x n filled with non-negative numbers , look for a path from the upper-left corner of the lattice to the lower-right corner, making the number and minimum on the path.

Note: Each time you can only move down or to the right in the grid.


Algorithm Analysis:

Method One:

Recursive strategy, starting from the end of the lower right corner of the forward recursion, but also using the problem of the optimal solution contains sub-problem of the idea of the optimal solution, layers of recursion, until the beginning

But the problem timed out.

Method Two:
Dynamic planning

The idea is similar to "Unique Paths".

Define DP[I][J]: represents the smallest and most of all paths from the top left to the [i,j] point, so the minimum path for [i,j] depends only on [i-1,j] and [i,j-1] two positions, because it can only move down and to the right.

That is Dp[i][j]=min (Dp[i-1][j],dp[i][j-1]) +grid[i][j] But the first and first columns are handled in a special order.


AC Code:

method One:(timed out)

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];   }


Method Two:

<span style= "Font-family:microsoft Yahei;" >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];    Note that the size of the rows is not necessarily the same      }</span>


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Minimum Path Sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.