C++ Short general code with O(3^n)
Create all possible subsets and keep assigning valid ones to sessions:\n\nclass Solution {\n    // sumMap is for faster subSet sum lookup:\n    int getSum(int m
Create all possible subsets and keep assigning valid ones to sessions:\n```\nclass Solution {\n    // sumMap is for faster subSet sum lookup:\n    int getSum(int mask, const std::vector<int> &tasks, const int n)\n    {\n        int sum = 0;\n        for(int i = 0; i < n && mask; ++i) {\n            sum += (mask &1) ? tasks[i] : 0;\n            mask >>= 1;\n        }\n        return sum;\n    }   \n    int enumerate(int state, std::vector<int> &resultMap, const std::vector<int> &sumMap,  const int sessionTime)\n    {\n        if(state == 0) return 0;\n        if(resultMap[state] != -1) return resultMap[state];\n        int result = INT_MAX;\n        for (int s=state; s ; s=(s-1)&state) {\n            if(sumMap[s] <= sessionTime) {\n                result = std::min(result, 1+enumerate(state-s,resultMap,sumMap,sessionTime));\n            }\n        }\n        return resultMap[state]=result;\n    }\npublic:\t\n    int minSessions(const std::vector<int> &tasks, const int sessionTime)\n    {\n        int n      = tasks.size();\n        int state  = (1<<n)-1;\n        \n        std::vector<int> resultMap(std::vector<int>(1<<n, -1)); //result from each state\n        std::vector<int> sumMap(1<<n,0);  // sum for each state.  \n        for (int s=state; s ; s=(s-1)&state) sumMap[s] = getSum(s,tasks,n);\n        return enumerate(state, resultMap,sumMap,sessionTime);\n    }\n};\n```\nComplexity is O(3^n) .. See this for details: https://cp-algorithms.com/algebra/all-submasks.html .\n\nI think this general approach is more effective from learning point of view. \nLet me know if something is missing.
[]