Skip to content

Commit 4f73731

Browse files
authored
Create maximum-profit-in-job-scheduling.py
1 parent 5e1680c commit 4f73731

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
import itertools
5+
import bisect
6+
7+
8+
class Solution(object):
9+
def jobScheduling(self, startTime, endTime, profit):
10+
"""
11+
:type startTime: List[int]
12+
:type endTime: List[int]
13+
:type profit: List[int]
14+
:rtype: int
15+
"""
16+
jobs = sorted(itertools.izip(endTime, startTime, profit))
17+
dp = [(0, 0)]
18+
for e, s, p in jobs:
19+
i = bisect.bisect_right(dp, (s+1, 0))-1
20+
if dp[i][1]+p > dp[-1][1]:
21+
dp.append((e, dp[i][1]+p))
22+
return dp[-1][1]
23+
24+
25+
# Time: O(nlogn)
26+
# Space: O(n)
27+
import heapq
28+
class Solution(object):
29+
def jobScheduling(self, startTime, endTime, profit):
30+
"""
31+
:type startTime: List[int]
32+
:type endTime: List[int]
33+
:type profit: List[int]
34+
:rtype: int
35+
"""
36+
min_heap = zip(startTime, endTime, profit)
37+
heapq.heapify(min_heap)
38+
result = 0
39+
while min_heap:
40+
s, e, p = heapq.heappop(min_heap)
41+
if s < e:
42+
heapq.heappush(min_heap, (e, s, result+p))
43+
else:
44+
result = max(result, p)
45+
return result

0 commit comments

Comments
 (0)