Dada una array M de orden N*M donde Mij representa la ocurrencia de j en la fila i. La tarea es encontrar la probabilidad de ocurrencia de k dada en la última fila después de aplicar la operación dada:
- A partir de la primera fila, seleccionamos un elemento de cualquier columna de toda la fila y lo agregamos a la misma columna de la siguiente fila. Repetimos esto hasta la última fila.
Ejemplos:
Input: k = 1, M[][] = {{0, 1}, {1, 1}} Output:0.666667 Input: k = 1, M[][] = {{0, 1}, {1, 1}} Output: 0.333333
Acercarse :
- Calcule previamente una array sum[] que almacena la suma de todos los elementos de la primera fila.
- Rellene la primera fila de dp[][] por la primera fila de elementos M[][].
- Calcule la probabilidad de seleccionar un elemento de cada columna de una fila en particular y agregue lo mismo a la columna correspondiente.
- Además, actualice Sum[] de la fila mientras actualiza el valor de la array dp[][].
- Finalmente, el valor de dp[n][k] para k dado es el valor requerido para la probabilidad de seleccionar el elemento k después de todas las iteraciones.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> #define n 4 #define m 4 using namespace std; // Function to calculate probability float calcProbability(int M[][m], int k) { // declare dp[][] and sum[] float dp[m][n], sum[n]; // precalculate the first row for (int j = 0; j < n; j++) { dp[0][j] = M[0][j]; sum[0] += dp[0][j]; } // calculate the probability for // each element and update dp table for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] += dp[i - 1][j] / sum[i - 1] + M[i][j]; sum[i] += dp[i][j]; } } // return result return dp[n - 1][k - 1] / sum[n - 1]; } // Driver code int main() { int M[m][n] = { { 1, 1, 0, 3 }, { 2, 3, 2, 3 }, { 9, 3, 0, 2 }, { 2, 3, 2, 2 } }; int k = 3; cout << calcProbability(M, k); return 0; }
Java
// Java implementation of the above approach class GFG { final static int n = 4 ; final static int m = 4 ; // Function to calculate probability static float calcProbability(int M[][], int k) { // declare dp[][] and sum[] float dp[][] = new float[m][n] ; float sum[] = new float[n]; // precalculate the first row for (int j = 0; j < n; j++) { dp[0][j] = M[0][j]; sum[0] = sum[0] + dp[0][j]; } // calculate the probability for // each element and update dp table for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i][j] += dp[i - 1][j] / sum[i - 1] + M[i][j]; sum[i] += dp[i][j]; } } // return result return dp[n - 1][k - 1] / sum[n - 1]; } // Driver code public static void main(String []args) { int M[][] = { { 1, 1, 0, 3 }, { 2, 3, 2, 3 }, { 9, 3, 0, 2 }, { 2, 3, 2, 2 } }; int k = 3; System.out.println(calcProbability(M, k)); } } // This code is contributed by Ryuga
Python3
# Python3 implementation of the # above approach n = 4 m = 4 # Function to calculate probability def calcProbability(M, k): # declare dp[][] and sum[] dp = [[0 for i in range(n)] for i in range(m)] Sum = [0 for i in range(n)] # precalculate the first row for j in range(n): dp[0][j] = M[0][j] Sum[0] += dp[0][j] # calculate the probability for # each element and update dp table for i in range(1, m): for j in range(n): dp[i][j] += (dp[i - 1][j] / Sum[i - 1] + M[i][j]) Sum[i] += dp[i][j] # return result return dp[n - 1][k - 1] / Sum[n - 1] # Driver code M = [[ 1, 1, 0, 3 ], [ 2, 3, 2, 3 ], [ 9, 3, 0, 2 ], [ 2, 3, 2, 2 ]] k = 3 print(calcProbability(M, k)) # This code is contributed # by mohit kumar 29
C#
// C# implementation of the above approach using System; class GFG { static int n = 4 ; static int m = 4 ; // Function to calculate probability static float calcProbability(int[,] M, int k) { // declare dp[][] and sum[] float[,] dp = new float[m,n] ; float[] sum = new float[n]; // precalculate the first row for (int j = 0; j < n; j++) { dp[0, j] = M[0, j]; sum[0] = sum[0] + dp[0, j]; } // calculate the probability for // each element and update dp table for (int i = 1; i < m; i++) { for (int j = 0; j < n; j++) { dp[i, j] += dp[i - 1,j] / sum[i - 1] + M[i, j]; sum[i] += dp[i, j]; } } // return result return dp[n - 1,k - 1] / sum[n - 1]; } // Driver code public static void Main() { int[,] M = { { 1, 1, 0, 3 }, { 2, 3, 2, 3 }, { 9, 3, 0, 2 }, { 2, 3, 2, 2 } }; int k = 3; Console.Write(calcProbability(M, k)); } } // This code is contributed by Ita_c.
PHP
<?php // PHP implementation of the above approach // Function to calculate probability function calcProbability($M, $k) { $m = 4; $n = 4; // declare dp[][] and sum[] $dp = array(); $sum = array(); // precalculate the first row for ($j = 0; $j < $n; $j++) { $dp[0][$j] = $M[0][$j]; $sum[0] += $dp[0][$j]; } // calculate the probability for // each element and update dp table for ($i = 1; $i < $m; $i++) { for ($j = 0; $j <$n; $j++) { $dp[$i][$j] += $dp[$i - 1][$j] / $sum[$i - 1] + $M[$i][$j]; $sum[$i] += $dp[$i][$j]; } } // return result return $dp[$n - 1][$k - 1] / $sum[$n - 1]; } // Driver code $M = array(array(1, 1, 0, 3), array(2, 3, 2, 3), array(9, 3, 0, 2), array(2, 3, 2, 2)); $k = 3; echo calcProbability($M, $k); // This code is contributed // by Arnub Kundu ?>
Javascript
<script> // Javascript implementation of the above approach let n = 4 ; let m = 4 ; // Function to calculate probability function calcProbability(M,k) { // declare dp[][] and sum[] let dp = new Array(m) ; let sum = new Array(n); for(let i = 0; i < dp.length; i++) { dp[i] = new Array(n); for(let j = 0; j < n; j++) { dp[i][j] = 0; } } for(let i = 0; i < n; i++) { sum[i] = 0; } // precalculate the first row for (let j = 0; j < n; j++) { dp[0][j] = M[0][j]; sum[0] = sum[0] + dp[0][j]; } // calculate the probability for // each element and update dp table for (let i = 1; i < m; i++) { for (let j = 0; j < n; j++) { dp[i][j] += dp[i - 1][j] / sum[i - 1] + M[i][j]; sum[i] += dp[i][j]; } } // return result return dp[n - 1][k - 1] / sum[n - 1]; } // Driver code let M = [[ 1, 1, 0, 3 ], [ 2, 3, 2, 3 ], [ 9, 3, 0, 2 ], [ 2, 3, 2, 2 ]]; let k = 3; document.write(calcProbability(M, k)); // This code is contributed by rag2127 </script>
Producción:
0.201212
Publicación traducida automáticamente
Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA