C++ & Java Bitmask DP with Explanation time: O(n * 2^n) space: O(2^n)
dp[mask] = {a, b} where\na - minimum number of session\nb - minimum time of last session\nThe idea is to go through all tasks who belong to mask and optimally c
dp[mask] = {a, b} where\na - minimum number of session\nb - minimum time of last session\nThe idea is to go through all tasks who belong to mask and optimally choose the last task \'t\' that was added to last session.\n\nSimilar problem: CSES Problem Set - Elevator Rides https://cses.fi/problemset/task/1653\nThis is exactly the same problem if you change the capacity of elevator to \'sessionTime\' and weights of people to \'task\'.\n```\n// C++ Solution\nclass Solution {\npublic:\n    int minSessions(vector<int>& tasks, int sessionTime) {\n        const int N = tasks.size();\n        const int INF = 1e9;\n        vector<pair<int, int>> dp(1 << N, {INF, INF});\n        dp[0] = {0, INF};\n        for(int mask = 1; mask < (1 << N); ++mask) {\n            pair<int, int> best = {INF, INF};\n            for(int i = 0; i < N; ++i) {\n                if(mask & (1 << i)) {\n                    pair<int, int> cur = dp[mask ^ (1 << i)];\n                    if(cur.second + tasks[i] > sessionTime) {\n                        cur = {cur.first + 1, tasks[i]};\n                    } else\n                        cur.second += tasks[i];\n                    best = min(best, cur);\n                }\n            }\n            dp[mask] = best;\n        }\n        return dp[(1 << N) - 1].first;\n    }\n};\n```\n```\n// Java Solution\nclass Solution {\n    public int minSessions(int[] tasks, int sessionTime) {\n        int n = tasks.length, MAX = Integer.MAX_VALUE;\n        int[][] dp = new int[1<<n][2];\n        dp[0][0] = 1;\n        dp[0][1] = 0;\n        \n        for(int i = 1; i < (1 << n); i++) {\n            dp[i][0] = MAX;\n            dp[i][1] = 0;\n            \n            for(int t = 0; t < n; t++) {\n                if(((1<<t) & i) == 0) continue;\n                \n                int[] prev = dp[(1<<t) ^ i];\n                if(prev[1] + tasks[t] <= sessionTime) {\n                    dp[i] = min(dp[i], new int[]{prev[0], prev[1] + tasks[t]});\n                }else{\n                    dp[i] = min(dp[i], new int[]{prev[0] + 1, tasks[t]});\n                }\n            }            \n        }\n        \n        return dp[(1<<n) - 1][0];\n    }\n    \n    private int[] min(int[] d1, int[] d2) {\n        if(d1[0] > d2[0]) return d2;\n        if(d1[0] < d2[0]) return d1;\n        if(d1[1] > d2[1]) return d2;\n        \n        return d1;\n    }\n}\n```
['Dynamic Programming', 'C', 'Bitmask', 'Java']