📖 Scenario: Imagine you run a small workshop where you receive several jobs each day. Each job has a deadline and a profit if completed on time. You want to schedule jobs to maximize your total profit without missing deadlines.
🎯 Goal: Build a simple program that schedules jobs with deadlines to maximize profit. You will create a list of jobs, set a maximum deadline, select jobs to maximize profit, and finally print the scheduled jobs.
📋 What You'll Learn
Create an array of job objects with exact properties: id, deadline, and profit
Create a variable for the maximum deadline
Implement the job scheduling logic to select jobs maximizing profit without missing deadlines
Print the scheduled jobs in order of their deadlines
💡 Why This Matters
🌍 Real World
Job scheduling algorithms are used in manufacturing, computer task scheduling, and project management to maximize efficiency and profit.
💼 Career
Understanding job scheduling helps in roles like software development, operations management, and systems engineering where task prioritization is key.
Progress0 / 4 steps
1
Create the jobs array
Create an array called jobs with these exact job objects: {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}
DSA Javascript
Hint
Use const jobs = [ ... ] and include all job objects exactly as shown.
2
Set the maximum deadline
Create a variable called maxDeadline and set it to 3.
DSA Javascript
Hint
Use const maxDeadline = 3; exactly.
3
Schedule jobs to maximize profit
Create a variable called scheduledJobs and assign it the result of scheduling jobs to maximize profit without missing deadlines. Use these steps: sort jobs by profit descending, create an array result of length maxDeadline filled with null, then for each job, place it in the latest free slot before its deadline in result. Assign result to scheduledJobs.
DSA Javascript
Hint
Sort jobs by profit descending, create an array of nulls for slots, then assign jobs to the latest free slot before their deadline.
4
Print the scheduled jobs
Use console.log to print the scheduled jobs in order of their deadlines. Print the job id for each scheduled job separated by -> . Skip null slots. For example, print a -> c -> e.
DSA Javascript
Hint
Filter out nulls, map to job ids, join with ' -> ', and print using console.log.
Practice
(1/5)
1. What is the main goal of the Job Scheduling with Deadlines problem?
easy
A. Maximize the total profit by scheduling jobs before their deadlines
B. Minimize the number of jobs scheduled
C. Schedule jobs in the order they appear without considering deadlines
D. Maximize the number of jobs scheduled regardless of profit
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 A
Quick Check:
Goal = Maximize profit before deadlines [OK]
Hint: Focus on profit and deadlines to maximize earnings [OK]
Common Mistakes:
Ignoring deadlines and scheduling all jobs
Trying to schedule jobs in input order
Maximizing number of jobs instead of profit
2. Which of the following is the correct way to sort jobs by profit in descending order in JavaScript?
easy
A. jobs.sort((a, b) => a.profit - b.profit);
B. jobs.sort((a, b) => b.profit - a.profit);
C. jobs.sort((a, b) => a.deadline - b.deadline);
D. jobs.sort((a, b) => b.deadline - a.deadline);
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 B
Quick Check:
Descending profit sort = b.profit - a.profit [OK]
Hint: Use b - a for descending numeric sort in JavaScript [OK]