2Brute2Best
🔥0

Job Sequencing Problem

Medium·GeeksforGeeks Pro OnlySlight Variation
GeeksforGeeks
01

Problem Statement

Each job takes exactly 1 unit of time, and only one job runs at a time. The time slots are 1, 2, 3, and so on.

A job earns its profit only if it finishes by its deadline. So job i must run in a slot that is at or before deadline[i].

Pick the set of jobs that earns the most profit in total. Return two numbers: how many jobs you picked, and the total profit.

Input

Two arrays deadline and profit of the same length. Job i has deadline deadline[i] and profit profit[i].

Output

A list [count, totalProfit]: the number of jobs picked for the maximum profit, and that maximum profit.

Example 1
Input: deadline = [4, 1, 1, 1], profit = [20, 10, 40, 30]
Output: [2, 60]

Job 1 (deadline 4, profit 20) and job 3 (deadline 1, profit 40) give 20 + 40 = 60. Only one job can use slot 1.

Example 2
Input: deadline = [2, 1, 2, 1, 1], profit = [100, 19, 27, 25, 15]
Output: [2, 127]

Job 1 (profit 100) and job 3 (profit 27) use slots 1 and 2. Total 127.

Example 3
Input: deadline = [3, 1, 2, 2], profit = [50, 10, 20, 30]
Output: [3, 100]

Jobs 1, 3 and 4 (50 + 20 + 30) fit in slots 3, 1 and 2.

Constraints
  • • The number of jobs is at most 10^5, and `deadline` and `profit` have the same length.
  • • 1 ≤ deadline[i] ≤ n, where n is the number of jobs
  • • 1 ≤ profit[i] ≤ 500
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“each job takes 1 unit of time”
→ One slot holds one job, so a deadline d means at most d jobs can finish by then.
“profit only if done by its deadline”
→ Jobs can be dropped. This is pick-the-best-set, not 'do them all'.
“maximum total profit”
→ Profits are positive, so a bigger set is always better, as long as it still fits.
03

Approach Complexity Ladder

Brute Force — try every subset of jobsO(2^N · N log N)O(N)

Try every subset; keep the most profitable one that can run on time

Better — sort by profit, take the latest free slotO(N · D)O(N)

Sort by profit (highest first); each job takes the latest free slot up to its deadline

Optimal — sort by deadline, keep a min-heap of profitsO(N log N)O(N)

Sort by deadline; keep a min-heap of profits; when it grows past the deadline, pop the smallest

Brute Force Intuition

There are 2^N subsets of jobs. For each subset, check whether it can run on time: sort its deadlines, and the k-th job (1-based) must have deadline ≥ k. Keep the feasible subset with the largest total profit, and report its size.

Recognition rule for this sub-pattern:

Sort by the right key (ratio, deadline, height); argue that swapping any two out-of-order items cannot help.
Remember this · Greedy
Trigger
Maximise count / minimise cost with a natural "best next step", sorting makes the choice obvious, reachability.
Think
Is there a choice I can always make safely without looking ahead?
Key question
Why can't the greedy choice be worse than the optimal one? (Try to break it with a small example.)
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.