InterviewSam
Tech TracksStudySystem DesignAptitudeDaily Quiz
Log inStart free
Tech TracksStudySystem DesignAptitudeDaily Quiz
All topics

Greedy

Make the locally best choice, and know how to prove it is safe.

5 pages, free to read

Start studying
  1. 1

    When greedy works (exchange argument)

    Intermediate

    A greedy algorithm makes the locally best choice at every step and never looks back; it is correct only when you can prove that some optimal answer agrees with that choice.

    10 min read
  2. 2

    Activity selection

    Intermediate

    To fit the most non-overlapping activities, sort by end time and keep every activity that starts after the last one you kept ends.

    9 min read
  3. 3

    Reachability (jump game)

    Intermediate

    Track the farthest index you can reach so far; if you ever stand beyond it you are stuck, and counting how often you must extend that frontier gives the minimum number of jumps.

    9 min read
  4. 4

    Circular routes (gas station)

    Intermediate

    If total gas covers total cost a valid start exists, and it is the station right after the last point where the running tank went negative.

    9 min read
  5. 5

    Cheapest merging (Huffman-style)

    Advanced

    When combining two items costs their total size and you must combine everything, always merge the two smallest first, using a min-heap.

    10 min read