-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
feat: Add Weighted Job Scheduling algorithm to dynamic programming #13378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """ | ||
| Author : Prince Kumar Prajapati | ||
| Date : October 9, 2025 | ||
|
|
||
| Weighted Job Scheduling Problem | ||
| -------------------------------- | ||
| Given N jobs where every job has a start time, finish time, and profit, | ||
| find the maximum profit subset of jobs such that no two jobs overlap. | ||
|
|
||
| Approach: | ||
| - Sort all jobs by their finish time. | ||
| - For each job, find the last non-conflicting job using binary search. | ||
| - Use Dynamic Programming to build up the maximum profit table. | ||
|
|
||
| Time Complexity: O(n log n) | ||
| Space Complexity: O(n) | ||
| """ | ||
|
|
||
|
|
||
| def find_last_non_conflicting(jobs, index): | ||
| """ | ||
| Binary search to find the last job that doesn't overlap | ||
| with the current job (at index). | ||
| """ | ||
| low, high = 0, index - 1 | ||
| while low <= high: | ||
| mid = (low + high) // 2 | ||
| if jobs[mid][1] <= jobs[index][0]: | ||
| if mid + 1 < index and jobs[mid + 1][1] <= jobs[index][0]: | ||
| low = mid + 1 | ||
| else: | ||
| return mid | ||
| else: | ||
| high = mid - 1 | ||
| return -1 | ||
|
|
||
|
|
||
| def weighted_job_scheduling(jobs): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: |
||
| """ | ||
| Function to find the maximum profit from a set of jobs. | ||
|
|
||
| Parameters: | ||
| jobs (list of tuples): Each tuple represents (start_time, end_time, profit) | ||
| Returns: | ||
| int: Maximum profit achievable without overlapping jobs. | ||
| """ | ||
|
|
||
| # Step 1: Sort jobs by their finish time | ||
| jobs.sort(key=lambda x: x[1]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
|
|
||
| n = len(jobs) | ||
| dp = [0] * n # dp[i] will store the max profit up to job i | ||
| dp[0] = jobs[0][2] # First job profit is the base case | ||
|
|
||
| # Step 2: Iterate through all jobs | ||
| for i in range(1, n): | ||
| # Include current job | ||
| include_profit = jobs[i][2] | ||
|
|
||
| # Find the last non-conflicting job | ||
| j = find_last_non_conflicting(jobs, i) | ||
| if j != -1: | ||
| include_profit += dp[j] | ||
|
|
||
| # Exclude current job (take previous best) | ||
| dp[i] = max(include_profit, dp[i - 1]) | ||
|
|
||
| # Step 3: Return the maximum profit at the end | ||
| return dp[-1] | ||
|
|
||
|
|
||
| # Example usage / Test case | ||
| if __name__ == "__main__": | ||
| # Each job is represented as (start_time, end_time, profit) | ||
| jobs = [(1, 3, 50), (2, 4, 10), (3, 5, 40), (3, 6, 70)] | ||
|
|
||
| print("Maximum Profit:", weighted_job_scheduling(jobs)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
dynamic_programming/weighted_job_scheduling.py, please provide doctest for the functionfind_last_non_conflictingPlease provide return type hint for the function:
find_last_non_conflicting. If the function does not return a value, please provide the type hint as:def function() -> None:Please provide type hint for the parameter:
jobsPlease provide type hint for the parameter:
index