Skip to content

Commit 462e39e

Browse files
authored
Create maximize-profit-from-task-assignment.py
1 parent 4e22af1 commit 462e39e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Time: O(n + tlogt)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
# freq table, sort, greedy
8+
class Solution(object):
9+
def maxProfit(self, workers, tasks):
10+
"""
11+
:type workers: List[int]
12+
:type tasks: List[List[int]]
13+
:rtype: int
14+
"""
15+
cnt = collections.defaultdict(int)
16+
for x in workers:
17+
cnt[x] += 1
18+
tasks.sort(key=lambda x: x[1], reverse=True)
19+
result = 0
20+
k = 1
21+
for s, p in tasks:
22+
if cnt[s]:
23+
cnt[s] -= 1
24+
result += p
25+
elif k:
26+
k -= 1
27+
result += p
28+
return result

0 commit comments

Comments
 (0)