【问题标题】:Time complexity of path finding problem using dynamic programming使用动态规划的寻路问题的时间复杂度
【发布时间】:2022-06-16 23:18:46
【问题描述】:

在第一节给你 x, y 坐标,你尝试到达原点 (0,0) 例如如果你在点 (1,1) 你可以去点 1,0 然后 0 ,0。返回您可以采取多少条独特的路径。 您只能向左或向下移动。

这可以递归解决,并且通过记忆,您可以优化解决方案。

class Solution {
    public static int findUniquePaths(int x, int y){
        if(x == 0 || y == 0 ){
            return 1;
        }
        
        return findUniquePaths(x -1, y) + findUniquePaths(x, y -1);
    }
    
    //initialize mem to -1 before calling this function.
    public static int findUniquePathsDP(int x, int y,int[][] mem){
        if(x == 0 || y == 0 ){
            return 1;
        }
        if (mem[x][y] != -1){
            return mem[x][y];
        }
    
        mem[x][y] = findUniquePathsDP(x -1, y,mem) + findUniquePathsDP(x, y -1,mem);
        return mem[x][y];
    }
}

无论有没有记忆,我如何才能找到这个问题的时间复杂度?

【问题讨论】:

  • “原点”有点不清楚,你的意思是打 X 轴还是 Y 轴(你的实现意味着什么)?还是您的意思是到达 (0,0) 点?

标签: java dynamic-programming


【解决方案1】:

我发现对于没有 mem 的递归,它将是 O(2^(m+n)),但对于 mem,它将是 O(m*n)。但是我不确定 100。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2012-04-09
    相关资源
    最近更新 更多