[C++] || Recursion + Memoization With BitMasking || ✅
\nclass Solution {\npublic:\n    int n;\n    vector<int> tasks;\n    int sessionTime;\n\n    int bitMask;  //the standard BitMask\n    \n    vector<vector<int>>
```\nclass Solution {\npublic:\n    int n;\n    vector<int> tasks;\n    int sessionTime;\n\n    int bitMask;  //the standard BitMask\n    \n    vector<vector<int>> f; // the memoization matrix \n\n    int memo(int taskNo, int timeRemain){\n        /*\n        - taskNo: of all the remaining tasks, which task must be done as the \'taskNo\'th task\n            Eg. TaskNo: 0, 1, 2, 3, 4, 5, 6....\n            Tasks(i=) : 7, 10,5, 4, 13, 3,1...\n            \n            Note: taskNo is actually a function of bitMask\n            \n            ==> Note: taskNo = Number of 1\'s in the bitMask\n            \n        - timeRemain: time time remaining/available in the current session\n        */\n        \n        if(f[bitMask][timeRemain] != -1) return f[bitMask][timeRemain];\n        \n        \n        if(taskNo == n)\n            return  f[bitMask][timeRemain]=  0; // all tasks done!\n        \n        int mini = INT_MAX;\n\n        \n        //Which task should I do as the \'taskNo\'th task?\n        \n        for(int i=0;i<n;i++){\n            \n            //Is task[i] yet to be done?\n            \n            if( (bitMask & (1 << i)) == 0){ //Yes!, i\'th task has yet to be done \n                \n                \n                //Can I do task[i] in the current session ?\n                \n                if(tasks[i] <= timeRemain){  //Yes, you can!\n                    \n                    bitMask = (bitMask | (1 << i)); //since we\'re doing task[i], set the i\'th LSB\n                    \n                    //Option1. Do task[i] as the \'taskNo\'th task in current session\n                    int a = memo(taskNo+1, timeRemain-tasks[i]);\n                    \n                    //Option2. End this session, and do task[i] as the \'taskNo\'th task in the next session\n                    int b = 1 + memo(taskNo+1, sessionTime - tasks[i]);\n                    \n                    bitMask = (bitMask & (~(1 << i))); //BackTracking!, unset the i\'th LSB\n           \n                    //the minimum sessions needed b/w option1 & option2\n                    mini = min(mini, min(a, b));\n                }\n                else{ //No, I cannot do task[i] in the current session\n                    \n                    bitMask = (bitMask | (1 << i)); //since we\'re doing task[i], set the i\'th LSB\n                    \n                    //Hence, end this session here, and do it in the next session\n                    int c = 1 + memo(taskNo+1, sessionTime - tasks[i]);\n                    \n                    bitMask = (bitMask & (~(1 << i))); //BackTracking!, unset the i\'th LSB\n                    \n                    mini = min(mini, c);             \n                }\n            }\n        }\n        \n        \n        return  f[bitMask][timeRemain]=  mini;\n    }\n\n\n    int minSessions(vector<int>& tasks, int sessionTime) {\n        this->tasks = tasks;\n        this->n = tasks.size();\n        this->sessionTime = sessionTime;\n\n        this->bitMask = 0;\n        this->f.resize((1<<n), vector<int>(sessionTime + 1, -1));\n        \n        return 1 + memo(0,sessionTime);\n    }\n};\n```
[]