<p>You are given an integer array <code>jobs</code>, where <code>jobs[i]</code> is the amount of time it takes to complete the <code>i<sup>th</sup></code> job.</p>

<p>There are <code>k</code> workers that you can assign jobs to. Each job should be assigned to <strong>exactly</strong> one worker. The <strong>working time</strong> of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the <strong>maximum working time</strong> of any worker is <strong>minimized</strong>.</p>

<p><em>Return the <strong>minimum</strong> possible <strong>maximum working time</strong> of any assignment. </em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> jobs = [3,2,3], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> By assigning each person one job, the maximum time is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> jobs = [1,2,4,7,8], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> Assign the jobs the following way:
Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)
Worker 2: 4, 7 (working time = 4 + 7 = 11)
The maximum working time is 11.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= jobs.length &lt;= 12</code></li>
	<li><code>1 &lt;= jobs[i] &lt;= 10<sup>7</sup></code></li>
</ul>

#include <vector>
#include <algorithm>
#include <numeric> // For std::accumulate
using namespace std;

class Solution {
public:
    // Recursive backtracking helper function
    bool canAssignJobs(vector<int>& jobs, vector<int>& workers, int idx, int maxTime) {
        // If all jobs have been assigned, return true
        if (idx == jobs.size()) {
            return true;
        }
        
        int job = jobs[idx]; // Current job to be assigned
        
        // Try assigning this job to each worker
        for (int i = 0; i < workers.size(); ++i) {
            if (workers[i] + job <= maxTime) {
                workers[i] += job; // Assign job to worker i
                
                // Recursively assign the next job
                if (canAssignJobs(jobs, workers, idx + 1, maxTime)) {
                    return true; // If the remaining jobs can be assigned, return true
                }
                
                workers[i] -= job; // Backtrack: unassign the job
            }
            
            // Optimization: if a worker has no job and cannot take the current job, 
            // there's no point in trying further workers (because they are all empty)
            if (workers[i] == 0) {
                break;
            }
        }
        
        return false; // Unable to assign all jobs within maxTime
    }
    
    // Function to calculate the minimum possible maximum working time
    int minimumTimeRequired(vector<int>& jobs, int k) {
        int left = *max_element(jobs.begin(), jobs.end()); // The largest job time
        int right = accumulate(jobs.begin(), jobs.end(), 0); // Sum of all job times
        
        while (left < right) {
            int mid = (left + right) / 2;
            
            // Create a worker array to keep track of job assignments
            vector<int> workers(k, 0);
            
            // Use recursive backtracking to check if the current mid is feasible
            if (canAssignJobs(jobs, workers, 0, mid)) {
                right = mid; // If it's possible, search for a smaller maximum time
            } else {
                left = mid + 1; // Otherwise, increase the allowed time
            }
        }
        
        return left; // This will be the minimum possible maximum working time
    }
};

11366