001235-Maximum-Profit-in-Job-Scheduling
Problem
Solution
class Solution:
def jobScheduling(self, startTime: List[int], endTime: List[int], profit: List[int]) -> int:
if not startTime:
return 0
# sort job by endtime jobs = [endtime, starttime, profit]
jobs = sorted(zip(endTime, startTime, profit))
print("jobs== ", jobs)
n = len(jobs)
# max profit at ith job
dp = [0] * n
dp[0] = jobs[0][2]
# iterate all jobs
for i in range(1, n):
# not pick up ith job
dp[i] = dp[i-1]
# pick up ith job,
# search previous job end time <= current job start time.
# since end time is sorted, do binary search
prev_idx = bisect.bisect_left(jobs, (jobs[i][1]+1, 0, 0))
if prev_idx == 0:
dp[i] = max(dp[i-1], jobs[i][2])
else:
dp[i] = max(dp[i], dp[prev_idx-1] + jobs[i][2])
return dp[-1]Previous001218-Longest-Arithmetic-Subsequence-of-Given-DifferenceNext001493-Longest-Subarray-of 1-After-Deleting-One-Element
Last updated