trending_upGreedy

Greedy: when to spot it, explain it, and practice it

Greedy is not a pattern you recognize from syntax. You recognize it when one local choice can be proven safe because any better global solution can be transformed to match it without getting worse.

Pattern coverage

45+

Best first move

Name the greedy choice explicitly.

Common failure point

Choosing greedily without any argument against counterexamples.

When this pattern should come to mind

A local choice seems obvious and you can argue why delaying it never helps.
Sorting by one key reveals a structure that simplifies every later decision.
Dynamic programming feels possible but too heavy for the input size or problem intent.

Checklist before you code

Name the greedy choice explicitly.
Explain why another optimal solution can be adjusted to make the same choice.
Check whether sorting is part of the proof or just a convenience.
Test counterexamples before trusting the greedy idea.

The solving flow that works well in interviews

1

Identify the local choice that seems to preserve the most future flexibility.

2

Sort or preprocess if that makes the choice testable.

3

State the invariant after taking that greedy action.

4

Use a proof sketch or exchange argument to justify it.

5

Only then implement the linear scan or boundary tracking.

Common variants

Interval scheduling

Sort by end time or start time based on what keeps future choices widest.

Reachability greedy

Track the best boundary or farthest reach so far.

Resource allocation

Pick the choice that minimizes future cost or overlap.

Template preview

PythonPublic preview
items.sort(key=priority)
answer = 0
for item in items:
    if can_take(item):
        take(item)
        answer = update(answer)

A more useful problem ladder for practice

This is not a random list. It is ordered to help candidates build recognition first, add key variants next, and then increase pressure with harder cases.

High-frequency mistakes

warning

Choosing greedily without any argument against counterexamples.

warning

Sorting by a key that feels plausible but is not part of a proof.

warning

Mistaking a monotonic boundary problem for DP or vice versa.

warning

Only explaining implementation and never explaining why greedy works.

Recommended practice path

1

Start with jump game and interval merge/removal.

2

Then solve arrow shooting and queue reconstruction.

3

Add partition or token-budget style greedy problems next.

4

Finish with harder proof-heavy greedy questions after the intuition settles.

Greedy Pattern Guide | LeetCode Interview Prep - Interview AiBox