[LeetCode-interview algorithm classic-Java implementation] [064-Minimum Path Sum (Minimum Path and)], leetcode -- java
[064-Minimum Path Sum (Minimum Path and )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]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.
Theme
Given a square of m x n, the values of each element are non-negative. Find the path from the top left vertex to the smallest vertex and value in the bottom right corner, and return the minimum sum found.
Solutions
Divide and conquer law,
First: S [0] [0] = grid [0] [0]
Row 1: S [0] [j] = S [0] [j-1] + grid [0] [j]
Column 1: 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 test 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];} // The first column for (int I = 1; I <result. length; I ++) {result [I] [0] = result [I-1] [0] + grid [I] [0];} // In other cases, for (int I = 1; I <result. length; I ++) {for (int j = 1; j <result [0]. length; 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 grouping and restriction, the following method times out /////////////////////////////////// //////////////////////////////////////// /// // public int minPathSum2 (int [] [] grid) {// parameter test if (grid = null | grid. length <1 | grid [0]. length <1) {return 0;} // used to record the smallest path of each int [] minSum = {Integer. MAX_VALUE}; int [] curSum = {0}; // solve (grid, 0, 0, curSum, minSum); // return result return minSum [0];} public void solve (int [] [] grid, int row, int col, int [] curSum, int [] minSum) {// if the destination is reached, if (row = grid. length-1 & col = grid [0]. length-1) {curSum [0] + = grid [row] [col]; // the smallest update and if (curSum [0] <minSum [0]) {minSum [0] = curSum [0];} curSum [0]-= grid [row] [col];} // The end point has not been reached, 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 the minimum paths not less than the recorded values can perform the next operation if (curSum [0] <= minSum [0]) {// solve (grid, row, col + 1, curSum, minSum); // go down to solve (grid, row + 1, col, curSum, minSum );} curSum [0]-= grid [row] [col] ;}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the source for reprinting at http://blog.csdn.net/derrantcm/article/details/47203311]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.