10
10
11
11
from collections import defaultdict
12
12
13
+
13
14
class AssignmentUsingBitmask :
14
15
def __init__ (self , task_performed , total ):
15
16
"""
16
17
Initialize the AssignmentUsingBitmask class.
17
18
18
19
:param task_performed: A list of lists where each sublist contains the tasks that a person can perform.
19
20
:param total: Total number of tasks.
20
-
21
+
21
22
>>> a = AssignmentUsingBitmask([[1, 3, 4], [1, 2, 5], [3, 4]], 5)
22
23
>>> a.total_tasks
23
24
5
@@ -28,7 +29,9 @@ def __init__(self, task_performed, total):
28
29
29
30
# DP table will have a dimension of (2^M) x (N+1)
30
31
# initially all values are set to -1
31
- self .dp = [[- 1 for _ in range (total + 1 )] for _ in range (2 ** len (task_performed ))]
32
+ self .dp = [
33
+ [- 1 for _ in range (total + 1 )] for _ in range (2 ** len (task_performed ))
34
+ ]
32
35
33
36
self .task = defaultdict (list ) # stores the list of persons for each task
34
37
@@ -42,7 +45,7 @@ def count_ways_until(self, mask, task_no):
42
45
:param mask: Current mask representing which persons have been assigned tasks.
43
46
:param task_no: The current task number being considered.
44
47
:return: The number of ways to assign tasks starting from the current state.
45
-
48
+
46
49
>>> a = AssignmentUsingBitmask([[1, 3, 4], [1, 2, 5], [3, 4]], 5)
47
50
>>> a.count_ways_until(0, 1)
48
51
0
@@ -85,7 +88,7 @@ def count_no_of_ways(self, task_performed):
85
88
86
89
:param task_performed: A list of lists where each sublist contains the tasks that a person can perform.
87
90
:return: The total number of ways to distribute tasks.
88
-
91
+
89
92
>>> a = AssignmentUsingBitmask([[1, 3, 4], [1, 2, 5], [3, 4]], 5)
90
93
>>> a.count_no_of_ways([[1, 3, 4], [1, 2, 5], [3, 4]])
91
94
10
@@ -98,6 +101,7 @@ def count_no_of_ways(self, task_performed):
98
101
# Call the function to fill the DP table, final answer is stored in dp[0][1]
99
102
return self .count_ways_until (0 , 1 )
100
103
104
+
101
105
if __name__ == "__main__" :
102
106
total_tasks = 5 # Total number of tasks (the value of N)
103
107
0 commit comments