Leetcode64-minimum path Sum (min path and) __DP

Source: Internet
Author: User

Problem Description:

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.

Given a grid of M * N, there are nonnegative digits in the grid. A robot to go from the upper left corner to the lower right corner, each can only move down or to the right one position, find a sum of the smallest path, return the minimum value.

The subject is similar to Leetcode62-unique paths.

Problem Solving:

A typical dynamic programming problem. three elements of dynamic programming: the phase of the problem, the state of each phase, and the recurrence relationship between the previous phase and the last stage. the recursive relationship must be transformed from a minor problem to a larger one, from this point of view, dynamic programming can often be implemented with recursive procedures, but because recursion can take advantage of the solution of the previously saved child problem to reduce the duplication of computation, so for large-scale problems, there is a recursive incomparable advantage, This is also the core of the dynamic programming algorithm. To determine the dynamic programming of these three elements, the entire solution process can be described with an optimal decision table, the Optimal decision table is a two-dimensional table, where the row represents the decision-making stage, the column represents the problem state, The data that the form needs to fill in generally corresponds to the optimal value of the problem at some stage of a certain state (e.g. shortest path, the longest common subsequence, the maximum value, etc., the process of filling out is based on the recursive relationship, from 1 rows of 1 columns, in order to row or column priority, sequentially fill in the form, Finally, according to the data of the whole table, the optimal solution of the problem is obtained by simple choice or operation.

First, we find the recurrence relation, such as setting the minimum path for storing the starting point to each lattice i,j and the two-dimensional array of mps[i][j], and the recursive formula is:

MPS[I][J] = Min (mps[i-1][j],mps[i][j-1]) + grid[i][j];

the MPs value of lattice i,j may have two sources: its left lattice i,j-1 or its upper side lattice i-1,j, and the smaller MPs value of these two sources, plus the value of the current lattice grid[i][j] is the result ...

Because it is from the top left to the right, so we can use a double loop for iterative calculation, the outer loop acts as a unit of behavior, and the inner loop is listed as a unit, so that the results of the current lattice can be computed using the already calculated stage and state, because each time a lattice is computed, its left and upper side lattice results are well , which is why dynamic planning is faster than recursion.

Class Solution {public
:
    int minpathsum (vector<vector<int>>& grid) {
        int m = grid.size (), n = Grid[0].size ();
        Vector<vector<int>> minpathsum (grid);
        for (int i=0;i<m;i++)
        {
            for (int j=0;j<n;j++)
            {
                if (i==0 | | j==0)
                {//First row or first column
                    if (i= =0) Minpathsum[0][j] + + = minpathsum[0][j-1];
                    else minpathsum[i][0] + = minpathsum[i-1][0];
                }
                else
                {//Grid [I][J] has a minpathsum value of two sources: left lattice [i][j-1], or upper side lattice [i-1][j].
                 Take both the smaller value, plus the current lattice value grid[i][j] that is the result
                    minpathsum[i][j] = min (minpathsum[i][j-1), minpathsum[i-1][j]) +grid[i][j];
            }
        }
        return minpathsum[m-1][n-1];
    }
;

The space complexity is O (M * n), and the time complexity is O (M * n).

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.