JAVA solution 15ms DFS + Pruning
The pruning - you only need to put the first k tasks in first k sessions.\n\nFor example, assume you are trying if the tasks can be assgiend to 5 sessions (i.e.
The pruning - you only need to put the first k tasks in first k sessions.\n\nFor example, assume you are trying if the tasks can be assgiend to 5 sessions (i.e., n == 5).\n\n-  When you run DFS for the first task, you see 5 empty sessions. It doesn\'t matter which session the task is assgiend to, becasue all of the 5 sessions are identical (/empty). If the DFS fails with the first taks assigned to session #1, there is no need to try the DFS with the first task assigned to session #2, #3, etc.\n-  Similarly, for the first two tasks, you only need to consider assigning them to the first two sessions. For example, the following cases are all identical and there is no need to try all of them:\n\t-  | session #1 | session #2 | session #3 | session #4 | session #5 |\n\t-  |------------|------------|------------|------------|------------|\n\t-  | task 1     | task 2     |            |            |            |\n\t-  | task 1     |            | task 2     |            |            |\n\t-  |      |            |            | task 2     |   task 1    |\n-  For the first 3 tasks, you only need to consider assigning them to the first 3 sessions.\n-  etc.\n\n```JAVA\nclass Solution {\n    public int minSessions(int[] tasks, int sessionTime) {\n        for (int n = 1; n < tasks.length; n++) {\n            int[] remain = new int[n];\n            for (int i = 0; i < n; i++) {\n                remain[i] = sessionTime;\n            }\n            if (canWork(tasks, 0, remain)) {\n                return n;\n            }\n        }\n        return tasks.length;\n    }\n    \n    public boolean canWork(int[] tasks, int curr, int[] remain) {\n        if (curr == tasks.length) {\n            return true;\n        }\n        for (int i = 0; i < remain.length; i++) {\n            if (i > curr) continue;  // *pruning*\n            if (remain[i] >= tasks[curr]) {\n                remain[i] -= tasks[curr];\n                if (canWork(tasks, curr + 1, remain)) {\n                    return true;\n                }\n                remain[i] += tasks[curr];\n            }\n        }\n        return false;\n    }\n}\n```
[]