📌  相关文章
📜  非递减数组的计数 arr3[] 使得 arr1[i] <= arr3[i] <= arr2[i]

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

非递减数组的计数 arr3[] 使得 arr1[i] <= arr3[i] <= arr2[i]

给定两个数组arr1[]arr2[]N个非递减顺序的整数,任务是找到长度为N的非递减数组arr3[]的计数,使得arr1[i] <= arr3[i] < = arr2[i]对于[0, N)范围内的i的所有值。

例子

方法:给定的问题可以使用动态规划来解决。考虑一个二维数组dp[][]使得dp[i][j]表示长度为i的数组的计数,使得第i元素是j 。将 dp 数组的所有元素初始化为0 ,将dp[0][0] 初始化为 1 。经观察,上述问题的DP关系可表述如下:

因此,使用上述关系,计算[0, N]范围内的每个i[0, M]范围内的每个jdp[i][j]的值,其中M表示两个范围内的最大整数给定数组arr1[]arr2[] 。因此,存储在dp[N][M]中的值就是所需的答案。

下面是上述方法的实现:

C++
// C++ Program of the above approach
#include 
using namespace std;
 
// Function to find the count of
// valid sorted arrays
int arrCount(int arr1[], int arr2[], int N)
{
 
    // Maximum possible value
    // of arr1 and arr2
    int M = 1000;
 
    // Stores the dp states
    vector > dp(
        N + 1,
        vector(M + 1, 0));
 
    // Initial condition
    dp[0][0] = 1;
 
    // Loop to iterate over range [0, N]
    for (int i = 0; i <= N; i++) {
 
        // Loop to iterate over
        // the range [0, M]
        for (int j = 0; j < M; j++) {
            dp[i][j + 1] += dp[i][j];
        }
 
        // If current index is not
        // the final index
        if (i != N) {
 
            // Loop to iterate in the
            // range [arr1[i], arr2[i]]
            for (int j = arr1[i]; j <= arr2[i]; j++)
                dp[i + 1][j] += dp[i][j];
        }
    }
 
    // Return Answer
    return dp[N][M];
}
 
// Driver Code
int main()
{
    int arr1[] = { 1, 1 };
    int arr2[] = { 2, 3 };
    int N = sizeof(arr1) / sizeof(int);
 
    cout << arrCount(arr1, arr2, N);
 
    return 0;
}


Java
// Java Program of the above approach
import java.util.*;
 
public class GFG{
     
// Function to find the count of
// valid sorted arrays
static int arrCount(int[] arr1, int[] arr2, int N)
{
     
    // Maximum possible value
    // of arr1 and arr2
    int M = 1000;
 
    // Stores the dp states
    int[][] dp = new int[N + 1][M + 1];
 
    // Initial condition
    dp[0][0] = 1;
 
    // Loop to iterate over range [0, N]
    for(int i = 0; i <= N; i++)
    {
         
        // Loop to iterate over
        // the range [0, M]
        for(int j = 0; j < M; j++)
        {
            dp[i][j + 1] += dp[i][j];
        }
 
        // If current index is not
        // the final index
        if (i != N)
        {
             
            // Loop to iterate in the
            // range [arr1[i], arr2[i]]
            for(int j = arr1[i]; j <= arr2[i]; j++)
                dp[i + 1][j] += dp[i][j];
        }
    }
 
    // Return Answer
    return dp[N][M];
}
 
// Driver Code
public static void main(String args[])
{
    int[] arr1 = { 1, 1 };
    int[] arr2 = { 2, 3 };
    int N = arr1.length;
 
    System.out.println(arrCount(arr1, arr2, N));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python Program to implement
# the above approach
 
# Function to find the count of
# valid sorted arrays
def arrCount(arr1, arr2, N):
 
    # Maximum possible value
    # of arr1 and arr2
    M = 1000
 
    # Stores the dp states
    dp = [0] * (N + 1)
    for i in range(len(dp)):
        dp[i] = [0] * (M + 1)
 
    # Initial condition
    dp[0][0] = 1
 
    # Loop to iterate over range [0, N]
    for i in range(N + 1):
 
        # Loop to iterate over
        # the range [0, M]
        for j in range(M):
            dp[i][j + 1] += dp[i][j]
 
        # If current index is not
        # the final index
        if (i != N):
 
            # Loop to iterate in the
            # range [arr1[i], arr2[i]]
            for j in range(arr1[i], arr2[i] + 1):
                dp[i + 1][j] += dp[i][j]
 
    # Return Answer
    return dp[N][M]
 
# Driver Code
arr1 = [1, 1]
arr2 = [2, 3]
N = len(arr1)
 
print(arrCount(arr1, arr2, N))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# Program of the above approach
using System;
 
class GFG{
     
// Function to find the count of
// valid sorted arrays
static int arrCount(int[] arr1, int[] arr2, int N)
{
     
    // Maximum possible value
    // of arr1 and arr2
    int M = 1000;
 
    // Stores the dp states
    int[,] dp = new int[N + 1, M + 1];
 
    // Initial condition
    dp[0, 0] = 1;
 
    // Loop to iterate over range [0, N]
    for(int i = 0; i <= N; i++)
    {
         
        // Loop to iterate over
        // the range [0, M]
        for(int j = 0; j < M; j++)
        {
            dp[i, j + 1] += dp[i, j];
        }
 
        // If current index is not
        // the final index
        if (i != N)
        {
             
            // Loop to iterate in the
            // range [arr1[i], arr2[i]]
            for(int j = arr1[i]; j <= arr2[i]; j++)
                dp[i + 1, j] += dp[i, j];
        }
    }
 
    // Return Answer
    return dp[N, M];
}
 
// Driver Code
public static void Main()
{
    int[] arr1 = { 1, 1 };
    int[] arr2 = { 2, 3 };
    int N = arr1.Length;
 
    Console.WriteLine(arrCount(arr1, arr2, N));
}
}
 
// This code is contributed by ukasp


Javascript


输出
5

时间复杂度: O(N * M),其中 M 表示数组 arr1[] 和 arr2[] 中整数的最大值。
辅助空间: O(N * M)