IntermediateAbout 20 min

    Greedy Algorithms

    Read the full article on Substack

    6 min read.

    Solve scheduling and optimization problems by always making the locally optimal choice. Learn the Activity Selection Problem — the canonical greedy success — and the exchange argument that proves it optimal.

    Think of it this way: You manage a single conference room. A naive scheduler picks meetings by importance or length. A greedy scheduler always picks whichever meeting ends soonest — leaving the room free for as many future bookings as possible. Counterintuitively, this "earliest finisher first" rule — with zero lookahead — is provably optimal via an exchange argument: swapping any other first pick for the earliest-ending one never reduces the total count.

    0123456789101112131415161–413–520–635–743–955–966–1078–1188–1292–141012–1611

    Sort by end time: [1,4] [3,5] [0,6] [5,7] [3,9] [5,9] [6,10] [8,11] [8,12] [2,14] [12,16].

    1/13

    Check your understanding

    Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.

    Does greedy work here?

    Sort

    Sort each problem by whether the obvious greedy choice is optimal.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    Greedy is optimal

    Drop items here

    Greedy can fail

    Drop items here
    6 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    What is the time complexity of the greedy Activity Selection algorithm?

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going