Python 3D Academyবাংলা

Greedy & Intervals in Python

Chapter 56: Greedy & Intervals

4 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. The greedy choice, and when it fails

    A greedy algorithm builds its answer one step at a time, each time taking the choice that looks best right now and never going back; it is right only when that choice is always safe, and one counterexample is enough to show it isn't.

  2. Picking the most activities

    Sort the meetings by when they end, then book each one that starts once the last booked one has ended; finishing early always leaves the most room for what comes after.

  3. Merging and inserting intervals

    Sort the intervals by start; then each one either overlaps the block you are building, and stretches it, or starts after a gap, and begins a new block.

  4. Jump game and gas station

    The stones you can reach always form one unbroken stretch from the start, so keeping just the farthest reach tells you everything; if you ever stand past it, you are stuck.