Skip to content

Commit e9d90ee

Browse files
Update job_scheduling.py
1 parent 97130b5 commit e9d90ee

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

dynamic_programming/job_scheduling.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
Weighted Job Scheduling Problem
33
Given jobs with start time, end time, and profit, find the maximum profit
44
subset of non-overlapping jobs.
5+
https://en.wikipedia.org/wiki/Weighted_interval_scheduling
56
"""
67

78
from bisect import bisect_right
9+
from typing import List, Tuple
810

911

10-
def job_scheduling(jobs):
12+
def job_scheduling(jobs: List[Tuple[int, int, int]]) -> int:
1113
"""
1214
>>> jobs = [(1, 3, 50), (3, 5, 20), (0, 6, 100), (4, 6, 70), (3, 8, 60)]
1315
>>> job_scheduling(jobs)
@@ -18,21 +20,19 @@ def job_scheduling(jobs):
1820
"""
1921
if not jobs:
2022
return 0
21-
# Sort jobs by end time
22-
jobs = sorted(jobs, key=lambda x: x[1])
23+
24+
jobs.sort(key=lambda x: x[1])
2325
n = len(jobs)
24-
# dp[i] stores max profit including jobs[i]
2526
dp = [0] * n
2627
dp[0] = jobs[0][2]
27-
28-
# Store end times separately for binary search
2928
end_times = [job[1] for job in jobs]
3029

3130
for i in range(1, n):
3231
profit_incl = jobs[i][2]
33-
# Find last non-conflicting job using binary search
34-
index = bisect_right(end_times, jobs[i][0] - 1) - 1
32+
# ✅ allow jobs that start when another ends
33+
index = bisect_right(end_times, jobs[i][0]) - 1
3534
if index != -1:
3635
profit_incl += dp[index]
3736
dp[i] = max(profit_incl, dp[i - 1])
37+
3838
return dp[-1]

0 commit comments

Comments
 (0)