Maximum score possible from an array with jumps of at most length K | Function to calculate maximum score possible from the array A [ ] ; Stores the score of previous k indices ; Stores the maximum score for current index ; Maximum score at first index ; Traverse the array to calculate maximum score for all indices ; Remove maximum scores for indices less than i - K ; Calculate maximum score for current index ; Push maximum score of current index along with index in maxheap ; Return the maximum score ; Driver Code ; Function call to calculate maximum score from the array A [ ]
function maxScore ( A , K , N ) { var maxheap = [ ] ; var maxScore = 0 ; maxheap . push ( [ A [ 0 ] , 0 ] ) ; for ( var i = 1 ; i < N ; i ++ ) { while ( maxheap [ maxheap . length - 1 ] [ 1 ] < ( i - K ) ) { maxheap . pop ( ) ; } maxScore = A [ i ] + maxheap [ maxheap . length - 1 ] [ 0 ] ; maxheap . push ( [ maxScore , i ] ) ; maxheap . sort ( ) ; } return maxScore ; } var A = [ - 44 , - 17 , - 54 , 79 ] ; var K = 2 ; var N = A . length ; document . write ( maxScore ( A , K , N ) ) ;