C++ Solution | DP + Bit masking
We denote which tasks have been done and which are yet to be done by the binary representation of a variable mask (basic masking). Now everytime we have to sele
We denote which tasks have been done and which are yet to be done by the binary representation of a variable mask (basic masking). Now everytime we have to select a task to do, we have the following choices : \n1. If the current task requires time <= the time remaining in your current session, you can either select this task to be done or we can move on to some other undone task.\n2. If the current task requires time > the time remaining in your current session, to do this task, you need a new session.\n\nOur answer is the minimum of these three cases.\n\nIn this case a state of dp is defined by two variables, mask and time remaining in your current session, and stores the value of the number of new sessions required to do all tasks from current state.\n\n\'\'\'\n\nclass Solution {\npublic:\n    int sto; \n\t\n    int dp[(1<<14)-1][18]; int n;\n\t\n    vector<int> tasks;\n\t\n    int solve(int mask,int st){\n        if(mask==(1<<n)-1){//If all the tasks are done, return 0;\n            return 0;\n        }\n        if(dp[mask][st]!=-1) return dp[mask][st]; //If dp has the value of present state, no need to recalculate\n\t\t\n        int ans=INT_MAX; \n\t\t\n        for(int i=0;i<tasks.size();i++){\n            if(tasks[i]<=st&&((mask&(1<<i))==0)){//case 1\n                ans=min(ans,solve(mask|(1<<i),st-tasks[i]));\n            }\n            else if(((mask&(1<<i))==0)){//case 2\n                ans=min(ans,solve(mask|(1<<i),sto-tasks[i])+1);\n            }\n        }\n        return dp[mask][st]=ans;\n    }\n    \n    int minSessions(vector<int>& task, int sessionTime) {\n        tasks=task;\n        sto=sessionTime; n=tasks.size();\n        memset(dp,-1,sizeof(dp));\n        int mask=0;\n        return solve(mask,sto)+1; // + 1 for the initial session\n        \n    }\n};\n\n\'\'\'\n\n**If you found this helpful, please consider upvoting.**
['Bit Manipulation', 'Recursion', 'Memoization']