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.py
51 lines (48 loc) · 1.76 KB
/
minimum-number-of-work-sessions-to-finish-the-tasks.py
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
# Time: O(n * 2^n)
# Space: O(2^n)
class Solution(object):
def minSessions(self, tasks, sessionTime):
"""
:type tasks: List[int]
:type sessionTime: int
:rtype: int
"""
# dp[mask]: min used time by choosing tasks in mask bitset
dp = [float("inf") for _ in xrange(1<<len(tasks))]
dp[0] = 0
for mask in xrange(len(dp)-1):
basis = 1
for task in tasks:
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[-1]+sessionTime-1)//sessionTime
# Time: O(n * 2^n)
# Space: O(2^n)
class Solution2(object):
def minSessions(self, tasks, sessionTime):
"""
:type tasks: List[int]
:type sessionTime: int
:rtype: int
"""
# 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
dp = [[float("inf")]*2 for _ in xrange(1<<len(tasks))]
dp[0] = [0, sessionTime]
for mask in xrange(len(dp)-1):
basis = 1
for task in tasks:
new_mask = mask|basis
basis <<= 1
if new_mask == mask:
continue
if dp[mask][1]+task <= sessionTime:
dp[new_mask] = min(dp[new_mask], [dp[mask][0], dp[mask][1]+task])
else:
dp[new_mask] = min(dp[new_mask], [dp[mask][0]+1, task])
return dp[-1][0]