Python backtracking about 280ms and very low space complexity
This was directly inspired by this solution:  https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1433054/Python-or-Backtr
This was directly inspired by this solution:  https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1433054/Python-or-Backtracking-or-664ms-or-100-time-and-space-or-Explanation\n\nBasically the idea is to formulate it as a problem of finding least number of sessions such that their sum is at most equal to session time.\n\nSo for every value, we have two choices:\n\t1. add it to any one of the sessions we\'ve already made without exceeding the session time total\n\t2. put it in its own session\n\nWe try these choices in all combinations to find the least number.\n\nA couple of optimizations can be made to make the solution faster in certain cases.\n\nThe hardest part was figuring out looping over tasks will get you TLE. It is the sessions you need to loop over.\n\n```python\nclass Solution:\n    def minSessions(self, tasks: List[int], sessionTime: int) -> int:\n\t\n        # if sum fully divisible, the answer can be directly calculated\n        total_tasks = sum(tasks)\n        quotient, remainder = divmod(total_tasks, sessionTime)\n        \n        sessions = []\n        ans = len(tasks)  # cant be more sessions so start with that\n        least_num_sessions = quotient + (remainder > 0)  # incase of a non-zero remainder, this is the least number of sessions possible\n        \n        def dfs(idx):\n            nonlocal ans\n            if len(sessions) >= ans:\n                return\n            \n            if idx == len(tasks):\n                if ans == least_num_sessions:\n                    return True  # cant be lower so stop searching\n                ans = min(ans, len(sessions))\n                return\n            \n            # check if this value fits in any of the existing sessions\n            for i in range(len(sessions)):\n                if sessions[i] + tasks[idx] <= sessionTime:\n                    sessions[i] += tasks[idx]\n                    if dfs(idx + 1):\n                        return True\n                    sessions[i] -= tasks[idx]\n            \n            # give value its own session\n            sessions.append(tasks[idx])\n            if dfs(idx + 1):\n                return True\n            sessions.pop()\n        \n        dfs(0)\n        return ans\n```
['Backtracking', 'Python', 'Python3']