"Leetcode" Unique Paths

Source: Internet
Author: User

Test instructions


A robot is located at the Top-left corner of a m x N grid (marked ' Start ' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying-to-reach the bottom-right corner of the grid (marked ' Finish ' in the diagram below).

How many possible unique paths is there?


Above is a 3 x 7 grid. How many possible unique paths is there?

Note: m and N would be is at most 100.


Ideas:

The robot can only go right and down, then we can easily think that the number of paths to a point is not equal to the number of paths to the left point of the point, plus the number of paths to the point above the point.

Next consider the special case, the point of the first column, there is no other point on the left, so it can only be reached by the point above it. The first line of the point, only through its left point to arrive, and then think about the first line of the point of the number of paths is not only 1?

See here, do you want to use a two-dimensional array? It's not necessary because we don't need that much state record. What we just need is the result of the last iteration (that is, the previous line). If I use a one-dimensional array of length n to hold the number of paths to a single line of points, then use m-1 iterations (why use m-1?). ), the first time an array is saved at the beginning of each iteration is the result of the previous row, so I can get the result of that point by adding the number of paths to the point on its left.


Code:

C++:

Class Solution {public:    int uniquepaths (int m, int n) {        int* DP = new Int[n];        for (int i = 0;i < N;++i)            dp[i] = 1;        for (int i = 1;i < M;++i)        {for            (int j = 1;j < N;++j)            {                Dp[j] + = dp[j-1];            }        }        int ans = dp[n-1];        delete []DP;        return ans;    };

Python:

Class solution:    # @param {integer} m    # @param {integer} n    # @return {integer}    def uniquepaths (self, M, N):        DP = [1 for i in range (0,n)] for        I in range (1,m): for            J in Range (1,n):                dp[j] = Dp[j] + dp[j-1]        return DP[N-1]

"Leetcode" Unique Paths

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.