Maximum score possible from an array with jumps of at most length K | Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A [ ] ; Initialize dp [ ] with - 1 ; Driver Code
function maxScore ( i , A , K , N , dp ) { if ( i >= N - 1 ) return A [ N - 1 ] ; if ( dp [ i ] != - 1 ) return dp [ i ] ; let score = - 1000 ; for ( let j = 1 ; j <= K ; j ++ ) { score = Math . max ( score , maxScore ( i + j , A , K , N , dp ) ) ; } return dp [ i ] = score + A [ i ] ; } function getScore ( A , N , K ) { let dp = new Array ( N ) . fill ( - 1 ) ; document . write ( maxScore ( 0 , A , K , N , dp ) ) ; } let A = [ 100 , - 30 , - 50 , - 15 , - 20 , - 30 ] ; let K = 3 ; let N = A . length ; getScore ( A , N , K ) ;