07. Weighted Job Scheduling
β GFG solution to the Weighted Job Scheduling problem: maximize profit by scheduling non-overlapping jobs using dynamic programming and binary search. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a 2D array jobs[][] of size n Γ 3, where each row represents a single job with the following details:
jobs[i][0]: Start time of the jobjobs[i][1]: End time of the jobjobs[i][2]: Profit earned by completing the job
Your task is to find the maximum profit you can earn by scheduling non-overlapping jobs.
Note: Two jobs are said to be non-overlapping if the end time of one job is less than or equal to the start time of the next job. If a job ends at time X, another job can start exactly at time X.
π Examples
Example 1
Input: jobs[][] = [[1, 2, 50],
[3, 5, 20],
[6, 19, 100],
[2, 100, 200]]
Output: 250
Explanation: The first and fourth jobs with the time range [1, 2] and [2, 100] can be chosen
to give maximum profit of 50 + 200 = 250.Example 2
π Constraints
$1 \le \text{jobs.size()} \le 10^5$
$1 \le \text{jobs}[i][0] < \text{jobs}[i][1] \le 10^9$
$1 \le \text{jobs}[i][2] \le 10^4$
β
My Approach
The optimal approach uses Dynamic Programming with Binary Search to efficiently find the maximum profit by scheduling non-overlapping jobs:
Dynamic Programming + Binary Search
Sort Jobs by Start Time:
First, sort all jobs by their start time. This allows us to process jobs in chronological order and makes binary search possible for finding compatible jobs.
Initialize DP Array:
Create a DP array
dp[i]wheredp[i]represents the maximum profit achievable from job indexito the end.Initialize
dp[n] = 0(base case: no profit after all jobs).
Process Jobs Backwards:
Iterate from the last job to the first (
i = n-1to0).For each job
i, we have two choices:Take the job: Earn
jobs[i][2]profit and add profit from the next compatible job.Skip the job: Take the profit from
dp[i+1].
Find Next Compatible Job:
Use binary search to find the earliest job that starts at or after the current job ends.
Search for the smallest index
nxtwherejobs[nxt][0] >= jobs[i][1].
Update DP Value:
dp[i] = max(jobs[i][2] + dp[nxt], dp[i+1])This represents choosing the maximum between taking and skipping the current job.
Return Result:
The answer is stored in
dp[0], which represents the maximum profit starting from the first job.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the number of jobs. Sorting takes O(n log n), and for each job, we perform a binary search which takes O(log n), resulting in O(n log n) overall.
Expected Auxiliary Space Complexity: O(n), as we use a DP array of size n+1 to store the maximum profit for each position.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated