题目描述
You are given an integer array jobs, where jobs[i] is the amount of time it takes to complete the ith job.
There are k workers that you can assign jobs to. Each job should be assigned to exactly one worker. The working time 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 maximum working time of any worker is minimized.
Return the minimum possible maximum working time of any assignment.
样例
Input: jobs = [3,2,3], k = 3
Output: 3
Explanation: By assigning each person one job, the maximum time is 3.
Input: jobs = [1,2,4,7,8], k = 2
Output: 11
Explanation: 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.
Constraints:
1 <= k <= jobs.length <= 12
1 <= jobs[i] <= 107
算法1
(DP) O(3n)
C++ 代码
class Solution {
private:
int f[13][1 << 12];
int time[1 << 12];
public:
int minimumTimeRequired(vector<int>& jobs, int k) {
int n = jobs.size();
int pat = 1;
for(int i = 0; i < n; i++) time[pat] = jobs[i], pat <<= 1;
time[0] = 0;
for(int i = 1; i < (1 << n); i++) {
int t = (i & (i - 1));
time[i] = time[t] + time[i - t];
}
memset(f, 0x3f, sizeof(int) * (1 << n));
for(int i = 1; i <= k; i++) {
for(int j = 0; j < (1 << n); j++) {
f[i][j] = time[j];
for(int s = j; s; s = (s-1)&j) {
f[i][j] = min(f[i][j], max(f[i-1][s], time[s^j]));
}
}
}
return f[k][(1 << n) - 1];
}
};
// enum assingment status and compare the time?
// how to enum?
// assign jobs to workers or choose worker for jobs?
// 1. assign jobs to workers:
// 1.1 each step assign jobs to a worker, and the worker will never be assigned again in subsequent recursion
// 1.2 at corner case, all jobs are assigned and cal the time
// 2. choose worker for jobs:
// 2.1 each step choose a worker for job, and never reassign the job to other workes in subsequent recursion
// 2.2 at corner case, all jobs are assigned and cal the time
//
// every job has k workers to be assigned
// 12 ^ 12 TLE...
// how to improved? could we find duplicate sub process?
// for enum 1. the detail job assigned status in sub process, will not affect the pre process
// the pre process dont care how sub process assign the jobs subset
// it only care about the the min work time of sub process for jobs subset
// so we can represent status with 2 value, workers amount, which jobs are assigned...
// for enum 2. in sub-process, which worker are chosen for each job is less important
// again, what we care is those jobs min work time
// so how we represent the status? jobs number and something, but we dont care about the single job!!!
// improve way for enum 2 is not so intuitive...
// lets dp using improve of enum 1
// f[i][j], jobs' subset j assigned to pre i workers
// property: int, min work time
// there at most 12 jobs, we can use a 32 bit signed int j to represent its assigned status
// state trans:
// f[i][j] = for js as j's subset, min(max(f[i-1][js], work time for (j - js)))
// and the time overhead is
// according to binomial theorem: (a+b)^n = c(n,0)a^n + c(n,1)a^(n-1)b + ... + c(n,n)b^n
// o(c(n,0) * 2^0 + ... + c(n,n) * 2^n) + o(2^n)
// = o((1+2)^n + 2^n) = o(3^n) = 5e5
// see? the way we search affect whether we can store the sub state...
// remember, think both ways assign a to b or choose a b for a...