There are n tasks assigned to you. The task times are represented as an integer array tasks of length n, where the ith task takes tasks[i] hours to finish. A work session is when you work for at most sessionTime consecutive hours and then take a break.

You should finish the given tasks in a way that satisfies the following conditions:


	If you start a task in a work session, you must complete it in the same work session.
	You can start a new task immediately after finishing the previous one.
	You may complete the tasks in any order.


Given tasks and sessionTime, return the minimum number of work sessions needed to finish all the tasks following the conditions above.

The tests are generated such that sessionTime is greater than or equal to the maximum element in tasks[i].

&nbsp;
Example 1:

Input: tasks = [1,2,3], sessionTime = 3
Output: 2
Explanation: You can finish the tasks in two work sessions.
- First work session: finish the first and the second tasks in 1 + 2 = 3 hours.
- Second work session: finish the third task in 3 hours.


Example 2:

Input: tasks = [3,1,3,1,1], sessionTime = 8
Output: 2
Explanation: You can finish the tasks in two work sessions.
- First work session: finish all the tasks except the last one in 3 + 1 + 3 + 1 = 8 hours.
- Second work session: finish the last task in 1 hour.


Example 3:

Input: tasks = [1,2,3,4,5], sessionTime = 15
Output: 1
Explanation: You can finish all the tasks in one work session.


&nbsp;
Constraints:


	n == tasks.length
	1 &lt;= n &lt;= 14
	1 &lt;= tasks[i] &lt;= 10
	max(tasks[i]) &lt;= sessionTime &lt;= 15


class Solution:
    def minSessions(self, tasks, T):
        n = len(tasks)

        @lru_cache(None)
        def dp(mask):
            if mask == 0: return (1, 0)
            ans = (float("inf"), float("inf"))
            for j in range(n):
                if mask & (1<<j):
                    pieces, last = dp(mask - (1 << j))
                    full = (last + tasks[j] > T)
                    ans = min(ans, (pieces + full, tasks[j] + (1-full)*last))  
            return ans

        return dp((1<<n) - 1)[0]
// Java Solution
class Solution {
    public int minSessions(int[] tasks, int sessionTime) {
        int n = tasks.length, MAX = Integer.MAX_VALUE;
        int[][] dp = new int[1<<n][2];
        dp[0][0] = 1;
        dp[0][1] = 0;

        for(int i = 1; i < (1 << n); i++) {
            dp[i][0] = MAX;
            dp[i][1] = 0;

            for(int t = 0; t < n; t++) {
                if(((1<<t) & i) == 0) continue;

                int[] prev = dp[(1<<t) ^ i];
                if(prev[1] + tasks[t] <= sessionTime) {
                    dp[i] = min(dp[i], new int[]{prev[0], prev[1] + tasks[t]});
                }else{
                    dp[i] = min(dp[i], new int[]{prev[0] + 1, tasks[t]});
                }
            }
        }

        return dp[(1<<n) - 1][0];
    }

    private int[] min(int[] d1, int[] d2) {
        if(d1[0] > d2[0]) return d2;
        if(d1[0] < d2[0]) return d1;
        if(d1[1] > d2[1]) return d2;

        return d1;
    }
}
// C++ Solution
class Solution {
public:
    int minSessions(vector<int>& tasks, int sessionTime) {
        const int N = tasks.size();
        const int INF = 1e9;
        vector<pair<int, int>> dp(1 << N, {INF, INF});
        dp[0] = {0, INF};
        for(int mask = 1; mask < (1 << N); ++mask) {
            pair<int, int> best = {INF, INF};
            for(int i = 0; i < N; ++i) {
                if(mask & (1 << i)) {
                    pair<int, int> cur = dp[mask ^ (1 << i)];
                    if(cur.second + tasks[i] > sessionTime) {
                        cur = {cur.first + 1, tasks[i]};
                    } else
                        cur.second += tasks[i];
                    best = min(best, cur);
                }
            }
            dp[mask] = best;
        }
        return dp[(1 << N) - 1].first;
    }
};
/**
 * @param {number[]} tasks
 * @param {number} sessionTime
 * @return {number}
 */
var minSessions = function(tasks, sessionTime) {
  const n = tasks.length;
  const dp = Array(1 << n).fill().map(() => Array(16).fill(-1));
  
  const solve = (mask, time) => {
    if (mask === (1 << n) - 1) {
      return 1;
    }
    
    if (dp[mask][time] !== -1) {
      return dp[mask][time];
    }
    
    let min = Infinity;
    for (let i = 0; i < n; ++i) {
      if (mask & (1 << i)) {
        continue;
      }

      if (time >= tasks[i]) {
        min = Math.min(
          min,
          solve(mask | (1 << i), time - tasks[i]),
        );
      } else {
        min = Math.min(
          min,
          1 + solve(mask | (1 << i), sessionTime - tasks[i]),
        );
      }
    }
    
    dp[mask][time] = min;
    return min;
  }
  
  return solve(0, sessionTime);
};
Minimum Number of Work Sessions to Finish the Tasks