Greedy

Greedy

Locally optimal choices that provably yield a global optimum.

Activity Selection

Pick the maximum number of mutually compatible activities by repeatedly taking the one that finishes earliest.

O(n log n) · O(1) space
Interval Scheduling

The family of interval problems: unweighted selection (greedy by finish), interval partitioning into minimum rooms (greedy by start with a min-heap), and weighted selection (DP with binary search).

O(n log n) · O(n) space
Fractional Knapsack

Maximize value in a capacity-limited knapsack when items can be taken in fractions: take items in decreasing value-per-weight order.

O(n log n) · O(1) space
Huffman Coding

Build an optimal prefix-free binary code by repeatedly merging the two least frequent symbols with a min-heap.

O(n log n) · O(n) space
Job Sequencing with Deadlines

Schedule unit-length jobs with deadlines and profits to maximize total profit: take jobs in profit order and place each in the latest free slot before its deadline.

O(n log n + n · α(n)) · O(n + maxD) space
Gas Station (Circular Tour)

Find the unique start on a circular route from which a car can complete the loop, in one pass: whenever the running tank goes negative, restart from the next station.

O(n) · O(1) space
Merge Intervals

Sort intervals by start and sweep once, extending the current interval while the next one overlaps and emitting it when a gap appears.

O(n log n) · O(n) space
Greedy Algorithms

Build a solution by repeatedly taking the locally best choice — correct only when an exchange argument proves that choice never hurts.

O(n log n) · O(1) space