Dynamic Programming | High | Returns the maximum among the 2 numbers ; Returns maximum amount of task that can be done till day n ; An array task_dp that stores the maximum task done ; If n = 0 , no solution exists ; If n = 1 , high effort task on that day will be the solution ; Fill the entire array determining which task to choose on day i ; Driver code
function max ( x , y ) { return ( x > y ? x : y ) ; } function maxTasks ( high , low , n ) { var task_dp = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; task_dp [ 0 ] = 0 ; task_dp [ 1 ] = high [ 0 ] ; for ( i = 2 ; i <= n ; i ++ ) task_dp [ i ] = Math . max ( high [ i - 1 ] + task_dp [ i - 2 ] , low [ i - 1 ] + task_dp [ i - 1 ] ) ; return task_dp [ n ] ; } var n = 5 ; var high = [ 3 , 6 , 8 , 7 , 6 ] ; var low = [ 1 , 5 , 4 , 5 , 3 ] ; document . write ( maxTasks ( high , low , n ) ) ;