Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | Function to find the maximum score possible ; Base Case ; If previously occurred subproblem occurred ; Check if lastpicked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum score from the two possibilities ; Function to prlet maximum score ; DP array to store results ; Initialise dp with - 1 ; Function call ; Given arrays ; Function Call
function maximumSum ( a , b , n , index , lastpicked , dp ) { if ( index == n ) return 0 ; if ( dp [ index ] [ lastpicked + 1 ] != - 1 ) return dp [ index ] [ lastpicked + 1 ] ; let option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) { option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index , dp ) ; } option2 = maximumSum ( a , b , n , index + 1 , lastpicked , dp ) ; return dp [ index ] [ lastpicked + 1 ] = Math . max ( option1 , option2 ) ; } function maximumPolets ( arr , brr , n ) { let index = 0 , lastPicked = - 1 ; let dp = new Array ( n + 5 ) ; for ( var i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < dp . length ; i ++ ) { for ( var j = 0 ; j < dp . length ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } document . write ( maximumSum ( arr , brr , n , index , lastPicked , dp ) ) ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 1 ] ; let brr = [ - 1 , 2 , 10 , 20 , - 10 , - 9 ] ; let N = arr . length ; maximumPolets ( arr , brr , N ) ;