Job Scheduling with Deadlines in DSA C - Time & Space Complexity
We want to understand how the time needed to schedule jobs changes as the number of jobs grows.
How does the scheduling process scale when we add more jobs with deadlines?
Analyze the time complexity of the following code snippet.
// Job Scheduling with Deadlines
// jobs[] contains pairs of (deadline, profit)
// n is number of jobs
void jobScheduling(Job jobs[], int n) {
// Sort jobs by profit descending
qsort(jobs, n, sizeof(Job), compareProfit);
int result[n];
bool slot[n];
for (int i = 0; i < n; i++) slot[i] = false;
for (int i = 0; i < n; i++) {
for (int j = 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 free slot before its deadline.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where for each job we try to find a free slot before its deadline.
- How many times: Outer loop runs n times (for each job), inner loop can run up to n times in worst case (checking slots).
As the number of jobs increases, the number of checks to find free slots grows roughly with the square of the number of jobs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 checks |
| 100 | About 10,000 checks |
| 1000 | About 1,000,000 checks |
Pattern observation: The operations grow roughly by the square of the input size.
Time Complexity: O(n2)
This means if you double the number of jobs, the time to schedule them roughly quadruples.
[X] Wrong: "The scheduling always takes linear time because we just loop through jobs once."
[OK] Correct: Each job may require checking multiple slots before finding a free one, causing nested loops and more work as jobs increase.
Understanding how nested loops affect time helps you explain and improve scheduling algorithms clearly in real situations.
"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 asks to schedule jobs so that the total profit is maximized while respecting each job's deadline.Step 2: Identify the goal
The goal is to select jobs that fit within their deadlines and yield the highest total profit.Final Answer:
Maximize the total profit by scheduling jobs within their deadlines -> Option DQuick Check:
Goal = Maximize profit within deadlines [OK]
- Ignoring deadlines and scheduling all jobs
- Trying to minimize jobs instead of maximizing profit
- Scheduling jobs without considering profit
Solution
Step 1: Identify the sorting function in C
In C,qsortis used for sorting arrays with a comparator function.Step 2: Check the correct parameters
The correct call isqsort(array, number_of_elements, size_of_element, comparator_function). qsort(jobs, n, sizeof(Job), compareProfitDesc); matches this.Final Answer:
qsort(jobs, n, sizeof(Job), compareProfitDesc); -> Option CQuick Check:
Use qsort with correct parameters in C [OK]
- Using C++ sort() in C code
- Passing wrong size or count parameters
- Using ascending comparator instead of descending
Solution
Step 1: Sort jobs by profit descending
Order: Job1(20,2), Job2(15,2), Job3(10,1), Job4(5,3)Step 2: Schedule jobs greedily within deadlines
Schedule Job1 at slot 2, Job2 at slot 1 (since slot 2 taken), Job4 at slot 3. Job3 cannot be scheduled (slot 1 taken). Total profit = 20+15+5=40.Final Answer:
40 -> Option AQuick Check:
Greedy scheduling = 40 profit [OK]
- Scheduling jobs ignoring deadlines
- Overlapping jobs in same slot
- Counting all jobs without checking slot availability
for (int i = 0; i < n; i++) {
int slot = jobs[i].deadline - 1;
while (slot >= 0 && result[slot] != -1) {
slot--;
}
if (slot >= 0) {
result[slot] = i;
}
}Solution
Step 1: Check initialization of result array
The array 'result' stores scheduled jobs. It must be initialized to -1 to mark free slots.Step 2: Identify missing initialization
The code snippet does not show initialization of 'result' to -1, which can cause incorrect scheduling.Final Answer:
The array 'result' is not initialized to -1 before use -> Option BQuick Check:
Initialize result array to -1 before scheduling [OK]
- Forgetting to initialize result array
- Wrong loop direction for scheduling
- Incorrect slot boundary conditions
Solution
Step 1: Sort jobs by profit descending
Order: J1(100,2), J3(27,2), J4(25,1), J2(19,1)Step 2: Schedule jobs greedily within deadlines
Schedule J1 at slot 2, J3 at slot 1 (slot 1 free). J4 and J2 cannot be scheduled as slot 1 is taken. Total profit = 100 + 27 = 127.Final Answer:
Profit=127; Jobs scheduled: J1 at slot 2, J3 at slot 1 -> Option AQuick Check:
Greedy scheduling = 127 profit [OK]
- Scheduling multiple jobs in same slot
- Ignoring deadlines when scheduling
- Adding profits of unscheduled jobs
Ready to prove it?
Pass the test and this pattern moves unknown
