Minimum number of days to debug all programs | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSessions ( vector < int > & codeTime , vector < vector < int > > & dp , int ones , int n , int mask , int currTime , int WorkingSessionTime ) { if ( currTime > WorkingSessionTime ) return INT_MAX ; if ( mask == ones ) return 1 ; if ( dp [ mask ] [ currTime ] != -1 ) return dp [ mask ] [ currTime ] ; int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { int inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) ; int inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) ; ans = min ( { ans , inc , inc_next } ) ; } } return dp [ mask ] [ currTime ] = ans ; } int solve ( vector < int > codeTime , int n , int WorkingSessionTime ) { vector < vector < int > > dp ( ( 1 << 14 ) , vector < int > ( 15 , -1 ) ) ; int ones = ( 1 << n ) - 1 ; int ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) ; if ( WorkingSessionTime < 6 ) { if ( ans % 2 == 0 ) ans = ans / 2 ; else ans = ( ans / 2 ) + 1 ; } return ans ; } int main ( ) { vector < int > codeTime = { 1 , 2 , 3 , 1 , 1 , 3 } ; int n = codeTime . size ( ) ; int WorkingSessionTime = 4 ; cout << solve ( codeTime , n , WorkingSessionTime ) << endl ; return 0 ; }