"Leetcode-Interview algorithm classic-java Implementation" "064-minimum path Sum (minimum path and)"

Source: Internet
Author: User

"064-minimum path Sum (minimum path and)" "leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index" Original Question

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.

Main Topic

Given a square of M x N, each element's value is nonnegative, finding the path from the top-left vertex to the lower-right vertex and the lowest value, returning the smallest and found.

Thinking of solving problems

Divide and conquer the law,
First: s[0][0] = grid[0][0]
First line: s[0][j] = s[0][j-1] + grid[0][j]
First column: s[i][0] = s[i-1][0] + grid[i][0]
Other cases: s[i][j] = min (S[i-1][j], s[i][j-1]) + grid[i][j]

Code Implementation

Algorithm implementation class

 Public classSolution { Public int Minpathsum(int[] grid) {//parameter checking        if(Grid = =NULL|| Grid.length <1|| grid[0].length <1) {return 0; }int[[] result =New int[Grid.length] [grid[0].length];//Firstresult[0][0] = grid[0][0];//First line         for(inti =1; I < result[0].length; i++) {result[0][i] = result[0][i-1] + grid[0][i]; }//First column         for(inti =1; i < result.length; i++) {result[i][0] = Result[i-1][0] + grid[i][0]; }//Other conditions         for(inti =1; i < result.length; i++) { for(intj =1; J < result[0].length; J + +) {Result[i][j] = math.min (Result[i-1][J], Result[i][j-1]) + grid[i][j]; }        }returnResult[result.length-1][result[0].length-1]; }////////////////////////////////////////////////////////////////////////////////////////////////    //dynamic attribution and branching, the following method will time out    ////////////////////////////////////////////////////////////////////////////////////////////////     Public int minPathSum2(int[] grid) {//parameter checking        if(Grid = =NULL|| Grid.length <1|| grid[0].length <1) {return 0; }//Used to record the smallest path        int[] Minsum = {Integer.max_value};int[] Cursum = {0};//SolvingSolve (grid,0,0, Cursum, minsum);//Return results        returnminsum[0]; } Public void Solve(int[] Grid,intRowintColint[] Cursum,int[] minsum) {//If you have reached the end        if(Row = = Grid.length-1&& col = = grid[0].length-1) {cursum[0] + = Grid[row][col];//Update the minimum and            if(cursum[0] < minsum[0]) {minsum[0] = cursum[0]; } cursum[0]-= Grid[row][col]; }//has not reached the end point and is within the grid        Else if(Row >=0&& Row < grid.length && Col >=0&& Col < grid[0].length) {cursum[0] + = Grid[row][col];//current and only minimum path values not less than recorded to proceed to the next steps            if(cursum[0] <= minsum[0]) {//GO rightSolve (grid, row, col +1, Cursum, minsum);//Go downSolve (grid, row +1, col, Cursum, minsum); } cursum[0]-= Grid[row][col]; }    }}
Evaluation Results

  Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.

Special Instructions Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47203311"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Leetcode-Interview algorithm classic-java Implementation" "064-minimum path Sum (minimum path and)"

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.