Job Sequencing Problem — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Job Sequencing Problem
Detailed explanation and reference materials
Problem Overview

Job Sequencing Problem

Problem Description

In this problem, we are given a set of n jobs. Each job has the following parameters:

  1. ID: Unique identifier for the job.
  2. Deadline: The time by which the job must be completed.
  3. Profit: The profit associated with completing the job.

Goal

Determine the optimal sequence of jobs that maximizes the profit while ensuring that no two jobs are executed simultaneously and each job is completed within its deadline.


Steps to Solve the Problem

1. Sort Jobs by Profit (Descending Order):

Sort the jobs in descending order based on their profits. This ensures that the most profitable jobs are considered first.


2. Find the Maximum Deadline:

Find the maximum deadline among all jobs. This determines the number of time slots available.


3. Initialize Variables:

  • slots: An array of size maxDeadline initialized with -1. This array tracks which job is assigned to each time slot.
  • maxProfit: Initialize to 0 to track the maximum profit achieved.
  • count: Initialize to 0 to count the number of jobs included in the optimal sequence.

4. Iterate Over Jobs:

For each job in the sorted list:

  • Check if there is an available time slot before the job's deadline.
  • If an available slot is found:
    • Assign the job to that slot.
    • Increment the job count.
    • Add the job's profit to maxProfit.

5. Return Results:

After iterating through all jobs, return:

  • count: Total number of jobs included in the sequence.
  • maxProfit: Maximum profit achieved.

Example

Input:

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} ]

Steps:

  1. Sort Jobs by Profit:
    Sorted jobs:
    [ {id: 1, profit: 100}, {id: 4, profit: 25}, {id: 3, profit: 27}, {id: 2, profit: 19}, {id: 5, profit: 15} ]
  2. Maximum Deadline:
    Maximum deadline = 3
  3. Assign Jobs to Slots:
    • Job 1: Assigned to slot 2.
    • Job 3: Assigned to slot 1.
    • Job 5: Assigned to slot 3.

Output:

count = 3 (Jobs 1, 3, and 5)
maxProfit = 142


Test Cases

Test Case 1

Input:
jobs = [ {id: 1, deadline: 2, profit: 50}, {id: 2, deadline: 1, profit: 40}, {id: 3, deadline: 2, profit: 60}, {id: 4, deadline: 1, profit: 20} ]

Output:
count = 2 (Jobs 3 and 1)
maxProfit = 110


Test Case 2

Input:
jobs = [ {id: 1, deadline: 1, profit: 10}, {id: 2, deadline: 2, profit: 20}, {id: 3, deadline: 3, profit: 30} ]

Output:
count = 3 (Jobs 3, 2, and 1)
maxProfit = 60


Test Case 3

Input:
jobs = [ {id: 1, deadline: 3, profit: 20}, {id: 2, deadline: 3, profit: 15}, {id: 3, deadline: 1, profit: 10} ]

Output:
count = 2 (Jobs 1 and 3)
maxProfit = 30


Constraints

  1. 1 <= n <= 1000
  2. 1 <= profit[i], deadline[i] <= 10^4

Complexity

Time Complexity:

  • Sorting: O(n log n)
  • Assigning Jobs to Slots: O(n * d) (where d is the maximum deadline)

Space Complexity:

  • O(d) for the slots array.
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
array
java
Reference Link
https://www.geeksforgeeks.org/problems/job-sequencing-problem-1587115620/1

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.