Backtracking Solution | Java |  Simpler to understand
The idea is to populate the tasks in such groups such that we can fit in optimal tasks together and at the same time maintain the threshold for a given session.
The idea is to populate the tasks in such groups such that we can fit in optimal tasks together and at the same time maintain the threshold for a given session. \n\nTo handle this, let us try to group them initially all independently and then backtrack to find space in any of the buckets and add the task to that bucket.\n```\nclass Solution {\n    int minBucket = Integer.MAX_VALUE;\n    public void helper(int[] tasks, int time, int index, List<Integer> buckets){\n        \n        //there has already been a setting when less buckets could satisfy requirements\n        if(buckets.size() >= minBucket) \n            return;\n        if(index == tasks.length){\n            minBucket = Math.min(minBucket, buckets.size());\n            return;\n        }\n        \n        buckets.add(tasks[index]);\n        helper(tasks, time, index+1, buckets);\n        buckets.remove(buckets.size() - 1);\n        \n        //now iterate over all the buckets and if there is space, put and backtrack again\n        for(int i=0; i<buckets.size(); i++){\n            if(tasks[index] + buckets.get(i) <= time){\n                buckets.set(i, buckets.get(i) + tasks[index]);\n                helper(tasks, time, index+1, buckets);\n                buckets.set(i, buckets.get(i) - tasks[index]);\n            }\n        }    \n    }\n    \n    private void reverse(int[] tasks){\n        int i=0, j=tasks.length-1;\n        while(i < j){\n            int temp = tasks[i];\n            tasks[i] = tasks[j];\n            tasks[j] = temp;\n            i++;\n            j--;\n        }\n    }\n    public int minSessions(int[] tasks, int sessionTime) {\n        Arrays.sort(tasks);\n        reverse(tasks);\n        helper(tasks, sessionTime, 0, new ArrayList<Integer>());\n        return minBucket;\n    }\n}\n```
['Backtracking', 'Java']