Skip to content

Commit c11743e

Browse files
committed
ruff
1 parent 66d6969 commit c11743e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

scheduling/weighted_interval_scheduling.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# Implementation of Weighted Interval Scheduling algorithm
2-
# In this algorithm, we are given a list of jobs with start and end times, and each job has a specific weight.
2+
# In this algorithm, we are given a list of jobs with start and end times,
3+
# and each job has a specific weight.
34
# The goal is to find the maximum weight subset of non-overlapping jobs.
4-
# https://en.wikipedia.org/wiki/Interval_scheduling#:~:text=their%20finishing%20times.-,Weighted,-%5Bedit%5D
5+
# https://en.wikipedia.org/wiki/Interval_scheduling
56

67
from __future__ import annotations
78

89

910
def latest_non_conflict(jobs: list[tuple[int, int, int]], n: int) -> int:
1011
"""
11-
This function finds the latest job that does not conflict with the current job at index `n`.
12-
The jobs are given as (start_time, end_time, weight), and the jobs should be sorted by end time.
13-
It returns the index of the latest job that finishes before the current job starts.
12+
This function finds the latest job that does not conflict with
13+
the current job at index `n`.
14+
The jobs are given as (start_time, end_time, weight), and the
15+
jobs should be sorted by end time.
16+
It returns the index of the latest job that finishes before the
17+
current job starts.
1418
Return: The index of the latest non-conflicting job.
1519
>>> latest_non_conflict([(1, 3, 50), (2, 5, 20), (4, 6, 30)], 2)
1620
0
@@ -25,9 +29,11 @@ def latest_non_conflict(jobs: list[tuple[int, int, int]], n: int) -> int:
2529

2630
def find_max_weight(jobs: list[tuple[int, int, int]]) -> int:
2731
"""
28-
This function calculates the maximum weight of non-overlapping jobs using dynamic programming.
32+
This function calculates the maximum weight of non-overlapping jobs
33+
using dynamic programming.
2934
Each job is represented by a tuple (start_time, end_time, weight).
30-
The function builds a DP table where each entry `dp[i]` represents the maximum weight achievable
35+
The function builds a DP table where each entry `dp[i]` represents
36+
the maximum weight achievable
3137
using jobs from index 0 to i.
3238
Return: The maximum achievable weight without overlapping jobs.
3339
>>> find_max_weight([(1, 3, 50), (2, 5, 20), (4, 6, 30)])
@@ -52,7 +58,8 @@ def find_max_weight(jobs: list[tuple[int, int, int]]) -> int:
5258
if latest_job != -1:
5359
include_weight += dp[latest_job]
5460

55-
# Exclude the current job, and take the maximum of including or excluding
61+
# Exclude the current job, and take the maximum of including or
62+
# excluding
5663
dp[i] = max(include_weight, dp[i - 1])
5764

5865
return dp[-1] # The last entry contains the maximum weight

0 commit comments

Comments
 (0)