Chapter 10: Greedy algorithms
A greedy algorithm makes the choice that looks best right now and never reconsiders it. That works only when the locally optimal choice is globally optimal, and the whole skill of greedy algorithms is knowing when that is true. The usual proof technique is an exchange argument: take any optimal solution that differs from the greedy one, swap one of its choices for the greedy choice, and show the result is no worse. Repeat until the optimal solution is the greedy one. A practical rule: if you cannot convince yourself a greedy choice is correct in two minutes, the problem is probably dynamic programming.
Concepts from other sections (2)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Dynamic programming (DP): solving a problem by breaking it into overlapping subproblems and storing each subproblem’s answer the first time it is computed, which turns an exponential recursion into a polynomial computation. Read more: Chapter 9: Dynamic programming
- Huffman tree: the binary tree Huffman’s algorithm builds, with each character at a leaf; a character’s code is its path from the root, 0 for a left branch and 1 for a right one, so frequent characters sit near the root and get short codes. Read more: 10.1 Huffman coding
This chapter follows Dynamic programming on purpose. Both rely on optimal substructure; what greedy adds is the greedy-choice property, which lets it commit to one choice instead of comparing them all. The chapter covers sorting then choosing, greedy with a heap, pairing from both ends, local decisions that need no sort, and Huffman coding, and ends with a guide for telling greedy problems from DP problems.
Learning objectives
- Prove a greedy choice correct with an exchange argument, or find the counterexample that rules it out.
- Solve interval scheduling by sorting on end time.
- Use a heap to repeatedly take the best available option.
- Solve running-extreme problems (Jump Game, Best Time to Buy and Sell Stock) in one pass.
- Build an optimal prefix code with Huffman’s algorithm.
Before you begin
Section titled “Before you begin”Read Dynamic programming first, so you can recognize when greedy fails and DP is needed. The heap-based patterns use a priority queue; the binary heap for top-k builds one, and Huffman coding builds a binary tree as in Trees.
Sort, then choose
Section titled “Sort, then choose”Interval scheduling, task assignment, and meeting rooms share a shape: sort by the right criterion, then make one pass taking whatever fits. For “the most non-overlapping intervals,” the right criterion is the end time. The interval that ends first leaves the most room for everything after it, and an exchange argument shows that any optimal schedule can be rewritten to start with it:
intervals.sort(key=lambda x: x[1]) # sort by END timecount, end = 0, float('-inf')for start, finish in intervals: if start >= end: count += 1 end = finishSorting by start time or by length both fail on small counterexamples, which is exactly why the criterion matters. Non-overlapping Intervals, Meeting Rooms II, Merge Intervals, and Minimum Number of Arrows to Burst Balloons use this pattern.
Greedy with a heap
Section titled “Greedy with a heap”When you need to repeatedly pick the best available option, and taking an option can make new options available, keep the options in a priority queue:
import heapqheap = initial_itemswhile heap and condition: best = heapq.heappop(heap) process(best) if has_more: heapq.heappush(heap, next_item)Task Scheduler, Reorganize String, K Closest Points to Origin, and Meeting Rooms II (a heap of end times, one per room in use) are the standard problems.
Pairing from both ends
Section titled “Pairing from both ends”To assign the largest and smallest items to specific positions, such as fitting people into two-person boats under a weight limit, sort and pair from the two ends:
arr.sort()left, right = 0, len(arr) - 1while left < right: pair(arr[left], arr[right]) left += 1 right -= 1This is the opposite-end pointer pattern from Two pointers and sliding window, used for a greedy assignment. Boats to Save People, Two City Scheduling, and Assign Cookies are examples.
Local decisions that need no sort
Section titled “Local decisions that need no sort”Some answers build up by making the obvious choice at each step, with no sorting at all. Each of these problems reduces to one line of greedy state:
- Jump Game: track the farthest reachable index,
farthest = max(farthest, i + nums[i]). - Best Time to Buy and Sell Stock: track the minimum price seen so far,
profit = max(profit, price - min_price). - Gas Station: if total gas is at least total cost, a solution exists, and it starts just after the point where the running sum is lowest.
- Container With Most Water: move the shorter pointer inward.
Huffman coding
Section titled “Huffman coding”Huffman’s algorithm builds an optimal prefix code, the binary tree with the minimum weighted path length. The greedy choice is to merge the two least frequent symbols into one subtree, since the least frequent symbols should be deepest:
import heapqheap = [(freq, char) for char, freq in freq_map.items()]heapq.heapify(heap)while len(heap) > 1: lo = heapq.heappop(heap) hi = heapq.heappop(heap) heapq.heappush(heap, (lo[0] + hi[0], Node(lo, hi)))With distinct symbols this takes . Huffman Coding and Minimum Cost to Merge Stones are the classic problems; Huffman coding in this chapter implements the full encoder and decoder.
Greedy or DP?
Section titled “Greedy or DP?”The decision between the two comes down to a few signals, summarized here:
| Signal | Greedy | DP |
|---|---|---|
| optimal substructure | yes | yes |
| greedy-choice property | yes | no: choices interact |
| “maximum items” or “minimum cost” with sorting | likely greedy | |
| “count all ways” or “minimum operations” | likely DP | |
| can you construct a counterexample to the greedy choice? | if no, greedy | if yes, DP |
Coin change is the standard cautionary example: taking the largest coin first works for US coins but fails for coins {1, 3, 4} and amount 6 (greedy gives , the optimum is ), which is why it lives in the DP chapter.
Interviewers favor different variants. As a rough guide:
| Company | Favorite variant | Difficulty |
|---|---|---|
| interval and heap combinations | Medium-Hard | |
| Amazon | meeting rooms, task scheduling | Medium |
| Meta | stock problems, Jump Game | Medium |
| Netflix | scheduling and resource allocation | Medium |
| Stripe | transaction ordering problems | Medium |
Key terms
Section titled “Key terms”- Greedy algorithm: an algorithm that commits at each step to the choice that looks best now and never reconsiders it; it is correct only when the problem has the greedy-choice property.
- Greedy-choice property: the property that some optimal solution begins with the locally best choice, so committing to that choice never rules out an optimum; it is what separates greedy problems from dynamic programming ones.
- Exchange argument: a proof that a greedy algorithm is optimal, made by taking any optimal solution that differs from the greedy one, swapping in the greedy choice, and showing the result is no worse.
- Interval scheduling: selecting the most non-overlapping intervals, solved greedily by sorting on end time and taking each interval that starts no earlier than the last chosen one ends.
- Prefix code: a code in which no codeword is a prefix of another, so a bit stream decodes left to right without separators; any prefix code is a binary tree with the symbols at its leaves.
- Huffman coding: an optimal prefix code built greedily by repeatedly merging the two least frequent symbols or subtrees with a min-heap, so the rarest symbols get the longest codewords; it takes for symbols.
In this chapter
Section titled “In this chapter”The JavaScript files are solution functions; run them with Node and a small driver, or in an online judge. The Python examples run with python3 <file> and take their input as command-line arguments.
- Best Time to Buy and Sell Stock: one pass that moves the buy day whenever a lower price appears and records the best profit from selling at each later price; the answer is if prices only fall.
- Jump Game: a backward greedy written as a tail recursion. It walks from the end toward the start, moving the target position left whenever index
ican reach it (nums[i] + i >= lastPosition), and succeeds if the target reaches index . - Answering the most questions: given a time budget
hand the time each question takes, answers the shortest questions first, keeping every question whose cumulative sorted time fits withinh. Run it aspython3 example-1.py <h> <times...>. - Fewest pit stops: a diver who can cover distance
don one breath travels between pit stops separated by the given distances, and stops only when the next leg would exceedd. Going as far as possible before each stop minimizes the number of stops, by the classic refueling exchange argument. Run it aspython3 example-2.py <d> <distances...>. - Greedy subsequence partition: sweeps the sequence’s values in sorted order, appending each to the current subsequence while it also comes later in the original order, and starting a new subsequence otherwise.
- Huffman coding: a Java program that compresses a text file with a Huffman tree and decodes it again.