<aside>

This section is part of the 5-hour LeetCode DSA Foundations course by Mukul Raina

SLIDE 121: Section Intro, Greedy Algorithms

Section 13: Greedy Algorithms

A greedy algorithm builds a solution piece by piece, always choosing the option that looks best at the current step, without ever revisiting or undoing a previous choice. When the problem has the right structure, this produces elegant O(n) or O(n log n) solutions. When the problem does not have the right structure, greedy produces confidently wrong answers.

The skill this section builds is knowing the difference. Recognizing when greedy works, when it fails, and what to reach for when it fails is more valuable than memorizing greedy solutions to specific problems.

What We’ll Cover

  1. The greedy principle and the two conditions that make it correct
  2. Interval scheduling as the canonical greedy pattern
  3. Jump Game as a second worked example
  4. When greedy fails and how to recognize the boundary between greedy and dynamic programming
  5. The counterexample test as a validation tool

Connection to Other Sections

Sorting (Section 9) is often a preprocessing step for greedy algorithms. Interval scheduling sorts by end time before making greedy selections. Backtracking (Section 12) explores all possibilities when greedy cannot guarantee correctness. Dynamic programming (covered in C6) handles problems where greedy fails by evaluating all choices instead of committing to one.

<aside>

Mukul Raina | youtube.com/@mukulraina | linkedin.com/in/mukulraina

</aside>


SLIDE 122: The Greedy Principle: When It Works and When It Fails

A greedy algorithm is correct when the problem satisfies two conditions. First, optimal substructure: the optimal solution to the whole problem contains optimal solutions to subproblems. Second, the greedy choice property: choosing the locally optimal option at each step leads to a globally optimal solution. If either condition is missing, greedy can fail silently, producing an answer that looks reasonable but is wrong.

Correct Greedy: Coin Change with Standard Denominations