📜  计算从一个点到原点的路径:内存优化

📅  最后修改于: 2021-09-17 16:08:58             🧑  作者: Mango

给定两个整数NM ,它们表示坐标系中的位置,任务是通过向左或向下步进来计算从该点到原点的路径。
例子:

我们已经在这篇文章中讨论过这个问题——
计算从一个点到 Origin 的路径
方法:思路是使用动态编程范式来解决这个问题。在这个问题中,有一个限制,只能在路径中向左或向下移动,因此对于任何点,路径数等于左侧点的路径数和右侧点的路径数之和通过这种方式,我们以自下而上的方式从 (0, 0) 到 (N, M) 计算每个点的路径。这个问题中减少空间的关键观察是计算一个点的解,比如 (i, j) 我们只需要 (i-1, j) 和 (i, j-1) 的值,也就是说我们不需要’一旦使用,就不需要 (i-1, j-1) 计算值的部分。
下面是上述方法的实现:

C++
// C++ implementation to count the
// paths from a points to origin
 
#include 
#include
 
using namespace std;
 
// Function to count the paths from
// a point to the origin
long Count_Paths(int x,int y)
{
    // Base Case when the point
    // is already at origin
    if(x == 0 && y == 0)
        return 0;
     
    // Base Case when the point
    // is on the x or y-axis
    if(x == 0 || y == 0)
        return 1;
     
    long dp[max(x,y)+1],
     p=max(x,y),q=min(x,y);
     
    // Loop to fill all the
    // position as 1 in the array
    for(int i=0;i<=p;i++)
        dp[i]=1;
     
    // Loop to count the number of
    // paths from a point to origin
    // in bottom-up manner
    for(int i=1;i<=q;i++)
        for(int j=1;j<=p;j++)
            dp[j]+=dp[j-1];
     
    return dp[p];
}
 
// Driver Code
int main()
{
    int x = 3,y = 3;
     
    // Function Call
    cout<< "Number of Paths "
        << Count_Paths(x,y);
    return 0;
}


Java
// Java implementation to count the
// paths from a points to origin
 
 
class GFG {
 
    // Function to count the paths from
    // a point to the origin
    static long Count_Paths(int x, int y) {
        // Base Case when the point
        // is already at origin
        if (x == 0 && y == 0)
            return 0;
 
        // Base Case when the point
        // is on the x or y-axis
        if (x == 0 || y == 0)
            return 1;
 
        int[] dp = new int[Math.max(x, y) + 1];
        int p = Math.max(x, y), q = Math.min(x, y);
 
        // Loop to fill all the
        // position as 1 in the array
        for (int i = 0; i <= p; i++)
            dp[i] = 1;
 
        // Loop to count the number of
        // paths from a point to origin
        // in bottom-up manner
        for (int i = 1; i <= q; i++)
            for (int j = 1; j <= p; j++)
                dp[j] += dp[j - 1];
 
        return dp[p];
    }
 
    // Driver Code
    public static void main(String[] args) {
        int x = 3, y = 3;
 
        // Function Call
        System.out.print("Number of Paths " + Count_Paths(x, y));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 implementation to count the
# paths from a points to origin
 
# Function to count the paths from
# a point to the origin
def Count_Paths(x, y):
     
    # Base Case when the point
    # is already at origin
    if (x == 0 and y == 0):
        return 0
     
    # Base Case when the point
    # is on the x or y-axis
    if (x == 0 and y == 0):
        return 1
     
    dp = [0] * (max(x, y) + 1)
     
    p = max(x, y)
    q = min(x, y)
     
    # Loop to fill all the
    # position as 1 in the array
    for i in range(p + 1):
        dp[i] = 1
     
    # Loop to count the number of
    # paths from a point to origin
    # in bottom-up manner
    for i in range(1, q + 1):
        for j in range(1, p + 1):
            dp[j] += dp[j - 1]
     
    return dp[p]
 
# Driver Code
x = 3
y = 3
     
# Function call
print("Number of Paths ", Count_Paths(x, y))
 
# This code is contributed by sanjoy_62


C#
// C# implementation to count the
// paths from a points to origin
using System;
 
class GFG {
  
    // Function to count the paths from
    // a point to the origin
    static long Count_Paths(int x, int y) {
        // Base Case when the point
        // is already at origin
        if (x == 0 && y == 0)
            return 0;
  
        // Base Case when the point
        // is on the x or y-axis
        if (x == 0 || y == 0)
            return 1;
  
        int[] dp = new int[Math.Max(x, y) + 1];
        int p = Math.Max(x, y), q = Math.Min(x, y);
  
        // Loop to fill all the
        // position as 1 in the array
        for (int i = 0; i <= p; i++)
            dp[i] = 1;
  
        // Loop to count the number of
        // paths from a point to origin
        // in bottom-up manner
        for (int i = 1; i <= q; i++)
            for (int j = 1; j <= p; j++)
                dp[j] += dp[j - 1];
  
        return dp[p];
    }
  
    // Driver Code
    public static void Main(String[] args) {
        int x = 3, y = 3;
  
        // Function Call
        Console.Write("Number of Paths " + Count_Paths(x, y));
    }
}
 
// This code is contributed by sapnasingh4991


输出:

Number of Paths  20

性能分析:

  • 时间复杂度:在上面给出的方法中,有两个循环在最坏的情况下需要 O(N*M) 时间。因此,这种方法的时间复杂度为O(N*M)
  • 空间复杂度:在上面给出的方法中,有一个大小最大值为 N 和 M 的数组。因此上述方法的空间复杂度为O(max(N, M))

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程