Skip to Content

Make the locally optimal choice at every step and hope it leads to a global optimum. Cheap but only works when the problem has a greedy-choice property and optimal substructure.

Data Flow (Activity Selection)

intervals: (1,3), (2,4), (3,5), (6,7)

sort by end: (1,3), (3,5), (2,4), (6,7)
greedy: pick shortest-finishing job that starts >= previous end
select (1,3) -> (3,5) -> (6,7)
max non-overlapping: 3

When to use

Pitfalls

Code (jump game — can we reach the end?)

def can_jump(nums):
    furthest = 0
    for i, n in enumerate(nums):
        if i > furthest: return False
        furthest = max(furthest, i + n)
    return True

Analogy

Driving a rental car back to the depot: always fill up at the cheapest station you can reach. It minimises total cost ONLY if prices are non-increasing.

Interview tip: If you cannot prove greedy correct within ~30 seconds, downswitch to DP.

Advertisement