Greedy Pattern

Beginner Algorithms and Data Structures English
Also available: Deutsch
Created by Best · 16.08.2026 at 09:13 UTC

Greedy selects locally optimal steps in the hope of achieving a global optimum. Sometimes this is correct (Huffman, interval scheduling with the right rule), and sometimes it is only a heuristic.

Proof requirement: Matroid or exchange argument, otherwise counterexample.

Where used

Scheduling with deadlines, Huffman-like coding, canonical coin change, activity selection. Fast, but only correct with proof or clear counterexample check.

Depth

A greedy algorithm builds a solution step by step, selecting the next step that is most favorable according to a criterion. Earlier decisions are not re-evaluated during the normal course. However, efficiency alone does not prove that the result is globally optimal.

A correctness proof often uses an exchange argument: An optimal solution can be transformed in such a way that it contains the greedy decision without becoming worse. Alternatively, a greedy-choice property together with optimal substructure shows that the remaining problem retains the same form.

Difficulty levels

  1. Name the selection rule and the remaining subproblem.
  2. Look for a counterexample for a plausible rule.
  3. Formulate an exchange argument for a correct rule.

Pitfalls

An intuitive heuristic is not yet a proof. Especially with weights or constraints, a seemingly favorable decision can restrict later options in such a way that the overall result becomes worse.

University approvals: 0
Tasks
Question 1

What characterizes Greedy?

Question 2

Which proof idea directly supports a greedy choice?

Question 3

Determine the maximum number of non-overlapping activities using the greedy method. The intervals are sorted by end time.

Hint

You can access the start and end of an interval with interval[0] and interval[1]. A for-each loop is possible with int[][].

Starter code is prefilled; replace TODO blocks with your solution.
1 test case will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Algorithms and Data Structures
  • Difficulty: Beginner
  • Completed: 0 users
Creator
Best
Best
BestBuddy