Skip to content

Commit 7bc79fa

Browse files
feat: Add Weighted Job Scheduling algorithm to dynamic programming
- Implemented efficient Weighted Job Scheduling algorithm using Dynamic Programming and Binary Search - Time Complexity: O(n log n), Space Complexity: O(n) - Added comprehensive documentation and example usage - Includes proper author attribution and date - Fixed linting issues: removed unused imports and applied formatting Hacktoberfest contribution - Dynamic Programming implementation for finding maximum profit from non-overlapping job intervals
1 parent 788d95b commit 7bc79fa

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [Word Break](backtracking/word_break.py)
2727
* [Word Ladder](backtracking/word_ladder.py)
2828
* [Word Search](backtracking/word_search.py)
29+
* [Weighted Job Scheduling](backtracking/weighted_job_scheduling.py)
2930

3031
## Bit Manipulation
3132
* [Binary And Operator](bit_manipulation/binary_and_operator.py)
@@ -880,6 +881,8 @@
880881
* [Sdes](other/sdes.py)
881882
* [Tower Of Hanoi](other/tower_of_hanoi.py)
882883
* [Word Search](other/word_search.py)
884+
* [Weighted Job Scheduling](backtracking/weighted_job_scheduling.py)
885+
883886

884887
## Physics
885888
* [Altitude Pressure](physics/altitude_pressure.py)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
Author : Prince Kumar Prajapati
3+
Date : October 9, 2025
4+
5+
Weighted Job Scheduling Problem
6+
--------------------------------
7+
Given N jobs where every job has a start time, finish time, and profit,
8+
find the maximum profit subset of jobs such that no two jobs overlap.
9+
10+
Approach:
11+
- Sort all jobs by their finish time.
12+
- For each job, find the last non-conflicting job using binary search.
13+
- Use Dynamic Programming to build up the maximum profit table.
14+
15+
Time Complexity: O(n log n)
16+
Space Complexity: O(n)
17+
"""
18+
19+
20+
def find_last_non_conflicting(jobs, index):
21+
"""
22+
Binary search to find the last job that doesn't overlap
23+
with the current job (at index).
24+
"""
25+
low, high = 0, index - 1
26+
while low <= high:
27+
mid = (low + high) // 2
28+
if jobs[mid][1] <= jobs[index][0]:
29+
if mid + 1 < index and jobs[mid + 1][1] <= jobs[index][0]:
30+
low = mid + 1
31+
else:
32+
return mid
33+
else:
34+
high = mid - 1
35+
return -1
36+
37+
38+
def weighted_job_scheduling(jobs):
39+
"""
40+
Function to find the maximum profit from a set of jobs.
41+
42+
Parameters:
43+
jobs (list of tuples): Each tuple represents (start_time, end_time, profit)
44+
Returns:
45+
int: Maximum profit achievable without overlapping jobs.
46+
"""
47+
48+
# Step 1: Sort jobs by their finish time
49+
jobs.sort(key=lambda x: x[1])
50+
51+
n = len(jobs)
52+
dp = [0] * n # dp[i] will store the max profit up to job i
53+
dp[0] = jobs[0][2] # First job profit is the base case
54+
55+
# Step 2: Iterate through all jobs
56+
for i in range(1, n):
57+
# Include current job
58+
include_profit = jobs[i][2]
59+
60+
# Find the last non-conflicting job
61+
j = find_last_non_conflicting(jobs, i)
62+
if j != -1:
63+
include_profit += dp[j]
64+
65+
# Exclude current job (take previous best)
66+
dp[i] = max(include_profit, dp[i - 1])
67+
68+
# Step 3: Return the maximum profit at the end
69+
return dp[-1]
70+
71+
72+
# Example usage / Test case
73+
if __name__ == "__main__":
74+
# Each job is represented as (start_time, end_time, profit)
75+
jobs = [(1, 3, 50), (2, 4, 10), (3, 5, 40), (3, 6, 70)]
76+
77+
print("Maximum Profit:", weighted_job_scheduling(jobs))

0 commit comments

Comments
 (0)