[LeetCode]19. Unique Paths唯一路徑

來源:互聯網
上載者:User

標籤:

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.

 

解:1:從左上方走到右下角需要向下移動m-1步,向右移動n-1步。每次移動前,判斷是否還能向下或者向右移動。在向下或者向右移動一步後,接下來的移動過程可以遞迴調用。

class Solution {public:    int uniquePaths(int m, int n) {        int i = m, j = n;        int res = 0;        if (i <= 1 || j <= 1)        {            res += 1;            return res;        }        else if (i > 1 && j > 1)            res += uniquePaths(i - 1, j) + uniquePaths(i, j - 1);        else if (i > 1 && j <= 1)            res += uniquePaths(i - 1, j);        else if (i <= 1 && j > 1)            res += uniquePaths(i, j - 1);    }};

換個思路考慮,如果某次移動到了最下行或者最右行,則接下來的移動沒有選擇,只有一條路徑了。因此可以改進程式如下所示:

class Solution {public:    int uniquePaths(int m, int n) {        if(m == 1 || n == 1)            return 1;        else            return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);    }};

因為遞迴調用,在m,n較大時會出現Time Limit Exceeded。

 

解法2:從左上方走到右下角需要向下移動m-1步,向右移動n-1步,一共移動m+n-2步。因為中間沒有任何障礙,所以很簡單:即是在m+n-2步中取m-1步向下,或者是在m+n-2步中取n-1步向右即可。即是求組合數C(m+n-2,m-1)或者C(m+n-2,n-1)。因為m+n-2=m-1+n-1,假設n>m,則C(m+n-2,m-1)=(m+n-2)*(m+n-3)*...*n/(m-1)!。

class Solution {public:    int uniquePaths(int m, int n) {        int res = 0;        int a = m > n ? m : n;        int b = m + n - a - 1;        int sum = m + n - 2;        long long p1 = 1, p2 = 1;        while (sum >= a)            p1 =  p1 * sum--;        while (b >= 1)            p2 = p2 * b--;        res = p1 / p2;        return res;    }};

 

解法3:動態規劃。移動到[i,j]的路徑path[i,j]=path[i-1,j]+path[i,j-1](0<i<m,0<j<n),初始化path[i][0]=1,path[0][j]=1(0<=i<m,0<=j<n)。最後path[m-1][n-1]即是最終結果。時間空間複雜度都是O(mn)。

class Solution {public:    int uniquePaths(int m, int n) {        int res = 0;        if(m <= 0 || n <= 0)            return res;                int** path = new int*[m];        for(int i = 0; i < m; i++)            path[i] = new int[n];                for(int i = 0; i < m; i++)            path[i][0] = 1;        for(int i = 0; i < n; i++)            path[0][i] = 1;                for(int i = 1; i < m; i++)        {            for(int j = 1; j < n; j++)                path[i][j] = path[i - 1][j] + path[i][j - 1];        }        res = path[m - 1][n - 1];                for(int i = 0; i < m; i++)            delete[] path[i];        delete[] path;                return res;    }};

利用滾動數組可以將空間複雜度降為O(n):

class Solution {public:    int uniquePaths(int m, int n) {        int res = 0;        if(m <= 0 || n <= 0)            return res;                int* path = new int[n];        for(int i = 0; i < n; i++)            path[i] = 1;        for(int i = 1; i < m; i++)        {            for(int j = 1; j < n; j++)                path[j] += path[j - 1];        }        res = path[n - 1];        delete[] path;                return res;    }};

外層迴圈使得path儲存了移動到當前行的上一行所有位置的路徑數目,因此內層迴圈只需要在加上當前列的左邊列的路徑數目即可。

[LeetCode]19. Unique Paths唯一路徑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.