长度为 N 且至少有 X 0 和 Y 1 的二进制字符串的计数
给定三个数字N、X和Y ,找出长度为N且至少包含X 0和Y 1的唯一二进制字符串的计数。
例子:
Input: N=5, X=1, Y=2
Output: 25
Input: N=3, X=1, Y=1
Output: 6
Explanation: There are 3 binary strings of length 3 with at least 1 0s and 1 1s, such as: 001, 010, 100, 011, 101, 110
天真的方法:生成所有长度为N的二进制字符串,然后计算至少具有X 0和Y 1的字符串的数量。
时间复杂度: O(2^N)
辅助空间: O(1)
更好的方法:这个问题也可以使用组合数学来解决。如果长度是N ,并且给定的是X 0s ,那么将有Y (=NX) 1s 。所以我们需要为此找到唯一组合的数量,可以得到(N)C(X)或(N)C(Y)。现在对于所有唯一的二进制字符串,我们需要找到[X, NY]范围内i的值的nCi并将其添加到变量中。在所有迭代之后这个总和的值将是所需的计数。
高效方法:上述方法可以在帕斯卡三角形的帮助下进一步优化以计算nCr 。 请按照以下步骤解决问题:
- 初始化二维数组p[][]以使用帕斯卡三角形进行计算。
- 将变量sum初始化为0以存储答案。
- 使用变量i遍历范围[x, ny]并执行以下任务:
- 将值p[n][i]添加到变量sum 。
- 执行上述步骤后,打印sum的值作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
long long int p[31][31];
// Function to use pascal triangle
void pascalTriangle()
{
p[0][0] = 1;
p[1][0] = 1;
p[1][1] = 1;
for (int i = 2; i < 31; i++) {
p[i][0] = 1;
for (int j = 1; j < i; j++)
p[i][j] = p[i - 1][j]
+ p[i - 1][j - 1];
p[i][i] = 1;
}
}
// Function to count the total number of ways
long long int countWays(int n, int x, int y)
{
// Store the answer
long long int sum = 0;
// Traverse
for (long long int i = x; i <= n - y; i++) {
sum += p[n][i];
}
return sum;
}
// Driver Code
int main()
{
pascalTriangle();
int N = 5, X = 1, Y = 2;
cout << countWays(N, X, Y) << endl;
return 0;
}
Java
// Java code for the above approach
import java.io.*;
class GFG {
static int[][] p = new int[31][31];
// Function to use pascal triangle
static void pascalTriangle()
{
p[0][0] = 1;
p[1][0] = 1;
p[1][1] = 1;
for (int i = 2; i < 31; i++) {
p[i][0] = 1;
for (int j = 1; j < i; j++)
p[i][j] = p[i - 1][j] + p[i - 1][j - 1];
p[i][i] = 1;
}
}
// Function to count the total number of ways
static long countWays(int n, int x, int y)
{
// Store the answer
long sum = 0;
// Traverse
for (int i = x; i <= n - y; i++) {
sum += p[n][i];
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
pascalTriangle();
int N = 5;
int X = 1;
int Y = 2;
System.out.println(countWays(N, X, Y));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python program for the above approach
p = [[0 for i in range(31)] for i in range(31)]
# Function to use pascal triangle
def pascalTriangle():
p[0][0] = 1
p[1][0] = 1
p[1][1] = 1
for i in range(2, 31):
p[i][0] = 1
for j in range(1, i):
p[i][j] = p[i - 1][j] + p[i - 1][j - 1]
p[i][i] = 1
# Function to count the total number of ways
def countWays(n, x, y):
# Store the answer
sum = 0
# Traverse
for i in range(x, n - y + 1):
sum += p[n][i]
return sum
# Driver Code
pascalTriangle()
N = 5
X = 1
Y = 2
print(countWays(N, X, Y))
# This code is contributed by gfgking
C#
// C# code for the above approach
using System;
public class GFG {
static int[,] p = new int[31,31];
// Function to use pascal triangle
static void pascalTriangle()
{
p[0,0] = 1;
p[1,0] = 1;
p[1,1] = 1;
for (int i = 2; i < 31; i++) {
p[i,0] = 1;
for (int j = 1; j < i; j++)
p[i,j] = p[i - 1,j] + p[i - 1,j - 1];
p[i,i] = 1;
}
}
// Function to count the total number of ways
static long countWays(int n, int x, int y)
{
// Store the answer
long sum = 0;
// Traverse
for (int i = x; i <= n - y; i++) {
sum += p[n,i];
}
return sum;
}
// Driver Code
public static void Main(String[] args)
{
pascalTriangle();
int N = 5;
int X = 1;
int Y = 2;
Console.WriteLine(countWays(N, X, Y));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
25
时间复杂度: O(N)
辅助空间: O(1)