Number of ways of scoring R runs in B balls with at most W wickets | javascript implementation of the approach ; Function to return the number of ways to score R runs in B balls with at most W wickets ; If the wickets lost are more ; If runs scored are more ; If condition is met ; If no run got scored ; Already visited state ; If scored 0 run ; If scored 1 run ; If scored 2 runs ; If scored 3 runs ; If scored 4 runs ; If scored 6 runs ; If scored no run and lost a wicket ; Memoize and return ; Driver code
var mod = 1000000007 ; var RUNMAX = 300 ; var BALLMAX = 50 ; var WICKETMAX = 10 ; function CountWays ( r , b , l , R , B , W , dp ) { if ( l > W ) return 0 ; if ( r > R ) return 0 ; if ( b == B && r == R ) return 1 ; if ( b == B ) return 0 ; if ( dp [ r ] [ b ] [ l ] != - 1 ) return dp [ r ] [ b ] [ l ] ; var ans = 0 ; ans += CountWays ( r , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 1 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 2 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 3 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 4 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 6 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r , b + 1 , l + 1 , R , B , W , dp ) ; ans = ans % mod ; return dp [ r ] [ b ] [ l ] = ans ; } var R = 40 , B = 10 , W = 4 ; var dp = Array ( RUNMAX ) . fill ( ) . map ( ( ) => Array ( BALLMAX ) . fill ( ) . map ( ( ) => Array ( WICKETMAX ) . fill ( 0 ) ) ) ; for ( i = 0 ; i < RUNMAX ; i ++ ) for ( j = 0 ; j < BALLMAX ; j ++ ) for ( k = 0 ; k < WICKETMAX ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; document . write ( CountWays ( 0 , 0 , 0 , R , B , W , dp ) ) ;