forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
maximum-profit-in-job-scheduling.py
45 lines (40 loc) · 1.14 KB
/
maximum-profit-in-job-scheduling.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
# Time: O(nlogn)
# Space: O(n)
import itertools
import bisect
class Solution(object):
def jobScheduling(self, startTime, endTime, profit):
"""
:type startTime: List[int]
:type endTime: List[int]
:type profit: List[int]
:rtype: int
"""
jobs = sorted(itertools.izip(endTime, startTime, profit))
dp = [(0, 0)]
for e, s, p in jobs:
i = bisect.bisect_right(dp, (s+1, 0))-1
if dp[i][1]+p > dp[-1][1]:
dp.append((e, dp[i][1]+p))
return dp[-1][1]
# Time: O(nlogn)
# Space: O(n)
import heapq
class Solution(object):
def jobScheduling(self, startTime, endTime, profit):
"""
:type startTime: List[int]
:type endTime: List[int]
:type profit: List[int]
:rtype: int
"""
min_heap = zip(startTime, endTime, profit)
heapq.heapify(min_heap)
result = 0
while min_heap:
s, e, p = heapq.heappop(min_heap)
if s < e:
heapq.heappush(min_heap, (e, s, result+p))
else:
result = max(result, p)
return result