Candy and assign cookies
Many greedy problems are solved by sorting and then matching or sweeping: smallest cookie that satisfies the least greedy child, change kept for later customers, the biggest values given the biggest weights. The skill is spotting the order that makes a local choice safe, and checking it with a swap argument or a small brute force.
How to recognise it
- A short array, and sorting it seems to make the choice obvious.
- Pairing items from two lists.
- Ratings, neighbours or prefix totals with a simple local rule.
How to find the greedy order
- Try sorting by the obvious key (size, value, end time, ratio).
- Look for a swap argument: if two neighbouring items are "out of order", does swapping them never hurt?
- Check the rule against a brute force on small inputs. This guide's tests do exactly that.
Patterns in this section
| Idea | Problems |
|---|---|
| sort both lists and match | assign cookies, pairs of two arrays |
| biggest values get the biggest weights | sum of arr[i] × i, maximum product subset |
| interleave small and large | maximum circular sum of differences |
| keep the most flexible resource | lemonade change |
| two passes | candy |
| running total | wine trading, three stacks, picking up chicks |
| take the biggest piece first | coins, smallest subset with a larger sum, k-centres |
Proving it
A greedy is only right when the choice it makes now can always be part of some best answer. The swap argument shows this: take any best answer, change it to agree with the greedy choice, and show it is still at least as good.
Common mistakes
- Trusting a greedy without a small brute-force check.
- Missing edge cases such as zeros, negatives or a single item.
Problems
- Assign Cookieseasyguide ready
- Lemonade Changeeasyguide ready
- Water Connection Problemmediumguide ready
- Greedy Algorithm to find Minimum number of Coinsmediumguide ready
- Find the minimum and maximum amount to buy all N candiesmediumguide ready
- Minimize Cash Flow among a given set of friends who have borrowed money from each othermediumguide ready
- Check if it is possible to survive on Islandmediumguide ready
- Maximum product subset of an arraymediumguide ready
- Maximize array sum after K negationsmediumguide ready
- Maximize the sum of arr*imediumguide ready
- Maximum sum of absolute difference of an arraymediumguide ready
- Maximize sum of consecutive differences in a circular arraymediumguide ready
- Minimum sum of absolute difference of pairs of two arraysmediumguide ready
- Smallest subset with sum greater than all other elementsmediumguide ready
- DEFKIN -Defense of a Kingdommediumguide ready
- DIEHARD -DIE HARDmediumguide ready
- GERGOVIA -Wine trading in Gergoviamediumguide ready
- Picking Up Chicksmediumguide ready
- CHOCOLA –Chocolatemediumguide ready
- ARRANGE -Arranging Amplifiersmediumguide ready
- K Centers Problemmediumguide ready
- Find smallest number with given number of digits and sum of digitsmediumguide ready
- Find maximum sum possible equal sum of three stacksmediumguide ready
- Candyhardguide ready