Greedy Choice
What is this?
A greedy choice means picking what looks best right this second and never taking it back, the way you hand over the biggest coins first when making change. The catch is that grabbing the best-looking option does not always lead to the best overall result, so the real work here is convincing yourself that the local pick is safe. Once you trust the order to pick things in, the actual solving is a quick single pass.
๐ก Fun fact: Sometimes only a handful of values can possibly be the answer, so trying every candidate is still fast. For the domino puzzle here, the winning value must appear on the very first domino, which leaves just two options to check out of all the possibilities.
๐ The 2 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: a greedy algorithm builds an answer by repeatedly taking the choice that looks best right now and never reconsidering it. The whole challenge is justifying that the local best is also globally safe โ once you trust the ordering, the solution is a short, fast scan.
The pattern
Each problem here picks a clear notion of "best next move" and commits to it. The supporting argument is an exchange or case analysis showing the greedy choice loses nothing.
- Sort by value density, then take. When you have a capacity to fill with items of differing worth, rank items by worth per unit and take from the top until full. Any swap toward a lower-density item can only reduce the total, so the greedy order is optimal.
- Try the few candidate targets. When the goal is to make everything equal and only a handful of values could possibly work, enumerate those candidates explicitly, test each for feasibility, and take the cheapest. The candidate set is tiny, so trying all of them is cheap and exhaustive.
The common thread: define the local criterion, argue it is safe, and let a single pass (after an optional sort) finish the job.
The problems
Maximum Units on a Truck โ each box type carries a number of units; the truck holds a fixed number of boxes. Sort box types by units per box descending, then load greedily from the densest type until the truck is full. Trading any loaded box for a lower-unit one only loses units, so the sort order is optimal.
Minimum Domino Rotations for Equal Row โ to make one row of a domino pair all identical, the common value must be either the top or the bottom of the first domino โ only two candidates. For each candidate, check that every domino can show it on the chosen row, count the rotations needed for top and bottom, and keep the minimum; if neither candidate is feasible, it is impossible.
Key takeaways
- Greedy builds an answer one locally optimal choice at a time and never backtracks.
- The real work is the justification โ an exchange argument or case check that the local best is globally safe.
- Sorting by value density and taking from the top maximizes worth under a capacity limit.
- When only a few values could be the answer, enumerate and test those candidates directly.
- Once the ordering is trusted, greedy solutions are short, fast, and usually one pass after a sort.
Minimum Domino Rotations For Equal Row
Core idea: You have a row of dominoes split into a
tops[]half and abottoms[]half; rotating dominoiswapstops[i]withbottoms[i]. You want one entire half โ all oftopsor all ofbottomsโ to show a single repeated value, using the fewest rotations. The trap is imagining you must search every value1..6. You don't. Whatever the winning valuevis, it has to work for domino 0 too, sovis forced to be eithertops[0]orbottoms[0]โ at most two candidates. For each candidate, sweep the row once: if some domino containsvon neither face, that candidate is impossible; otherwise count how many tops lackv(rotations to fill the top) and how many bottoms lackv(rotations to fill the bottom), and take the smaller. The overall answer is the best over the โค2 candidates, or-1if both fail. O(n) time, O(1) space.
The problem, rephrased
You're laying out a line of dominoes on a table. Each domino has been printed with a number on its top half and a number on its bottom half (every number is 1โ6). You're allowed to grab any domino and flip it end-over-end, which swaps its top and bottom numbers. Your goal: get an entire row showing the same number โ either every top reads the same, or every bottom reads the same. Each flip costs one move. Return the minimum number of flips, or -1 if no amount of flipping can make a uniform row.
This is LeetCode 1007 โ Minimum Domino Rotations For Equal Row.
| Input | Means | Output |
|---|---|---|
tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2] |
6 dominoes, two faces each | 2 |
tops = [3,5,1,2,3], bottoms = [3,6,3,3,4] |
5 dominoes | -1 |
tops = [1,2,1,1,1,2,2,2], bottoms = [2,1,2,2,2,2,2,2] |
8 dominoes | 1 |
You return a single integer: the fewest rotations, or -1 when it's impossible.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson