Job Scheduling with Deadlines in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to schedule jobs grows as the number of jobs increases.
How does the scheduling process scale when we have more jobs with deadlines?
Analyze the time complexity of the following code snippet.
function scheduleJobs(jobs) {
jobs.sort((a, b) => b.profit - a.profit);
const result = [];
const slot = Array(jobs.length).fill(false);
for (let i = 0; i < jobs.length; i++) {
for (let j = Math.min(jobs[i].deadline - 1, jobs.length - 1); j >= 0; j--) {
if (!slot[j]) {
slot[j] = true;
result[j] = jobs[i];
break;
}
}
}
return result;
}
This code schedules jobs to maximize profit by placing each job in the latest available slot before its deadline.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where the outer loop goes through each job and the inner loop searches for a free slot before the job's 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 grows, the sorting takes more time, and the nested loops check slots for each job.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 operations |
| 100 | About 10,000 operations |
| 1000 | About 1,000,000 operations |
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: "Since we only loop through jobs once, the time is O(n)."
[OK] Correct: The inner loop that finds a free slot can run many times for each job, making the total work much larger than just one pass.
Understanding how nested loops affect time helps you explain your approach clearly and shows you can analyze efficiency in real problems.
"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 goal
The problem aims to schedule jobs so that each job finishes before its deadline and the total profit is maximized.Step 2: Identify the objective
Maximizing total profit means choosing jobs carefully, not just scheduling all or in order.Final Answer:
Maximize the total profit by scheduling jobs before their deadlines -> Option AQuick Check:
Goal = Maximize profit before deadlines [OK]
- Ignoring deadlines and scheduling all jobs
- Trying to schedule jobs in input order
- Maximizing number of jobs instead of profit
Solution
Step 1: Understand sorting by profit descending
To sort descending, subtract b.profit - a.profit so higher profits come first.Step 2: Check the options
jobs.sort((a, b) => b.profit - a.profit); uses b.profit - a.profit, which sorts descending by profit correctly.Final Answer:
jobs.sort((a, b) => b.profit - a.profit); -> Option BQuick Check:
Descending profit sort = b.profit - a.profit [OK]
- Using a.profit - b.profit which sorts ascending
- Sorting by deadline instead of profit
- Confusing ascending and descending order
const jobs = [
{id: 'a', deadline: 2, profit: 100},
{id: 'b', deadline: 1, profit: 19},
{id: 'c', deadline: 2, profit: 27},
{id: 'd', deadline: 1, profit: 25},
{id: 'e', deadline: 3, profit: 15}
];After scheduling jobs to maximize profit with deadlines, which jobs are selected (in order of scheduling)?
Solution
Step 1: Sort jobs by profit descending
Sorted: a(100,2), c(27,2), d(25,1), b(19,1), e(15,3)Step 2: Schedule jobs greedily before deadlines
Schedule 'a' at latest slot <=2 (slot 2), 'c' at latest <=2 (slot 1 since 2 taken), skip 'b' (slot 1 taken), 'd' at slot 1 (since 'c' was assigned slot 1, 'd' cannot be assigned), 'e' at latest <=3 (slot 3). Correct scheduling is 'a' at slot 2, 'd' at slot 1, 'e' at slot 3. Order: ['a', 'd', 'e']Final Answer:
['a', 'd', 'e'] -> Option DQuick Check:
Max profit jobs scheduled = ['a', 'd', 'e'] [OK]
- Scheduling jobs only by deadline order
- Ignoring profit and picking lower profit jobs
- Not checking slot availability before deadline
jobs.sort((a, b) => b.profit - a.profit);
const result = [];
const slot = Array(jobs.length).fill(false);
for (let job of jobs) {
for (let j = 0; j <= job.deadline; j++) {
if (!slot[j]) {
slot[j] = true;
result.push(job.id);
break;
}
}
}
console.log(result);What is the main bug in this code?
Solution
Step 1: Analyze slot assignment loop
The inner loop checks slots from 0 up to deadline, but slots should be checked from deadline down to 1 to assign latest free slot.Step 2: Understand correct slot checking order
Checking from deadline down ensures jobs are scheduled as late as possible before deadline, maximizing slot usage.Final Answer:
The inner loop should run from job.deadline down to 1, not 0 up to deadline -> Option CQuick Check:
Check slots backward from deadline [OK]
- Checking slots forward from 0 wastes early slots
- Initializing slot array incorrectly
- Confusing sorting criteria
const jobs = [
{id: 'j1', deadline: 4, profit: 70},
{id: 'j2', deadline: 1, profit: 80},
{id: 'j3', deadline: 1, profit: 30},
{id: 'j4', deadline: 2, profit: 100},
{id: 'j5', deadline: 3, profit: 40}
];Using job scheduling with deadlines, what is the maximum total profit and which jobs are scheduled?
Solution
Step 1: Sort jobs by profit descending
Sorted: j4(100,2), j2(80,1), j1(70,4), j5(40,3), j3(30,1)Step 2: Schedule jobs greedily before deadlines
Assign j4 at slot 2, j2 at slot 1, j1 at slot 4, j5 at slot 3. j3 cannot fit. Total profit: 100+80+70+40=290. Scheduled: ['j4', 'j2', 'j1', 'j5']Final Answer:
Total profit 290; jobs scheduled: ['j4', 'j2', 'j1', 'j5'] -> Option AQuick Check:
Max profit = 290 with jobs j4,j2,j1,j5 [OK]
- Not assigning jobs to latest possible slot
- Miscounting total profit
- Ignoring job deadlines when scheduling
Ready to prove it?
Pass the test and this pattern moves unknown
