Minimum number of days to debug all programs | Function to calculate the minimum work sessions ; Break condition ; All bits are set ; Check if already calculated ; Store the answer ; Check if ith bit is set or unset ; Including in current work session ; Including in next work session ; Resultant answer will be minimum of both ; Function to initialize DP array and solve the problem ; Initialize dp table with - 1 ; Resultant mask ; no . of minimum work sessions is even ; no . of minimum work sessions is odd ; Driver code
function minSessions ( codeTime , dp , ones , n , mask , currTime , WorkingSessionTime ) { if ( currTime > WorkingSessionTime ) return Number . MAX_VALUE ; if ( mask == ones ) return 1 ; if ( dp [ mask ] [ currTime ] != - 1 ) return dp [ mask ] [ currTime ] ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { let inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) ; let inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) ; ans = Math . min ( ans , Math . min ( inc , inc_next ) ) ; } } return dp [ mask ] [ currTime ] = ans ; } function solve ( codeTime , n , WorkingSessionTime ) { let dp = new Array ( 1 << 14 ) ; for ( let i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 15 ) . fill ( - 1 ) ; } let ones = ( 1 << n ) - 1 ; let ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) ; if ( WorkingSessionTime < 6 ) { if ( ans % 2 == 0 ) ans = Math . floor ( ans / 2 ) ; else ans = Math . floor ( ans / 2 ) + 1 ; } return ans ; } let codeTime = [ 1 , 2 , 3 , 1 , 1 , 3 ] ; let n = codeTime . length ; let WorkingSessionTime = 4 ; document . write ( solve ( codeTime , n , WorkingSessionTime ) )