Dados dos arreglos arr1[] y arr2[] que tienen N enteros en orden no decreciente, la tarea es encontrar el conteo de arreglos no decrecientes arr3[] de longitud N tal que arr1[i] <= arr3[i] < = arr2[i] para todos los valores de i en el rango [0, N) .
Ejemplos :
Entrada : arr1[] = {1, 1}, arr2[] = {2, 3}
Salida : 5
Explicación: Los 5 arreglos posibles que siguen las condiciones requeridas son {1, 1}, {1, 2}, {1 , 3}, {2, 2}, {2, 3}Entrada : rangos[] = {{-12, 15}, {3, 9}, {-5, -2}, {20, 25}, {16, 20}}
Salida : 247
Enfoque: El problema dado se puede resolver usando Programación Dinámica . Considere una array 2D dp[][] tal que dp[i][j] representa el recuento de arrays de longitud i tal que el i -ésimo elemento es j . Inicialice todos los elementos de la array dp como 0 y dp[0][0] como 1 . Tras la observación, la relación DP del problema anterior se puede establecer de la siguiente manera:
pd[i][j] =
Por lo tanto, usando la relación anterior, calcule el valor de dp[i][j] para cada i en el rango [0, N] y para cada j en el rango [0, M] donde M representa el número entero máximo tanto en el arrays dadas arr1[] y arr2[] . Por lo tanto, el valor almacenado en dp[N][M] es la respuesta requerida.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program of the above approach #include <bits/stdc++.h> 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<vector<int> > dp( N + 1, vector<int>(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
<script> // JavaScript Program to implement // the above approach // Function to find the count of // valid sorted arrays function arrCount(arr1, arr2, N) { // Maximum possible value // of arr1 and arr2 let M = 1000; // Stores the dp states let dp = new Array(N + 1); for (let i = 0; i < dp.length; i++) { dp[i] = new Array(M + 1).fill(0); } // Initial condition dp[0][0] = 1; // Loop to iterate over range [0, N] for (let i = 0; i <= N; i++) { // Loop to iterate over // the range [0, M] for (let 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 (let j = arr1[i]; j <= arr2[i]; j++) dp[i + 1][j] += dp[i][j]; } } // Return Answer return dp[N][M]; } // Driver Code let arr1 = [1, 1]; let arr2 = [2, 3]; let N = arr1.length; document.write(arrCount(arr1, arr2, N)); // This code is contributed by Potta Lokesh </script>
5
Complejidad de tiempo : O(N * M), donde M representa el valor máximo de los enteros en la array arr1[] y arr2[].
Espacio Auxiliar: O(N * M)
Publicación traducida automáticamente
Artículo escrito por apratyush41 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA