classSolution:defjobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int])->int:
intervals =sorted(zip(startTime, endTime, profit))
cache ={}defdfs(i):if i ==len(intervals):return0if i in cache:return cache[i]# don't include
res = dfs(i +1)# include
j = i +1while j <len(intervals):if intervals[i][1]<= intervals[j][0]:break
j +=1
cache[i]= res =max(res, intervals[i][2]+ dfs(j))return res
return dfs(0)
classSolution:defjobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int])->int:
intervals =sorted(zip(startTime, endTime, profit))
cache ={}defdfs(i):if i ==len(intervals):return0if i in cache:return cache[i]# don't include
res = dfs(i +1)# include
j = bisect.bisect(intervals,(intervals[i][1],-1,-1))
cache[i]= res =max(res, intervals[i][2]+ dfs(j))return res
return dfs(0)
classSolution:defjobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int])->int:
n =len(startTime)
index =list(range(n))
index.sort(key=lambda i: startTime[i])
cache =[-1]* n
defdfs(i):if i == n:return0if cache[i]!=-1:return cache[i]
res = dfs(i +1)
left, right, j = i +1, n, n
while left < right:
mid =(left + right)//2if startTime[index[mid]]>= endTime[index[i]]:
j = mid
right = mid
else:
left = mid +1
cache[i]= res =max(res, profit[index[i]]+ dfs(j))return res
return dfs(0)