forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-number-of-work-sessions-to-finish-the-tasks.cpp
54 lines (52 loc) · 1.94 KB
/
minimum-number-of-work-sessions-to-finish-the-tasks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Time: O(n * 2^n)
// Space: O(2^n)
class Solution {
public:
int minSessions(vector<int>& tasks, int sessionTime) {
// dp[mask]: min used time by choosing tasks in mask bitset
vector<int> dp(1 << size(tasks), numeric_limits<int>::max());
dp[0] = 0;
for (int mask = 0; mask < size(dp) - 1; ++mask) {
int basis = 1;
for (auto task : tasks) {
const int new_mask = mask | basis;
basis <<= 1;
if (new_mask == mask) {
continue;
}
if (dp[mask] % sessionTime + task > sessionTime) {
task += sessionTime - dp[mask] % sessionTime; // take a break
}
dp[new_mask] = min(dp[new_mask], dp[mask] + task);
}
}
return (dp.back() + sessionTime - 1) / sessionTime;
}
};
// Time: O(n * 2^n)
// Space: O(2^n)
class Solution2 {
public:
int minSessions(vector<int>& tasks, int sessionTime) {
// dp[mask][0]: min number of sessions by choosing tasks in mask bitset
// dp[mask][1]: min used time of last session by choosing tasks in mask bitset
vector<pair<int, int>> dp(1 << size(tasks), pair(numeric_limits<int>::max(), numeric_limits<int>::max()));
dp[0] = {0, sessionTime};
for (int mask = 0; mask < size(dp) - 1; ++mask) {
int basis = 1;
for (const auto& task : tasks) {
const int new_mask = mask | basis;
basis <<= 1;
if (new_mask == mask) {
continue;
}
if (dp[mask].second + task <= sessionTime) {
dp[new_mask] = min(dp[new_mask], {dp[mask].first, dp[mask].second + task});
} else {
dp[new_mask] = min(dp[new_mask], {dp[mask].first + 1, task});
}
}
}
return dp.back().first;
}
};