-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.py
More file actions
78 lines (69 loc) · 2.18 KB
/
Copy pathproblem.py
File metadata and controls
78 lines (69 loc) · 2.18 KB
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
#!/usr/bin/env python3
from typing import List
from collections import deque
from bisect import bisect_left
class Solution:
def lc2071(
self, tasks: List[int], workers: List[int], pills: int, strength: int
) -> int:
"""
link: https://leetcode.cn/problems/maximum-number-of-tasks-you-can-assign/
"""
n, m = len(tasks), len(workers)
tasks.sort()
workers.sort()
def check(k: int) -> bool:
i, p = 0, pills
q = deque()
for w in workers[-k:]:
while i < k and tasks[i] <= w + strength:
q.append(tasks[i])
i += 1
if not q:
return False
if w >= q[0]: # 不嗑药能完成的任务
q.popleft()
else: # 嗑药能完成的最大任务
if p == 0:
return False
p -= 1
q.pop()
return True
left = ans = 0
right = min(n, m)
while left <= right:
mid = (left + right + 1) // 2
if check(mid):
ans = mid
left = mid + 1
else:
right = mid - 1
return ans
def lc2071_bisect(
self, tasks: List[int], workers: List[int], pills: int, strength: int
) -> int:
"""
link: https://leetcode.cn/problems/maximum-number-of-tasks-you-can-assign/
"""
n, m = len(tasks), len(workers)
tasks.sort()
workers.sort()
def check(k: int) -> bool:
k += 1
i, p = 0, pills
q = deque()
for w in workers[-k:]:
while i < k and tasks[i] <= w + strength:
q.append(tasks[i])
i += 1
if not q:
return True
if w >= q[0]:
q.popleft()
else:
if p == 0:
return True
p -= 1
q.pop()
return False
return bisect_left(range(min(n, m)), True, key=check)