Opening the reading…
Opening the reading…
DYNAMIC PROGRAMMING LEVEL 2 › DP WITH BITMASK
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →For any proposed maximum working time, the question becomes simpler: can all jobs be divided into at most k groups so that every group's total time is at most that limit? If the answer is yes for limit T, it remains yes for every larger limit, because an assignment that fits under T also fits under any larger value.
That monotonic behavior makes binary search possible. For one candidate limit, represent the jobs already assigned by a bitmask. A subset can be one worker's complete workload when its precomputed sum is within the limit. The DP records the fewest workers needed to cover each mask, trying every nonempty submask as the newest worker's jobs.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n x 2^n + 3^n x log S) | Computing every subset sum costs O(n x 2^n). In one feasibility check, each job mask examines all of its submasks; across all masks, each job belongs to one of three states - outside the mask, in the chosen submask, or in the remainder - giving O(3^n) pairs. Binary search repeats the check O(log S) times. |
| Space | O(2^n) extra | The sum and dp arrays each store one value per subset, so their combined size is O(2^n). The returned value is a single integer and output storage is excluded; the worst input shape is still covered by the same full subset arrays. |
#include <algorithm>
#include <climits>
#include <numeric>
#include <vector>
using namespace std;
class Solution {
public:
int minimumTimeRequired(vector<int>& jobs, int k) {
int n = static_cast<int>(jobs.size());
int totalMasks = 1 << n;
vector<int> sum(totalMasks, 0);
for (int mask = 0; mask < totalMasks; ++mask) {
for (int i = 0; i < n; ++i) {
if (mask & (1 << i)) {
sum[mask] += jobs[i];
}
}
}
int left = *max_element(jobs.begin(), jobs.end());
int right = accumulate(jobs.begin(), jobs.end(), 0);
while (left < right) {
int mid = left + (right - left) / 2;
if (canAssign(mid, sum, k, n)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
private:
bool canAssign(int limit, const vector<int>& sum, int k, int n) {
int fullMask = (1 << n) - 1;
vector<int> dp(1 << n, INT_MAX);
dp[0] = 0;
for (int mask = 1; mask <= fullMask; ++mask) {
for (int sub = mask; sub > 0; sub = (sub - 1) & mask) {
int remaining = mask ^ sub;
if (sum[sub] <= limit && dp[remaining] != INT_MAX) {
dp[mask] = min(dp[mask], dp[remaining] + 1);
}
}
}
return dp[fullMask] <= k;
}
};The transition removes sub from mask with mask ^ sub. This is exactly the partition step: sub is the last worker's workload, while the remaining jobs must already be covered. Enumerating sub from mask down to zero visits every nonempty subset of mask without duplicates, and the update counts one additional worker only after the previous remainder is known to be reachable.