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 have more jobs with deadlines?
Analyze the time complexity of the following code snippet.
// Job class with deadline and profit
class Job {
public int Id, Deadline, Profit;
public Job(int id, int deadline, int profit) {
Id = id; Deadline = deadline; Profit = profit;
}
}
// Scheduling jobs to maximize profit
void JobScheduling(Job[] jobs, int n) {
Array.Sort(jobs, (a, b) => b.Profit.CompareTo(a.Profit));
bool[] slot = new bool[n];
for (int i = 0; i < n; i++) {
for (int j = Math.Min(n, jobs[i].Deadline) - 1; j >= 0; j--) {
if (!slot[j]) { slot[j] = true; break; }
}
}
}
This code sorts jobs by profit and tries to schedule each job before its deadline in the latest free slot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where for each job we check slots backward to find a free spot.
- How many times: Outer loop runs n times (for each job), inner loop runs up to n times (checking slots).
As the number of jobs increases, the number of checks grows roughly by the square of the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 100 checks |
| 100 | Up to 10,000 checks |
| 1000 | Up to 1,000,000 checks |
Pattern observation: Doubling the number of jobs roughly quadruples the work done.
Time Complexity: O(n2)
This means the time needed grows roughly with the square of the number of jobs.
[X] Wrong: "Since we sort jobs first, the scheduling must be fast and close to linear time."
[OK] Correct: Sorting is only part of the work; the nested loops to find free slots dominate and cause quadratic growth.
Understanding this time complexity helps you explain how scheduling algorithms behave as tasks increase, showing your grasp of algorithm efficiency.
"What if we used a more efficient data structure to find free slots? How would the time complexity change?"
Practice
Job Scheduling with Deadlines problem?Solution
Step 1: Understand the problem goal
The problem aims to schedule jobs so that each job finishes before its deadline.Step 2: Identify the optimization criteria
We want to maximize the total profit earned by scheduling jobs within their deadlines.Final Answer:
Maximize total profit by scheduling jobs before their deadlines -> Option AQuick Check:
Goal = Maximize profit [OK]
- Thinking the goal is to schedule all jobs regardless of profit
- Ignoring deadlines when scheduling
- Assuming jobs must be scheduled in input order
Solution
Step 1: Identify sorting by profit descending
Sorting by profit descending means higher profit first, so compare b.Profit to a.Profit.Step 2: Check C# syntax for Array.Sort with custom comparer
Array.Sort with lambda (a, b) => b.Profit.CompareTo(a.Profit) sorts descending by profit.Final Answer:
Array.Sort(jobs, (a, b) => b.Profit.CompareTo(a.Profit)); -> Option BQuick Check:
Descending profit sort = Array.Sort(jobs, (a, b) => b.Profit.CompareTo(a.Profit)); [OK]
- Using a.Profit.CompareTo(b.Profit) which sorts ascending
- Using OrderBy instead of OrderByDescending for descending
- Sorting by deadline instead of profit
Solution
Step 1: Sort jobs by profit descending
Sorted jobs: (1,2,100), (3,2,27), (4,1,25), (2,1,19), (5,3,15)Step 2: Schedule jobs greedily before deadlines
Schedule Job 1 at slot 2, Job 3 at slot 1 (since slot 2 taken), Job 5 at slot 3. Jobs 4 and 2 cannot be scheduled as slots 1 and 2 are taken.Step 3: Calculate total profit
Profit = 100 + 27 + 15 = 142Final Answer:
142 -> Option CQuick Check:
Max profit = 142 [OK]
- Scheduling jobs without sorting by profit
- Assigning jobs to earliest slots instead of latest free slot
- Counting jobs that miss deadlines
Array.Sort(jobs, (a, b) => a.Profit.CompareTo(b.Profit));
foreach (var job in jobs) {
for (int slot = job.Deadline; slot > 0; slot--) {
if (timeSlots[slot] == false) {
timeSlots[slot] = true;
totalProfit += job.Profit;
break;
}
}
}Solution
Step 1: Check sorting order
Sorting by a.Profit.CompareTo(b.Profit) sorts ascending, but we need descending to maximize profit.Step 2: Verify scheduling logic
The loop tries to assign job to latest free slot before deadline, which is correct.Final Answer:
Jobs are sorted in ascending profit order, should be descending -> Option DQuick Check:
Sort descending by profit [OK]
- Sorting ascending profit by mistake
- Incorrect slot iteration direction
- Misindexing timeSlots array
Solution
Step 1: Sort jobs by profit descending
Sorted: (3,1,40), (4,1,30), (1,4,20), (2,1,10)Step 2: Schedule jobs in latest free slot before deadline
Job 3 scheduled at slot 1, Job 4 cannot be scheduled (slot 1 taken), Job 1 scheduled at slot 4, Job 2 cannot be scheduled (slot 1 taken)Step 3: Calculate total profit
Total profit = 40 + 20 = 60Final Answer:
Jobs: 3, 1; Total Profit: 60 -> Option AQuick Check:
Schedule highest profit jobs first [OK]
- Trying to schedule all jobs ignoring slot availability
- Scheduling jobs in input order
- Not checking slot availability correctly
Ready to prove it?
Pass the test and this pattern moves unknown
