forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-number-of-tasks-you-can-assign.py
167 lines (148 loc) · 5.11 KB
/
maximum-number-of-tasks-you-can-assign.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Time: O(n * (logn)^2)
# Space: O(n)
from sortedcontainers import SortedList
class Solution(object):
def maxTaskAssign(self, tasks, workers, pills, strength):
"""
:type tasks: List[int]
:type workers: List[int]
:type pills: int
:type strength: int
:rtype: int
"""
def check(tasks, workers, pills, strength, x):
t = SortedList(tasks[:x])
for worker in workers[-x:]: # enumerate from the weakest worker to the strongest worker, greedily assign him to the hardest task which he can do
i = t.bisect_right(worker)-1
if i != -1:
t.pop(i)
continue
if pills:
i = t.bisect_right(worker+strength)-1
if i != -1:
t.pop(i)
pills -= 1
continue
return False
return True
tasks.sort()
workers.sort()
left, right = 1, min(len(workers), len(tasks))
while left <= right:
mid = left + (right-left)//2
if not check(tasks, workers, pills, strength, mid):
right = mid-1
else:
left = mid+1
return right
# Time: O(n * (logn)^2)
# Space: O(n)
from sortedcontainers import SortedList
class Solution2(object):
def maxTaskAssign(self, tasks, workers, pills, strength):
"""
:type tasks: List[int]
:type workers: List[int]
:type pills: int
:type strength: int
:rtype: int
"""
def check(tasks, workers, pills, strength, x):
w = SortedList(workers[-x:])
for task in tasks[-x:]: # enumerate from the hardest task to the easiest task, greedily assign it to the weakest worker whom it can be done by
i = w.bisect_left(task)
if i != len(w):
w.pop(i)
continue
if pills:
i = w.bisect_left(task-strength)
if i != len(w):
w.pop(i)
pills -= 1
continue
return False
return True
tasks.sort(reverse=True)
workers.sort()
left, right = 1, min(len(workers), len(tasks))
while left <= right:
mid = left + (right-left)//2
if not check(tasks, workers, pills, strength, mid):
right = mid-1
else:
left = mid+1
return right
# Time: O(n^2 * logn)
# Space: O(n)
import bisect
class Solution3(object):
def maxTaskAssign(self, tasks, workers, pills, strength):
"""
:type tasks: List[int]
:type workers: List[int]
:type pills: int
:type strength: int
:rtype: int
"""
def check(tasks, workers, pills, strength, x):
t = tasks[:x]
for worker in workers[-x:]: # enumerate from the weakest worker to the strongest worker, greedily assign him to the hardest task which he can do
i = bisect.bisect_right(t, worker)-1
if i != -1:
t.pop(i)
continue
if pills:
i = bisect.bisect_right(t, worker+strength)-1
if i != -1:
t.pop(i)
pills -= 1
continue
return False
return True
tasks.sort()
workers.sort()
left, right = 1, min(len(workers), len(tasks))
while left <= right:
mid = left + (right-left)//2
if not check(tasks, workers, pills, strength, mid):
right = mid-1
else:
left = mid+1
return right
# Time: O(n^2 * logn)
# Space: O(n)
import bisect
class Solution4(object):
def maxTaskAssign(self, tasks, workers, pills, strength):
"""
:type tasks: List[int]
:type workers: List[int]
:type pills: int
:type strength: int
:rtype: int
"""
def check(tasks, workers, pills, strength, x):
w = workers[-x:]
for task in tasks[-x:]: # enumerate from the hardest task to the easiest task, greedily assign it to the weakest worker whom it can be done by
i = bisect.bisect_left(w, task)
if i != len(w):
w.pop(i)
continue
if pills:
i = bisect.bisect_left(w, task-strength)
if i != len(w):
w.pop(i)
pills -= 1
continue
return False
return True
tasks.sort(reverse=True)
workers.sort()
left, right = 1, min(len(workers), len(tasks))
while left <= right:
mid = left + (right-left)//2
if not check(tasks, workers, pills, strength, mid):
right = mid-1
else:
left = mid+1
return right