Skip to content

08-greedy

  • Contains: best-time-to-buy-and-sell-stock.js, jump-game.js, example-1.py, example-2.py, example-3.py.
  • Greedy strategy examples in JS and Python.
  • Greedy solutions rely on proving a local choice leads to a global optimum.
  • Track running minima/maxima to avoid extra passes.
  • JS files are solution functions (run with Node + a driver or an online judge).
  • Python examples are runnable with python3 <file>.

Greedy works when the locally optimal choice is globally optimal. The proof technique is usually an exchange argument: show that swapping any non-greedy choice for the greedy choice doesn’t worsen the solution. If you can’t convince yourself of correctness in 2 minutes, it’s probably DP.

When to use: Interval scheduling, task assignment, meeting rooms — sort by some criterion, then process greedily.

Template (Interval Scheduling):

intervals.sort(key=lambda x: x[1]) # sort by END time
count, end = 0, float('-inf')
for start, finish in intervals:
if start >= end:
count += 1
end = finish

Interview problems: Non-Overlapping Intervals, Meeting Rooms II, Merge Intervals, Minimum Number of Arrows

When to use: Need to repeatedly pick the best available option.

Template:

import heapq
heap = initial_items
while heap and condition:
best = heapq.heappop(heap)
process(best)
if has_more:
heapq.heappush(heap, next_item)

Interview problems: Task Scheduler, Reorganize String, K Closest Points, Meeting Rooms II

When to use: Assign largest/smallest items to specific positions.

Template:

arr.sort()
left, right = 0, len(arr) - 1
while left < right:
pair(arr[left], arr[right])
left += 1
right -= 1

Interview problems: Boats to Save People, Two City Scheduling, Assign Cookies

Pattern 4: Local Decision (No Sort Needed)

Section titled “Pattern 4: Local Decision (No Sort Needed)”

When to use: The answer builds up by making the obvious choice at each step.

Interview problems and their greedy decisions:

  • Jump Game: Track farthest reachable index. farthest = max(farthest, i + nums[i])
  • Best Time to Buy/Sell Stock: Track minimum price seen so far. profit = max(profit, price - min_price)
  • Gas Station: If total gas >= total cost, a solution exists. Start from where running sum is lowest.
  • Container With Most Water: Move the shorter pointer inward.

Pattern 5: Huffman Coding (Optimal Prefix Codes)

Section titled “Pattern 5: Huffman Coding (Optimal Prefix Codes)”

When to use: Build optimal binary tree with minimum weighted path length.

Template:

import heapq
heap = [(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)))

Interview problems: Huffman Coding, Minimum Cost to Merge Stones

SignalGreedyDP
Optimal substructureYesYes
Greedy choice propertyYesNo — choices interact
“Maximum items” or “minimum cost” with sortingLikely greedy—
“Count all ways” or “min operations”—Likely DP
Can you construct counterexample to greedy?If no → greedyIf yes → DP
CompanyFavorite VariantDifficulty
GoogleInterval + heap combosMedium-Hard
AmazonMeeting rooms, task schedulingMedium
MetaStock problems, jump gameMedium
NetflixScheduling/resource allocationMedium
StripeTransaction ordering problemsMedium