给定一个整数X和整数N ,任务是在N 元树中找到从根开始的唯一路径的数量,使得所有这些路径的总和等于X 。 N元树满足以下条件:
- 所有节点都有N个子节点,每条边的权重都不同,并且位于[1, N]范围内。
- 树被扩展到无穷大。
例子:
Input: N = 3, X = 2
Output: 2
Explanation: the two paths having path sum equal to 2 are {1, 1} and {2}.
Input: N = 3, X = 6
Output: 24
朴素的方法:最简单的方法是递归地找到所有可能的方法来获得等于X 的路径和并打印获得的计数。
时间复杂度: O(N * X)
辅助空间: O(1)
高效的方法:为了优化上述方法,思想是使用动态规划。请按照以下步骤解决问题:
- 初始化一个dp[]数组,该数组对于每个第i个索引,存储与i相加的路径数。
- 对于每个顶点,从strong>1 迭代到min(X, N),即其子节点的所有可能值,并从考虑的每个节点找到给定总和的可能路径数。
- 添加使用边1到N制作的所有路径,并检查是否已经计算了计数。如果已经计算,返回值。否则,通过考虑从当前顶点扩展树的所有可能方式,递归计算总和等于当前值的路径数。
- 更新dp[]数组并返回获得的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define ll long long
using namespace std;
const int mod = (int)1e9 + 7;
// Function for counting total
// no of paths possible with
// the sum is equal to X
ll findTotalPath(int X, int n,
vector& dp)
{
// If the path of the sum
// from the root to current
// node is stored in sum
if (X == 0) {
return 1;
}
ll ans = 0;
// If already commputed
if (dp[X] != -1) {
return dp[X];
}
// Count different no of paths
// using all possible ways
for (int i = 1; i <= min(X, n); ++i) {
ans += findTotalPath(
X - i, n, dp)
% mod;
ans %= mod;
}
// Return total no of paths
return dp[X] = ans;
}
// Driver Code
int main()
{
int n = 3, X = 2;
// Stores the number of ways
// to obtains sums 0 to X
vector dp(X + 1, -1);
// Function call
cout << findTotalPath(X, n, dp);
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
static int mod = (int)1e9 + 7;
// Function for counting total
// no of paths possible with
// the sum is equal to X
static int findTotalPath(int X, int n,
ArrayList dp)
{
// If the path of the sum
// from the root to current
// node is stored in sum
if (X == 0)
{
return 1;
}
int ans = 0;
// If already commputed
if (dp.get(X) != -1)
{
return dp.get(X);
}
// Count different no of paths
// using all possible ways
for(int i = 1; i <= Math.min(X, n); ++i)
{
ans += findTotalPath(X - i, n, dp) % mod;
ans %= mod;
}
// Return total no of paths
dp.set(X, ans);
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 3, X = 2;
// Stores the number of ways
// to obtains sums 0 to X
ArrayList dp = new ArrayList(
Collections.nCopies(X + 1, -1));
// Function call
System.out.print(findTotalPath(X, n, dp));
}
}
// This code is contributed by akhilsaini
Python3
# Python3 program for the above approach
mod = int(1e9 + 7)
# Function for counting total
# no of paths possible with
# the sum is equal to X
def findTotalPath(X, n, dp):
# If the path of the sum
# from the root to current
# node is stored in sum
if (X == 0):
return 1
ans = 0
# If already commputed
if (dp[X] != -1):
return dp[X]
# Count different no of paths
# using all possible ways
for i in range(1, min(X, n) + 1):
ans = ans + findTotalPath(X - i, n, dp) % mod;
ans %= mod;
# Return total no of paths
dp[X] = ans
return ans
# Driver Code
if __name__ == '__main__':
n = 3
X = 2
# Stores the number of ways
# to obtains sums 0 to X
dp = [-1] * (X + 1)
# Function call
print(findTotalPath(X, n, dp))
# This code is contributed by akhilsaini
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int mod = (int)1e9 + 7;
// Function for counting total
// no of paths possible with
// the sum is equal to X
static int findTotalPath(int X, int n, int[] dp)
{
// If the path of the sum
// from the root to current
// node is stored in sum
if (X == 0)
{
return 1;
}
int ans = 0;
// If already commputed
if (dp[X] != -1)
{
return dp[X];
}
// Count different no of paths
// using all possible ways
for(int i = 1; i <= Math.Min(X, n); ++i)
{
ans += findTotalPath(X - i, n, dp) % mod;
ans %= mod;
}
// Return total no of paths
dp[X] = ans;
return ans;
}
// Driver Code
public static void Main()
{
int n = 3, X = 2;
// Stores the number of ways
// to obtains sums 0 to X
int[] dp = new int[X + 1];
Array.Fill(dp, -1);
// Function call
Console.WriteLine(findTotalPath(X, n, dp));
}
}
// This code is contributed by akhilsaini
Javascript
输出:
2
时间复杂度: O(min (N, X))
辅助空间: O(X)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。