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
80 lines (73 loc) · 2.35 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
# 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):
w = SortedList(workers[-x:])
for task in tasks[-x:]: # greedily assign
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)
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 = workers[-x:]
for task in tasks[-x:]: # greedily assign
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