1
1
# Implementation of Weighted Interval Scheduling algorithm
2
- # In this algorithm, we are given a list of jobs with start and end times,
2
+ # In this algorithm, we are given a list of jobs with start and end times,
3
3
# and each job has a specific weight.
4
4
# The goal is to find the maximum weight subset of non-overlapping jobs.
5
5
# https://en.wikipedia.org/wiki/Interval_scheduling
9
9
10
10
def latest_non_conflict (jobs : list [tuple [int , int , int ]], n : int ) -> int :
11
11
"""
12
- This function finds the latest job that does not conflict with
12
+ This function finds the latest job that does not conflict with
13
13
the current job at index `n`.
14
- The jobs are given as (start_time, end_time, weight), and the
14
+ The jobs are given as (start_time, end_time, weight), and the
15
15
jobs should be sorted by end time.
16
- It returns the index of the latest job that finishes before the
16
+ It returns the index of the latest job that finishes before the
17
17
current job starts.
18
18
Return: The index of the latest non-conflicting job.
19
19
>>> latest_non_conflict([(1, 3, 50), (2, 5, 20), (4, 6, 30)], 2)
@@ -29,10 +29,10 @@ def latest_non_conflict(jobs: list[tuple[int, int, int]], n: int) -> int:
29
29
30
30
def find_max_weight (jobs : list [tuple [int , int , int ]]) -> int :
31
31
"""
32
- This function calculates the maximum weight of non-overlapping jobs
32
+ This function calculates the maximum weight of non-overlapping jobs
33
33
using dynamic programming.
34
34
Each job is represented by a tuple (start_time, end_time, weight).
35
- The function builds a DP table where each entry `dp[i]` represents
35
+ The function builds a DP table where each entry `dp[i]` represents
36
36
the maximum weight achievable
37
37
using jobs from index 0 to i.
38
38
Return: The maximum achievable weight without overlapping jobs.
@@ -58,7 +58,7 @@ def find_max_weight(jobs: list[tuple[int, int, int]]) -> int:
58
58
if latest_job != - 1 :
59
59
include_weight += dp [latest_job ]
60
60
61
- # Exclude the current job, and take the maximum of including or
61
+ # Exclude the current job, and take the maximum of including or
62
62
# excluding
63
63
dp [i ] = max (include_weight , dp [i - 1 ])
64
64
0 commit comments