在 [A[i], B[i]] 范围内具有第 i 个元素的非递减数组的计数
给定两个由N个整数组成的数组A[]和B[] ,任务是找到可以形成的大小为N的非递减数组的数量,使得每个数组元素位于[A[i] 范围内,乙[i]] 。
例子:
Input: A[] = {1, 1}, B[] = {2, 3}
Output: 5
Explanation:
The total number of valid arrays are {1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}. Therefore, the count of such arrays is 5.
Input: A[] = {3, 4, 5}, B[] = {4, 5, 6}
Output: 8
方法:给定的问题可以使用动态规划和前缀和来解决。请按照以下步骤解决问题:
- 用值0初始化一个二维数组dp[][] ,其中dp[i][j]表示直到位置i的总有效数组,当前元素为j 。将dp[0][0]初始化为1 。
- 用值0初始化一个二维数组pref[][]以存储数组的前缀和。
- 使用变量i迭代范围[0, B[N – 1]]并将pref[0][i]的值设置为1 。
- 使用变量i迭代范围[1, N]并执行以下步骤:
- 使用变量j迭代范围[A[i – 1], B[i – 1]]并将dp[i][j]的值增加pref[i – 1][j]并增加pref[i][j]由dp[i][j] 。
- 使用变量j在范围[0, B[N – 1]]上进行迭代,如果j大于0 ,则通过将pref[i][j] 的值增加 pref[i][j来更新前缀和表– 1] 。
- 将变量ans初始化为0以存储形成的数组的结果计数。
- 使用变量i遍历范围[A[N – 1], B[N – 1]]并将dp[N][i]的值添加到变量ans中。
- 执行上述步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the total number
// of possible valid arrays
int totalValidArrays(int a[], int b[],
int N)
{
// Make a 2D DP table
int dp[N + 1][b[N - 1] + 1];
// Make a 2D prefix sum table
int pref[N + 1][b[N - 1] + 1];
// Initialize all values to 0
memset(dp, 0, sizeof(dp)),
memset(pref, 0, sizeof(pref));
// Base Case
dp[0][0] = 1;
// Initialize the prefix values
for (int i = 0; i <= b[N - 1]; i++) {
pref[0][i] = 1;
}
// Iterate over the range and update
// the dp table accordingly
for (int i = 1; i <= N; i++) {
for (int j = a[i - 1];
j <= b[i - 1]; j++) {
dp[i][j] += pref[i - 1][j];
// Add the dp values to the
// prefix sum
pref[i][j] += dp[i][j];
}
// Update the prefix sum table
for (int j = 0; j <= b[N - 1]; j++) {
if (j > 0) {
pref[i][j] += pref[i][j - 1];
}
}
}
// Find the result count of
// arrays formed
int ans = 0;
for (int i = a[N - 1];
i <= b[N - 1]; i++) {
ans += dp[N][i];
}
// Return the total count of arrays
return ans;
}
// Driver Code
int main()
{
int A[] = { 1, 1 };
int B[] = { 2, 3 };
int N = sizeof(A) / sizeof(A[0]);
cout << totalValidArrays(A, B, N);
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to count the total number
// of possible valid arrays
static int totalValidArrays(int a[], int b[],
int N)
{
// Make a 2D DP table
int dp[][] = new int[N + 1][b[N - 1] + 1];
// Make a 2D prefix sum table
int pref[][] = new int[N + 1][b[N - 1] + 1];
// Initialize all values to 0
for (int i = 0; i < N + 1; i++)
for (int j = 0; j < b[N - 1] + 1; j++)
dp[i][j] = 0;
for (int i = 0; i < N + 1; i++)
for (int j = 0; j < b[N - 1] + 1; j++)
pref[i][j] = 0;
// Base Case
dp[0][0] = 1;
// Initialize the prefix values
for (int i = 0; i <= b[N - 1]; i++) {
pref[0][i] = 1;
}
// Iterate over the range and update
// the dp table accordingly
for (int i = 1; i <= N; i++) {
for (int j = a[i - 1];
j <= b[i - 1]; j++) {
dp[i][j] += pref[i - 1][j];
// Add the dp values to the
// prefix sum
pref[i][j] += dp[i][j];
}
// Update the prefix sum table
for (int j = 0; j <= b[N - 1]; j++) {
if (j > 0) {
pref[i][j] += pref[i][j - 1];
}
}
}
// Find the result count of
// arrays formed
int ans = 0;
for (int i = a[N - 1];
i <= b[N - 1]; i++) {
ans += dp[N][i];
}
// Return the total count of arrays
return ans;
}
// Driver Code
public static void main (String[] args)
{
int A[] = { 1, 1 };
int B[] = { 2, 3 };
int N = A.length;
System.out.println(totalValidArrays(A, B, N));
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
# Function to count the total number
# of possible valid arrays
def totalValidArrays(a, b, N):
# Make a 2D DP table
dp = [[0 for _ in range(b[N - 1] + 1)] for _ in range(N + 1)]
# Make a 2D prefix sum table
pref = [[0 for _ in range(b[N - 1] + 1)] for _ in range(N + 1)]
# Base Case
dp[0][0] = 1
# Initialize the prefix values
for i in range(0, b[N - 1] + 1):
pref[0][i] = 1
# Iterate over the range and update
# the dp table accordingly
for i in range(1, N + 1):
for j in range(a[i - 1], b[i - 1] + 1):
dp[i][j] += pref[i - 1][j]
# Add the dp values to the
# prefix sum
pref[i][j] += dp[i][j]
# Update the prefix sum table
for j in range(0, b[N - 1] + 1):
if (j > 0):
pref[i][j] += pref[i][j - 1]
# Find the result count of
# arrays formed
ans = 0
for i in range(a[N - 1], b[N - 1] + 1):
ans += dp[N][i]
# Return the total count of arrays
return ans
# Driver Code
if __name__ == "__main__":
A = [1, 1]
B = [2, 3]
N = len(A)
print(totalValidArrays(A, B, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the total number
// of possible valid arrays
static int totalValidArrays(int[] a, int[] b, int N)
{
// Make a 2D DP table
int[,] dp = new int[N + 1, b[N - 1] + 1];
// Make a 2D prefix sum table
int[,] pref = new int[N + 1, b[N - 1] + 1];
// Initialize all values to 0
for (int i = 0; i < N + 1; i++)
for (int j = 0; j < b[N - 1] + 1; j++)
dp[i, j] = 0;
for (int i = 0; i < N + 1; i++)
for (int j = 0; j < b[N - 1] + 1; j++)
pref[i, j] = 0;
// Base Case
dp[0, 0] = 1;
// Initialize the prefix values
for (int i = 0; i <= b[N - 1]; i++) {
pref[0, i] = 1;
}
// Iterate over the range and update
// the dp table accordingly
for (int i = 1; i <= N; i++) {
for (int j = a[i - 1];
j <= b[i - 1]; j++) {
dp[i, j] += pref[i - 1, j];
// Add the dp values to the
// prefix sum
pref[i, j] += dp[i, j];
}
// Update the prefix sum table
for (int j = 0; j <= b[N - 1]; j++) {
if (j > 0) {
pref[i, j] += pref[i, j - 1];
}
}
}
// Find the result count of
// arrays formed
int ans = 0;
for (int i = a[N - 1];
i <= b[N - 1]; i++) {
ans += dp[N, i];
}
// Return the total count of arrays
return ans;
}
// Driver Code
public static void Main ()
{
int[] A = { 1, 1 };
int[] B = { 2, 3 };
int N = A.Length;
Console.WriteLine(totalValidArrays(A, B, N));
}
}
// This code is contributed by Saurabh
Javascript
输出:
5
时间复杂度: O(N*M),其中 M 是数组 B[] 的最后一个元素。
辅助空间: O(N*M),其中 M 是数组 B[] 的最后一个元素。