<aside>
This section is part of the 5-hour LeetCode DSA Foundations course by Mukul Raina
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
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>
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