1. 程式人生 > >leetcode 62. Unique Paths-唯一路徑|動態規劃

leetcode 62. Unique Paths-唯一路徑|動態規劃

【思路-Java】

採用動態規劃。動態規劃要求利用到上一次的結果,是一種特殊的迭代思想,動態規劃的關鍵是要得到遞推關係式。對於本題,到達某一點的路徑數等於到達它上一點的路徑數與它左邊的路徑數之和。也即,起點到點(i, j)的路徑總數:ways[i][j] = 起點到點(i, j-1)的總數:ways[i][j-1] + 起點到點(i-1, j)總數:ways[i-1][j]。於是我們就得到遞推關係式:ways[i][j] = ways[i][j-1] + ways[i-1][j]

public class Solution {
    public int uniquePaths(int m, int n) {
        int[][] ways = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || j == 0) ways[i][j] = 1;
                else ways[i][j] = ways[i-1][j] + ways[i][j-1];
            }
        }
        return ways[m-1][n-1];
    }
}
61 / 61 test cases passed. Runtime: 1 ms  Your runtime beats 6.88% of javasubmissions.

【優化-Java、Python】

這種優化是對上述方法空間的一種優化,使得空間複雜度從原來的 O(n*m)下降為 O(n)。對於起點到點(i,j)的路徑總數:ways[j]= 起點到點(i-1, j) 的路徑總數:ways[j] + 起點到點(i, j-1)的路徑總數 ways[j-1],於是我們就得到遞推式:ways[j] = ways[j] + ways[j-1]

public class Solution {
    public int uniquePaths(int m, int n) {
        int[] ways = new int[n];
        ways[0] = 1;
        for(int i = 0; i < m; i++)
            for (int j = 1; j < n; j++)
                ways[j] += ways[j-1];
        return ways[n-1];
    }
}

61 / 61 test cases passed. Runtime: 0 ms  Your runtime beats 85.40% of javasubmissions.

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        ways = [0]*n
        ways[0] = 1
        for i in range(m) :
            for j in range(1, n) :
                ways[j] += ways[j-1]
        return ways[n-1]
61 / 61 test cases passed. Runtime: 60 ms  Your runtime beats 13.27% of pythonsubmissions.

【補充-Java】遞迴實現

用遞迴,但是對大case會超時

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

有沒有想過為什麼會超時?因為遞迴和迴圈不同,迴圈可以用陣列儲存上一次計算的結果,而遞迴的上一次結果會被銷燬,因而會多出來很多重複的中間計算結果:


到達某一格的路徑數量等於它的上面和左邊的路徑數之和,結束條件是走到行或者列的邊緣。標紅出表示重複計算,可以看到 n 和 m 非常大時,重複計算量是非常大的,時間複雜度是 n 和 m 的量級,超時為不足為怪!

相關推薦

leetcode 62. Unique Paths-唯一路徑|動態規劃

【思路-Java】 採用動態規劃。動態規劃要求利用到上一次的結果,是一種特殊的迭代思想,動態規劃的關鍵是要得到遞推關係式。對於本題,到達某一點的路徑數等於到達它上一點的路徑數與它左邊的路徑數之和。也即,起點到點(i, j)的路徑總數:ways[i][j] = 起點到點(

[LeetCode 62] Unique Paths(教科書般的動態規劃

題目內容 62 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

LeetCode 62. Unique Paths (獨立路徑)

原題 A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). A robot is located at the top-left corner

leetcodeUnique Paths II(動態規劃

63. Unique Paths II 題目描述 Discuss Pick One Follow up for “Unique Paths”: Now consider if some obstacles are added to the gr

LeetCode 62. Unique Paths--二維陣列從左上角到右下角的唯一路徑的種數有多少,只能向右或向下移動--DP

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 r

[LeetCode]62. Unique Paths&&動態規劃

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

Leetcode】【DP-二維陣列】 62. Unique Paths / 不同路徑

給一個形狀為m x n的矩陣,求從左上角到右下角的不同路徑的個數。行進時只能往右/下移動。 方法一:使用二維陣列儲存每個位置的dp值 稍作畫圖分析即可得到dp式子:dp [i] [j] = dp [i-1] [j] + dp [i] [j-1] (

LeetCode 62. Unique Paths Java

fin can 一個 其中 leet logs 網格 marked int 題目: A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below)

leetcode 62. Unique Paths

log etc clas 個數 vector uniq true space 條件 第一種最簡單的方法:遞歸求解: 要求uniquePaths(m,n)=uniquePaths(m-1,n)+uniquePaths(m,n-1)(m>1,n>1) uni

LeetCode-62. Unique Paths

使用數組 清晰 col == ++ tor leetcode 分享圖片 div 使用動態規劃,思路很清晰 使用如下代碼,發現超時了 int uniquePaths(int m, int n) { if (m == 1 || n == 1) ret

[leetcode]62. Unique Paths

[leetcode]62. Unique Paths Analysis 週一鴨—— [每天刷題並不難0.0] A robot is located at the top-left corner of a m x n grid (marked ‘Start’ i

LeetCode-62 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 eithe

[leetcode] 62 Unique Paths (Medium)

原題連結 思路: dp[i][j]儲存走到第i,j格共有幾種走法。 因為只能走→或者↓,所以邊界條件dp[0][j]+=dp[0][j-1] 同時容易得出遞推 dp[i][j]+=dp[i-1][j]

leetcode 62 Unique Paths 62 Unique Paths 62 Unique Paths 【走格子 簡單dp】

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 r

演算法分析與設計——LeetCode:62. 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 i

19.2.9 [LeetCode 62] Unique Paths

ets adb 技術 start tom 時間 lose poi return A robot is located at the top-left corner of a m x n grid (marked ‘Start‘ in the diagram below).

LeetCode62. Unique Paths(C++)

地址:https://leetcode.com/problems/unique-paths/ 題目: A robot is located at the top-left corner of a m

LeetCode算法系列:62. Unique Paths && 63. Unique Paths II && 64. Minimum Path Sum

這三個問題及其類似,背景都是和路徑相關,方法都是動態優化 目錄 62題: 題目描述: 演算法實現: 63題 題目描述: 演算法實現: 64題 題目描述 演算法實現 62題: 題目描述: A robot is located at the top-le

LeetCode Day51 Unique Paths II 不同的路徑之二

class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { vector<int> res(obsta

LeetCode 63. Unique Paths II Java

false mos tac empty uniq pub ons str ide 題目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How