Greedy & Interval Methods · 15 min · free lesson

Deadline Profit Scheduling: When to Use It

Recognise the problem shape before choosing Deadline Profit Scheduling.

Core idea

Earlier slots remain available for jobs with tighter deadlines. State the input preconditions before applying this invariant.

Algorithm Atlas · worked example

See the algorithm, step by step

On your device

Take jobs in descending profit and place each in the latest free slot at or before its deadline.

O(n log n) time · O(n) space82 execution steps
main · before line 27Initialize a local value

This call is starting. Local values appear as they are initialized.

Data & variables

Changed Source index
Example inputjobs=(d2,p100),(d1,p19),(d2,p27),(d1,p25),(d3,p15)

Follow the code. Data appears here when it enters scope.

Call stack · 1 frame
  1. maincall 1

Rust source

Line 27
fn maximum_profit(jobs: &[(usize, i32)]) -> i32 {
    fn free(parent: &mut [usize], x: usize) -> usize {
        if parent[x] != x {
            parent[x] = free(parent, parent[x]);
        }
        parent[x]
    }
    let mut sorted = jobs.to_vec();
    sorted.sort_by_key(|&(d, p)| (std::cmp::Reverse(p), d));
    let n = jobs.len();
    let mut parent: Vec<_> = (0..=n).collect();
    let mut profit = 0;
    for (deadline, value) in sorted {
        if value <= 0 {
            continue;
        }
        let slot = free(&mut parent, deadline.min(n));
        if slot > 0 {
            profit += value;
            parent[slot] = free(&mut parent, slot - 1);
        }
    }
    profit
}
 
fn main() {
    let result = maximum_profit(&[(2, 100), (1, 19), (2, 27), (1, 25), (3, 15)]).to_string();
    println!("{result}");
}

Try your own version

Compare the visible input `jobs=(d2,p100),(d1,p19),(d2,p27),(d1,p25),(d3,p15)` with `jobs=(d1,p3),(d1,p9),(d1,p6)`. Predict why their results are `142` and `9`. Then change one legal value and explain whether the same invariant, earlier slots remain available for jobs with tighter deadlines, still applies.

Open personal copy in Playground ↗

Start with this exact source and input, change a value, then use Run or Debug locally. This replay stays attached to the original example.

Recordings use a native Rust target. Playground runs on wasm32-wasip1; target-sized types such as usize can have different limits.

Recorded from this Rust example and checked against its original output. Playback runs locally in your browser. Values show the state before the highlighted line. Borrowed or moved values are hidden when they cannot be inspected safely.
Five-panel Greedy & Interval Methods infographic; panel 4 illustrates Deadline Scheduling.
Deadline Scheduling: follow panel 4 to see the operation, then trace this lesson's example.

Understand it

Take profit 100 with deadline 2 into slot 2, then profit 27 into slot 1, and profit 15 into slot 3. Total profit is 142. DSU predecessor links find the latest free slot without scanning every earlier time.

The worked program is a complete executable for this pattern. Its core function uses typed input so the state transitions remain visible. The challenge input contract is: Each job takes one time slot and earns its profit only if it runs in a slot no later than its 1-based deadline. Return the maximum total profit. The visible fixture is jobs=(d2,p100),(d1,p19),(d2,p27),(d1,p25),(d3,p15) and its example result is 142. The implementation and additional semantic fixtures were checked against an independent reference calculation. Those bounded tests establish these examples; they are not a proof for all inputs or a measurement on an iPhone.

Return a canonical String from solve: lowercase true/false for booleans; decimal integers without extra signs, leading zeros or whitespace; comma-separated sequence values and semicolon-separated groups. Whole-number medians have no decimal suffix; half medians use .5. Do not append a newline to the returned value. Preserve the explicitly named textual sentinels such as none or #. Input fixtures are bounded so the demonstrated arithmetic fits its named Rust types; satisfy the stated input preconditions before applying the algorithm.

String and character-grid fixtures use ASCII unless the lesson explicitly specifies Unicode scalar values.

A problem is a good match for Deadline Profit Scheduling when it asks for max reward under unit-time deadlines. Here the concrete task is: Each job takes one time slot and earns its profit only if it runs in a slot no later than its 1-based deadline. Return the maximum total profit. The visible input jobs=(d2,p100),(d1,p19),(d2,p27),(d1,p25),(d3,p15) is an example, not a definition of the whole problem. Ask what operations the solution must support and what the input guarantees. When the method sorts intervals or events, use the coordinate and tie break required by its proof. Other greedy walks keep an invariant without sorting. The right choice follows from those guarantees, not from a familiar noun in the prompt.

The decisive invariant is: Take jobs in descending profit and place each in the latest free slot at or before its deadline. Try to say why it applies to the task without quoting the implementation. Which fact becomes known after one update, and which possible answers remain? A greedy step commits to a local choice. Give the exchange or dominance reason that an optimal solution can still include that choice. If that claim cannot be maintained on a legal input, the pattern is wrong or one of its preconditions is missing. A counterexample is more useful than a vague warning: change one property of the sample, trace the first two steps, and identify the exact claim that fails.

Compare costs before committing. The advertised bound is O(n log n) time · O(n) space; identify the operation responsible for it and the extra memory it needs. Then compare a straightforward alternative: a direct scan, full sort, or repeated recomputation may be easier but can pay for the same work many times. Do not choose the more elaborate method just because it has a famous name. For the sample, predict 142 and explain which part of the state lets the method reach that result.

When the prompt is ambiguous, ask about input size, ordering, duplicates, and required output format. Test touching intervals, equal endpoints, empty input, and a locally attractive choice that blocks a better future choice. Keep interval endpoint conventions explicit where they apply. If sorting is part of the method, its Rust comparator must encode the proof's exact tie break. The Rust sketch can show a useful primitive without proving that the whole task is solved; connect each primitive to the requested output. You should be able to name a fitting case, a near miss, and the assumption separating them before writing the first line.

The pattern

Take jobs in descending profit and place each in the latest free slot at or before its deadline.

When to use it: max reward under unit-time deadlines

Complexity: O(n log n) time · O(n) space

Input: jobs=(d2,p100),(d1,p19),(d2,p27),(d1,p25),(d3,p15)

Read the code

fn maximum_profit(jobs: &[(usize, i32)]) -> i32 {
    fn free(parent: &mut [usize], x: usize) -> usize {
        if parent[x] != x {
            parent[x] = free(parent, parent[x]);
        }
        parent[x]
    }
    let mut sorted = jobs.to_vec();
    sorted.sort_by_key(|&(d, p)| (std::cmp::Reverse(p), d));
    let n = jobs.len();
    let mut parent: Vec<_> = (0..=n).collect();
    let mut profit = 0;
    for (deadline, value) in sorted {
        if value <= 0 {
            continue;
        }
        let slot = free(&mut parent, deadline.min(n));
        if slot > 0 {
            profit += value;
            parent[slot] = free(&mut parent, slot - 1);
        }
    }
    profit
}

fn main() {
    let result = maximum_profit(&[(2, 100), (1, 19), (2, 27), (1, 25), (3, 15)]).to_string();
    println!("{result}");
}

Complete std-only deadline-scheduling program for its typed teaching fixture. Parsing and output construction are separate from the core algorithm.

Try it

Compare the visible and independent fixtures. Explain the input assumption that makes this algorithm applicable, then identify a change that would invalidate it.

What success looks like: You can justify the choice from constraints rather than from a remembered problem title.

Check your understanding

Trace Deadline Profit Scheduling on input jobs=(d1,p10),(d2,p20). Which result follows from the documented contract?

Reveal the answer
  1. 31
  2. 30
  3. 29

For this input the result is 30. Earlier slots remain available for jobs with tighter deadlines. Compare this independent fixture with the worked trace: Take profit 100 with deadline 2 into slot 2, then profit 27 into slot 1, and profit 15 into slot 3. Total profit is 142. DSU predecessor links find the latest free slot without scanning every earlier time.

Go deeper

Common misconception: “Every job must occupy its deadline slot even if busy”

Earlier slots remain available for jobs with tighter deadlines. Take profit 100 with deadline 2 into slot 2, then profit 27 into slot 1, and profit 15 into slot 3. Total profit is 142. DSU predecessor links find the latest free slot without scanning every earlier time.

  1. Concrete transition: Take profit 100 with deadline 2 into slot 2, then profit 27 into slot 1, and profit 15 into slot 3. Total profit is 142. DSU predecessor links find the latest free slot without scanning every earlier time.
  2. Independent second fixture: Input jobs=(d1,p10),(d2,p20) produces 30. Execute the named function in the worked program with these typed arguments, then compare its actual result with this independently computed expectation.
  3. Boundary and contract: Input jobs=(d3,p5),(d1,p7),(d2,p8) produces 20. Earlier slots remain available for jobs with tighter deadlines. The stated work bound is O(n log n) time · O(n) space. Count actual state changes; a successful output test alone does not prove that bound.

Transfer challenge: Compare the visible input jobs=(d2,p100),(d1,p19),(d2,p27),(d1,p25),(d3,p15) with jobs=(d1,p3),(d1,p9),(d1,p6). Predict why their results are 142 and 9. Then change one legal value and explain whether the same invariant, earlier slots remain available for jobs with tighter deadlines, still applies.

Connections

  • Deadline Profit Scheduling: Mental Model: Take jobs in descending profit and place each in the latest free slot at or before its deadline.
  • Challenge: Deadline Profit Scheduling: Each job takes one time slot and earns its profit only if it runs in a slot no later than its 1-based deadline. Return the maximum total profit.
Reveal the Algorithm Atlas answer

142

Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Deadline Profit Scheduling: When to Use It (courseId "algorithms", lessonId "algorithm.deadline-scheduling.recognize")
Link: https://crabrix.com/learn/algorithms/algorithm.deadline-scheduling.recognize
Goal: Earlier slots remain available for jobs with tighter deadlines. State the input preconditions before applying this invariant.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=algorithms/algorithm.deadline-scheduling.recognize) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.