"064-minimum path Sum (min path and)"
" leetcode-interview algorithm classic-java Implementation" "All topic Directory Index"
Original 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 the any.
The main effect of the topic
Given a square of M x N, the value of each element is non-negative, finding the smallest and least-found path from the top left corner to the lower-right vertex and the lowest value.
ideas for solving problems
Divide 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 class Solution {public int minpathsum (int[][] grid) {//Parameter check if (Grid = = NULL | | grid.length < 1 | |
Grid[0].length < 1) {return 0;
} int[][] result = new Int[grid.length][grid[0].length];
The first result[0][0] = grid[0][0];
The first line for (int i = 1; i < result[0].length i++) {result[0][i] = Result[0][i-1] + grid[0][i]; //first column for (int i = 1; i < result.length i++) {result[i][0] = result[i-1][0]
+ grid[i][0]; }//other case for (int i = 1; i < Result.length i++) {for (int j = 1; J < Result[0].lengt H
J + +) {Result[i][j] = Math.min (Result[i-1][j], result[i][j-1]) + grid[i][j];
} return result[result.length-1][result[0].length-1];
}
//////////////////////////////////////////////////////////////////////////////////////////////// Dynamic attribution and branching limits, the following method will timeout////////////////////////////////////////////////////////////////////////////////////////// public int minPathSum2 (int[][] grid) {//Parameter check if (Grid = NULL | | Grid.length < 1 | | grid[0
].length < 1) {return 0;
}//For recording the smallest path of each int[] Minsum = {Integer.max_value};
Int[] Cursum = {0};
Problem solving solve (grid, 0, 0, cursum, minsum);
Returns the result return minsum[0]; } public void Solve (int[][] grid, int row, int col, int[] cursum, int[] minsum) {//If the endpoint is reached (row
= = Grid.length-1 && col = = grid[0].length-1) {cursum[0] + = Grid[row][col];
Update the smallest and if (Cursum[0] < minsum[0]) {minsum[0] = cursum[0];
} cursum[0]-= Grid[row][col]; //has not reached the end point and in the grid else if (row >= 0 && Row < grid.length && Col >= 0 && Col < grid[0].length) {cursum[0] + = Grid[row][col]; The current and only no less than the record to the minimum path value to proceed to the next action if (Cursum[0] <= minsum[0]) {//Go right solve (g
RID, row, col + 1, cursum, minsum);
Go down Solve (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, released in a new window to view the full picture.
Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47203311"