Greedy tutorial

Take the best move nowWorld 7 tutorial
0:00 / 0:00

Take what looks best right now and never go back. It works only when today's best pick can't hurt you later, so learn to spot when it's safe.

▼

Transcript

Welcome to the Greedy world. The move: at every step, take what looks best right now, and never go back.

Picture a climb. On each ledge, the hunter grabs the biggest gem and jumps up, without looking ahead. The ledge behind crumbles: no second thoughts.

It works when the best pick now can't hurt you later. Fit the most meetings in one room: keep taking the one that ends first. Four meetings, the most possible.

Why trust it? Take any best schedule, and swap its first meeting for the greedy one. It ends earlier, so everything still fits.

Careful, though: greedy can fail. Make six from coins one, three and four. Greedy grabs four, then one and one: three coins. But three plus three is just two. That one needs dynamic programming.

The clue: the problem asks for the minimum number or the maximum, works on intervals, or asks if you can reach the end.

Usually you sort first, or track one value, like the farthest reach. One pass is linear time; with the sort, n log n.

Now let's use it on Can Place Flowers.