zoukankan      html  css  js  c++  java
  • 【LeetCode】062. Unique Paths

     题目:

    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 are there?

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

    Note: m and n will be at most 100.

    题解:

    Solution 1 ()

    class Solution {
    public:
        int uniquePaths(int m, int n) {
            if(m<0 || n<0) return 0;
            if(m ==0 && n == 0) return 0;
            vector<vector<int>> dp(m, vector<int> (n,1));
            for(int i=1; i<m; ++i) {
                for(int j=1; j<n; ++j) {
                    dp[i][j] = dp[i-1][j] + dp[i][j-1];
                }
            }
            return dp[m-1][n-1];    
        }
    };

      用一维数组存储,d[j] = d[j] + d[j-1];这里的等号右边d[j]相当于d[i-1][j],d[j-1]相当于d[i][j-1];

    Solution 2 ()

    class Solution {
    public:
        int uniquePaths(int m, int n) {
            vector<int> dp(n, 1);
            for (int i = 1; i < m; ++i) {
                for (int j = 1; j < n; ++j) {
                    dp[j] += dp[j - 1]; 
                }
            }
            return dp[n - 1];
        }
    }; 

    First of all you should understand that we need to do n + m - 2 movements : m - 1 down, n - 1 right, because we start from cell (1, 1).

    Secondly, the path it is the sequence of movements( go down / go right),
    therefore we can say that two paths are different
    when there is i-th (1 .. m + n - 2) movement in path1 differ i-th movement in path2.

    So, how we can build paths.
    Let's choose (n - 1) movements(number of steps to the right) from (m + n - 2),
    and rest (m - 1) is (number of steps down).

    I think now it is obvious that count of different paths are all combinations (n - 1) movements from (m + n-2). (from here)

    Solution 3 ()

    class Solution {
    public:
        int uniquePaths(int m, int n) {
            int N = n + m - 2;// how much steps we need to do
            int k = m - 1; // number of steps that need to go down
            double res = 1;
            // here we calculate the total possible path number 
            // Combination(N, k) = n! / (k!(n - k)!)
            // reduce the numerator and denominator and get
            // C = ( (n - k + 1) * (n - k + 2) * ... * n ) / k!
            for (int i = 1; i <= k; i++)
                res = res * (N - k + i) / i;
            return (int)res;
       }
    };
  • 相关阅读:
    (转)视频编码标准汇总及比较
    (转)live555从RTSP服务器读取数据到使用接收到的数据流程分析
    (转)MPEG4码流简单分析
    H264裸流分析中,能获取哪些信息?
    (转)基于live555的流媒体代理转发服务器
    测试x264编码器的低延时编码和非延时编码
    ELK 日志分析系统
    Dubbox:来自当当网的SOA服务框架
    CHMOD命令怎么用?
    linux显示文件列表命令ls,使用ls --help列出所有命令参数
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6816870.html
Copyright © 2011-2022 走看看