[Leedcode 64] Minimum Path Sum

Source: Internet
Author: User

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.

 Public classSolution { Public intMinpathsum (int[] grid) {        //Dynamic Programming Idea: Constructs a dp[][] two-dimensional array, Dp[i][j] represents the minimum and the number of rows that reach the J column of I,//when I>0 and J>0, Dp[i][j]=math.min (Dp[i][j-1],dp[i-1][j]) +grid[i][j]//note the processing of the first row and the first column//There is a practice that does not require additional space to modify the value directly on the original array        introw=grid.length; intCol=grid[0].length; intdp[][]=New int[Row][col];  for(inti=0;i<row;i++){            if(i==0) dp[i][0]=grid[i][0]; ElseDp[i][0]=grid[i][0]+dp[i-1][0];  for(intj=1;j<col;j++){                if(i==0) {Dp[i][j]=dp[i][j-1]+Grid[i][j]; }Else{Dp[i][j]=math.min (Dp[i][j-1],dp[i-1][j]) +Grid[i][j]; }                            }        }        returnDp[row-1][col-1]; }}

[Leedcode 64] 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.