[LeetCode] Find Minimum Time to Finish All Jobs

1723. Find Minimum Time to Finish All Jobs

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int minimumTimeRequired(vector<int>& A, int k) {
int n = A.size();
vector<int> subset(1<<n, 0);
for(int i = 1; i < (1<<n); i++) {
for(int j = 0; j < n; j++) {
if(i & (1<<j)) subset[i] += A[j];
}
}

vector<vector<int>> dp(k, vector<int>(1<<n));
dp[0] = subset;
for(int i = 1; i < k; i++) {
for(int j = 0; j < (1<<n); j++) {
dp[i][j] = dp[i-1][j];
for(int k = j; k; k = (k - 1) & j) {
dp[i][j] = min(dp[i][j], max(subset[k], dp[i-1][j ^ k]));
}
}
}
return dp.back().back();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/02/PS/LeetCode/find-minimum-time-to-finish-all-jobs/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.