From c11743ecc7de42f8305f0ee7fb433294c6255544 Mon Sep 17 00:00:00 2001 From: B Karthik Date: Sat, 5 Oct 2024 08:35:44 +0530 Subject: [PATCH] ruff --- scheduling/weighted_interval_scheduling.py | 23 ++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/scheduling/weighted_interval_scheduling.py b/scheduling/weighted_interval_scheduling.py index d58e4364d..247a9af7c 100644 --- a/scheduling/weighted_interval_scheduling.py +++ b/scheduling/weighted_interval_scheduling.py @@ -1,16 +1,20 @@ # Implementation of Weighted Interval Scheduling algorithm -# In this algorithm, we are given a list of jobs with start and end times, and each job has a specific weight. +# In this algorithm, we are given a list of jobs with start and end times, +# and each job has a specific weight. # The goal is to find the maximum weight subset of non-overlapping jobs. -# https://en.wikipedia.org/wiki/Interval_scheduling#:~:text=their%20finishing%20times.-,Weighted,-%5Bedit%5D +# https://en.wikipedia.org/wiki/Interval_scheduling from __future__ import annotations def latest_non_conflict(jobs: list[tuple[int, int, int]], n: int) -> int: """ - This function finds the latest job that does not conflict with the current job at index `n`. - The jobs are given as (start_time, end_time, weight), and the jobs should be sorted by end time. - It returns the index of the latest job that finishes before the current job starts. + This function finds the latest job that does not conflict with + the current job at index `n`. + The jobs are given as (start_time, end_time, weight), and the + jobs should be sorted by end time. + It returns the index of the latest job that finishes before the + current job starts. Return: The index of the latest non-conflicting job. >>> latest_non_conflict([(1, 3, 50), (2, 5, 20), (4, 6, 30)], 2) 0 @@ -25,9 +29,11 @@ def latest_non_conflict(jobs: list[tuple[int, int, int]], n: int) -> int: def find_max_weight(jobs: list[tuple[int, int, int]]) -> int: """ - This function calculates the maximum weight of non-overlapping jobs using dynamic programming. + This function calculates the maximum weight of non-overlapping jobs + using dynamic programming. Each job is represented by a tuple (start_time, end_time, weight). - The function builds a DP table where each entry `dp[i]` represents the maximum weight achievable + The function builds a DP table where each entry `dp[i]` represents + the maximum weight achievable using jobs from index 0 to i. Return: The maximum achievable weight without overlapping jobs. >>> 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: if latest_job != -1: include_weight += dp[latest_job] - # Exclude the current job, and take the maximum of including or excluding + # Exclude the current job, and take the maximum of including or + # excluding dp[i] = max(include_weight, dp[i - 1]) return dp[-1] # The last entry contains the maximum weight