-
Notifications
You must be signed in to change notification settings - Fork 0
/
1235-Maximum_Profit_in_Job_Scheduling.py
52 lines (35 loc) · 1.56 KB
/
1235-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
46
47
48
49
50
51
52
class Solution:
def binery_next(self, endtime, startTime, position ):
start, end = position, len(startTime) - 1
nextindex = end + 1
while( start <= end ):
mid = (start + end) // 2
if( startTime[mid] < endtime ):
start = mid + 1
else:
nextindex = mid
end = mid - 1
return nextindex
def findMaxProfit(self, works, startTime, n, position ):
# the end, no more job
if position == n:
return 0
# already count
if memo[position] != -1:
return memo[position]
# find Nextindex
nextIndex = self.binery_next( works[position][1], startTime, position )
# do or not to do
maxProfit = max( works[position][2] + self.findMaxProfit( works, startTime, n, nextIndex ), self.findMaxProfit( works, startTime, n , position+1 ) )
memo[position] = maxProfit
return maxProfit
def jobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int]) -> int:
global memo
memo = [ -1 for _ in range(50001) ]
works = []
for s, e, p in zip( startTime, endTime, profit ):
works.append( [s,e,p] )
# sort 1 -> ...
works.sort( key = lambda work: work[0] )
startTime = [ work[0] for work in works ]
return self.findMaxProfit( works, startTime, len(works) ,0 )