πDay 3. Job Sequencing Problem π§
The problem can be found at the following link: Question Link
Note: Sorry for uploading late, my exam is going on.
π‘ Problem Description:
You are given two arrays: deadline[] and profit[], which represent a set of jobs. Each job:
Has a deadline by which it must be completed.
Has a profit associated with it.
Takes exactly one unit of time to complete.
Only one job can be scheduled at a time.
Your task is to determine:
The maximum number of jobs that can be completed within their deadlines.
The maximum total profit that can be earned by scheduling jobs optimally.
π Example Walkthrough:
Example 1:
Input:
Output:
Explanation:
Jobs 1 and 3 can be scheduled, maximizing profit (20 + 40 = 60).
Example 2:
Input:
Output:
Explanation:
Jobs 1 and 3 can be scheduled, maximizing profit (100 + 27 = 127).
Example 3:
Input:
Output:
Explanation:
Jobs 1, 3, and 4 can be scheduled, maximizing profit (50 + 20 + 30 = 100).
Constraints
$( 1 \leq \text{deadline.size()} == \text{profit.size()} \leq 10^5 )$
$( 1 \leq \text{deadline}[i] \leq \text{deadline.size()} )$
$( 1 \leq \text{profit}[i] \leq 500 )$
π― My Approach:
Greedy + Min Heap
Algorithm Steps:
Sort jobs in ascending order of deadlines (if two jobs have the same deadline, sort by profit).
Use a min heap to keep track of scheduled jobs based on their profits.
Iterate through jobs and schedule them:
If the number of scheduled jobs is less than the current job's deadline, add it to the heap.
If a lower-profit job exists in the heap, replace it with the current job.
Return the total jobs scheduled and the sum of profits from the heap.
π Time and Auxiliary Space Complexity
Expected Time Complexity: $( O(N \log N) )$, due to sorting and heap operations.
Expected Auxiliary Space Complexity: $( O(N) )$, due to storing jobs in a heap.
π Solution Code
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