Find maximum possible value of advertising | Function to find maximum possible advertising value ; To store advertising value at i - th minute ; Base Case ; If no advertisement is taken on ith minute ; If advertisement is taken on i - th minute ; array [ ] [ 0 ] start time array [ ] [ 1 ] advertising value
function max_value ( array , M , K , N ) { var time = Array ( M ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { time [ array [ i ] [ 0 ] ] = array [ i ] [ 1 ] ; } var dp = Array . from ( Array ( M ) , ( ) => Array ( 2 ) ) ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = time [ 0 ] ; for ( var i = 1 ; i < M ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = time [ i ] ; if ( i - K >= 0 ) { dp [ i ] [ 1 ] += Math . max ( dp [ i - K ] [ 0 ] , dp [ i - K ] [ 1 ] ) ; } } return Math . max ( dp [ M - 1 ] [ 0 ] , dp [ M - 1 ] [ 1 ] ) ; } var array = [ [ 0 , 10 ] , [ 4 , 110 ] , [ 5 , 30 ] ] ; var N = 3 ; var K = 4 ; var M = 6 ; document . write ( max_value ( array , M , K , N ) ) ;