📌  相关文章
📜  最多为 K 个数字的所有有效组合的计数,总和为 N

📅  最后修改于: 2022-05-13 01:56:09.774000             🧑  作者: Mango

最多为 K 个数字的所有有效组合的计数,总和为 N

给定两个数字NK ,任务是找到最多为 K个数字的所有有效组合的计数,这些数字的总和为N使得以下条件为真:

  • 仅使用数字 1 到 9。
  • 每个号码最多使用一次。

例子:

朴素方法:这个想法是创建一个从 1 到 9 的数字数组,并找到长度最多为 K 且总和为 N 的所有子序列的计数。

时间复杂度: O(10^2)
辅助空间: O(1)

递归方法:该问题也可以使用递归解决,如下所示:

  • 在 arr 中创建一个 1-9 的数字数组。
  • 创建递归函数,以当前索引为i 、当前总和为sum 、当前计数为c以及组合的结果计数为ans来迭代数组。
  • 基本情况 1:如果 (sum == n && c <= k)
    • 增加组合的结果计数
    • 返回答案
  • 基本情况 2:如果 (i >= arr.size() || sum > n || c > k)
    • 返回答案
  • 别的
    • 将当前数组元素推入临时向量
    • 调用递归函数
    • 从向量中弹出当前元素
    • 调用递归函数

下面是上述方法的实现:

C++
// C++ code to solve the above problem
 
#include 
#include 
#include 
#include 
#include 
using namespace std;
 
// Recursive program to find count of
// all combinations of at most K
// digits with sum N
int rec(vector& arr, int i,
        int k, int c, int n,
        int& ans, int sum)
{
 
    // Base case 1
    if (sum == n && c <= k) {
        ans++;
        return ans;
    }
 
    // Base case 2
    if (i >= arr.size()
        || sum > n || c > k)
        return ans;
 
    // Consider arr[i] into current selection
    // and call recursive function
    ans = rec(arr, i + 1, k, c + 1,
              n, ans, sum + arr[i]);
 
    // Do not consider arr[i] into current
    // selection and call recursive function
    ans = rec(arr, i + 1, k, c, n, ans, sum);
 
    return ans;
}
 
// Function to solve the problem
// and print the count of combinations
int combinationSum(int k, int n)
{
 
    vector arr(9, 0);
    for (int i = 1; i <= 9; i++)
        arr[i - 1] = i;
 
    int ans;
 
    // Recursive function call
    ans = rec(arr, 0, k, 0, n, ans, 0);
 
    return ans;
}
// Driver Code
int main()
{
    int N = 9, K = 3;
    cout << combinationSum(K, N) << endl;
 
    return 0;
}


Java
// JAVA code to solve the above problem
import java.util.*;
class GFG
{
 
  // Recursive program to find count of
  // all combinations of at most K
  // digits with sum N
  public static int rec(int[] arr, int i, int k, int c,
                        int n, int ans, int sum)
  {
 
    // Base case 1
    if (sum == n && c <= k) {
      ans++;
      return ans;
    }
 
    // Base case 2
    if (i >= arr.length || sum > n || c > k)
      return ans;
 
    // Consider arr[i] into current selection
    // and call recursive function
    ans = rec(arr, i + 1, k, c + 1, n, ans,
              sum + arr[i]);
 
    // Do not consider arr[i] into current
    // selection and call recursive function
    ans = rec(arr, i + 1, k, c, n, ans, sum);
 
    return ans;
  }
 
  // Function to solve the problem
  // and print the count of combinations
  public static int combinationSum(int k, int n)
  {
 
    int[] arr = new int[9];
    for (int i = 0; i < 9; i++) {
      arr[i] = 0;
    }
    for (int i = 1; i <= 9; i++)
      arr[i - 1] = i;
 
    int ans = 0;
 
    // Recursive function call
    ans = rec(arr, 0, k, 0, n, ans, 0);
 
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 9, K = 3;
    System.out.println(combinationSum(K, N));
  }
}
 
// This code is contributed by Taranpreet


Python3
# python3 code to solve the above problem
 
# Recursive program to find count of
# all combinations of at most K
# digits with sum N
def rec(arr, i, k, c, n, ans, sum):
 
    # Base case 1
    if (sum == n and c <= k):
        ans += 1
        return ans
 
    # Base case 2
    if (i >= len(arr)
            or sum > n or c > k):
        return ans
 
    # Consider arr[i] into current selection
    # and call recursive function
    ans = rec(arr, i + 1, k, c + 1,
              n, ans, sum + arr[i])
 
    # Do not consider arr[i] into current
    # selection and call recursive function
    ans = rec(arr, i + 1, k, c, n, ans, sum)
 
    return ans
 
# Function to solve the problem
# and print the count of combinations
def combinationSum(k, n):
 
    arr = [0 for _ in range(9)]
    for i in range(1, 10):
        arr[i - 1] = i
 
    ans = 0
     
    # Recursive function call
    ans = rec(arr, 0, k, 0, n, ans, 0)
 
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    N, K = 9, 3
    print(combinationSum(K, N))
 
    # This code is contributed by rakeshsahni


C#
// C# code to solve the above problem
using System;
class GFG {
 
  // Recursive program to find count of
  // all combinations of at most K
  // digits with sum N
  static int rec(int[] arr, int i, int k, int c, int n,
                 int ans, int sum)
  {
 
    // Base case 1
    if (sum == n && c <= k) {
      ans++;
      return ans;
    }
 
    // Base case 2
    if (i >= arr.Length || sum > n || c > k)
      return ans;
 
    // Consider arr[i] into current selection
    // and call recursive function
    ans = rec(arr, i + 1, k, c + 1, n, ans,
              sum + arr[i]);
 
    // Do not consider arr[i] into current
    // selection and call recursive function
    ans = rec(arr, i + 1, k, c, n, ans, sum);
 
    return ans;
  }
 
  // Function to solve the problem
  // and print the count of combinations
  static int combinationSum(int k, int n)
  {
 
    int[] arr = new int[9];
    for (int i = 0; i < 9; i++) {
      arr[i] = 0;
    }
    for (int i = 1; i <= 9; i++)
      arr[i - 1] = i;
 
    int ans = 0;
 
    // Recursive function call
    ans = rec(arr, 0, k, 0, n, ans, 0);
 
    return ans;
  }
   
  // Driver Code
  public static int Main()
  {
    int N = 9, K = 3;
    Console.WriteLine(combinationSum(K, N));
    return 0;
  }
}
 
// This code is contributed by Taranpreet


Javascript



输出
8

时间复杂度: O(10^2)
辅助空间: O(10^2)