Job Scheduling with Deadlines in DSA C++ - Time & Space Complexity
We want to understand how the time needed to schedule jobs changes as we add more jobs.
Specifically, how does the scheduling process grow when the number of jobs increases?
Analyze the time complexity of the following code snippet.
#include <vector>
#include <algorithm>
using namespace std;
struct Job {
int id, deadline, profit;
};
void scheduleJobs(vector<Job>& jobs) {
sort(jobs.begin(), jobs.end(), [](Job a, Job b) { return a.profit > b.profit; });
vector<int> result(jobs.size(), -1);
vector<bool> slot(jobs.size(), false);
for (int i = 0; i < jobs.size(); i++) {
for (int j = min((int)jobs.size() - 1, jobs[i].deadline - 1); j >= 0; j--) {
if (!slot[j]) {
slot[j] = true;
result[j] = i;
break;
}
}
}
}
This code schedules jobs to maximize profit by placing each job in the latest available slot before its deadline.
- Primary operation: Nested loops where for each job, we try to find a free slot before its deadline.
- How many times: Outer loop runs once per job (n times), inner loop can run up to n times in worst case.
As the number of jobs increases, the sorting step takes more time, and the nested loops try more slots.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 operations (sorting + nested loops) |
| 100 | About 10,000 operations |
| 1000 | About 1,000,000 operations |
Pattern observation: The operations grow roughly with the square of the number of jobs.
Time Complexity: O(n^2)
This means if you double the number of jobs, the time needed roughly quadruples.
[X] Wrong: "Since we only loop through jobs once, the time is O(n)."
[OK] Correct: Each job may check multiple slots in the inner loop, causing nested loops and more work than just one pass.
Understanding how nested loops affect time helps you explain your code clearly and shows you can analyze efficiency in real tasks.
"What if we used a data structure to find free slots faster? How would the time complexity change?"
Practice
Job Scheduling with Deadlines problem?Solution
Step 1: Understand the problem objective
The problem aims to schedule jobs so that each job finishes before its deadline to earn profit.Step 2: Identify the optimization goal
The goal is to maximize the total profit earned by selecting jobs that fit within their deadlines.Final Answer:
Maximize total profit by scheduling jobs before their deadlines -> Option CQuick Check:
Goal = Maximize profit [OK]
- Ignoring deadlines and scheduling all jobs
- Trying to schedule jobs in input order
- Maximizing number of jobs instead of profit
std::sort and a custom comparator?Solution
Step 1: Understand sorting by profit descending
To sort descending, comparator should return true if first profit is greater than second.Step 2: Check lambda comparator syntax
std::sort(jobs.begin(), jobs.end(), [](Job a, Job b){ return a.profit > b.profit; }); uses a lambda returning a.profit > b.profit, which sorts descending correctly.Final Answer:
std::sort(jobs.begin(), jobs.end(), [](Job a, Job b){ return a.profit > b.profit; }); -> Option BQuick Check:
Descending sort = a.profit > b.profit [OK]
- Using < instead of > for descending order
- Not providing a comparator for custom sorting
- Using default sort which sorts ascending
jobs = [{id:1, deadline:2, profit:100}, {id:2, deadline:1, profit:19}, {id:3, deadline:2, profit:27}, {id:4, deadline:1, profit:25}, {id:5, deadline:3, profit:15}]What is the maximum profit achievable by scheduling these jobs?
Solution
Step 1: Sort jobs by profit descending
Sorted jobs: (1,100,2), (3,27,2), (4,25,1), (2,19,1), (5,15,3)Step 2: Schedule jobs greedily before deadlines
Schedule job 1 at slot 2, job 4 at slot 1, job 5 at slot 3. Jobs 3 and 2 cannot fit without conflict.Step 3: Calculate total profit
Profit = 100 + 25 + 15 = 140. But check if job 3 can replace job 4 for better profit: job 3 (27) > job 4 (25), so schedule job 3 at slot 2, job 4 at slot 1 is not possible because slot 1 is taken by job 4. Instead, schedule job 3 at slot 2, job 2 at slot 1 (profit 19), job 5 at slot 3 (15). Total = 100 + 27 + 15 = 142. Better is job 1 (100), job 3 (27), job 4 (25) at slots 2,1,3 respectively? Slot 3 deadline is 3, so job 4 deadline 1 can't be at 3. So best is job 1 (100), job 3 (27), job 4 (25) at slots 2,1, no slot for job 5. Total 152. Actually, slot 3 is free for job 5 (15). So schedule job 1 at 2, job 4 at 1, job 5 at 3 = 100+25+15=140. Or job 1 at 2, job 3 at 1, job 5 at 3 = 100+27+15=142. Or job 1 at 2, job 4 at 1, job 3 can't fit. So max profit is 142.Final Answer:
142 -> Option DQuick Check:
Max profit = 142 [OK]
- Scheduling jobs ignoring deadlines
- Not sorting by profit first
- Overlapping jobs in same time slot
struct Job { int id, deadline, profit; };
std::vector<Job> jobs = ...;
std::sort(jobs.begin(), jobs.end(), [](Job a, Job b){ return a.profit < b.profit; });
std::vector<int> slot(maxDeadline + 1, -1);
for (auto &job : jobs) {
for (int j = job.deadline; j > 0; j--) {
if (slot[j] == -1) {
slot[j] = job.id;
break;
}
}
}Solution
Step 1: Analyze sorting comparator
The comparator returns true if a.profit < b.profit, which sorts ascending by profit.Step 2: Understand scheduling logic
Jobs must be sorted descending by profit to schedule highest profit jobs first.Final Answer:
Sorting comparator sorts ascending instead of descending -> Option AQuick Check:
Comparator must sort descending [OK]
- Using ascending sort comparator
- Incorrect slot vector size off by one
- Loop direction confusion
jobs = [{id:1, d:4, p:70}, {id:2, d:1, p:80}, {id:3, d:1, p:30}, {id:4, d:1, p:100}, {id:5, d:3, p:40}, {id:6, d:2, p:20}]Using the job scheduling algorithm, what is the maximum profit and which jobs are scheduled?
Solution
Step 1: Sort jobs by profit descending
Sorted: (4,100,1), (2,80,1), (1,70,4), (5,40,3), (3,30,1), (6,20,2)Step 2: Schedule jobs greedily before deadlines
Schedule job 4 at slot 1 (deadline 1), job 6 at slot 2 (deadline 2), job 5 at slot 3 (deadline 3), job 1 at slot 4 (deadline 4). Jobs 2 and 3 cannot fit because slot 1 is taken by job 4.Step 3: Calculate total profit and jobs scheduled
Profit = 100 + 20 + 40 + 70 = 230; Scheduled jobs in order of slots: 4 -> 6 -> 5 -> 1 (slots 1,2,3,4)Final Answer:
Profit: 230; Jobs scheduled: 4 -> 6 -> 5 -> 1 -> Option AQuick Check:
Max profit = 230 with jobs 4,6,5,1 [OK]
- Scheduling multiple jobs in same slot
- Ignoring deadlines when scheduling
- Not sorting jobs by profit first
Ready to prove it?
Pass the test and this pattern moves unknown
