All Patterns

Greedy

Make locally optimal choices at each step, proving they lead to a globally optimal solution.

When to use

  • • Optimal substructure + greedy choice property
  • • Activity selection
  • • Huffman coding
  • • Fractional knapsack

When NOT to use

  • • Local optimum doesn't guarantee global optimum
  • • Need to consider all combinations (use DP)

Common traps

  • • Assuming greedy works without proof
  • • Wrong sorting criterion
  • • Not handling ties correctly

Key Invariant

A greedy algorithm works only if the locally optimal choice is also globally optimal — prove it or use DP

Problems (10)

#151Jump Game II
medium
#152Gas Station
medium
#154Non-overlapping Intervals
medium
#155Partition Labels
medium
#156Queue Reconstruction by Height
medium
#157Minimum Number of Arrows to Burst Balloons
medium
#159Merge Intervals
medium
#161Maximum Number of Events That Can Be Attended
medium
#259Valid Parenthesis String
medium
#153Candy
hard