Coat Drive Handout
A winter coat drive has a pile of donated coats, each tagged with a warmth rating. Each person in line tells you the lowest rating that will keep them warm, and is happy with any coat rated at least that.
Each coat goes to one person, and each person takes at most one coat. Get as many people as possible walking away warm.
Your task
Write maxWarmPeople(needs, coats). needs[i] is the lowest warmth person i accepts. coats[j] is the warmth of coat j. Return the largest number of people who can each get a coat with coats[j] ≥ needs[i].
Either list may be empty; then the answer is 0. Neither list is sorted.
maxWarmPeople(needs: int[], coats: int[]) → int
Examples
needs = [3, 6, 4] coats = [2, 5, 4]
2
The coat rated 2 is too thin for everyone. The 4 can go to the person needing 3, and the 5 can go to the person needing 4. No coat is warm enough for the person needing 6, so 2 people get coats.
needs = [2, 7] coats = [8, 2]
2
If you hand out the first coat that fits, the person needing 2 takes the 8 and the person needing 7 gets nothing. If you give the 2 to the first person and the 8 to the second, both are warm.
Constraints
- 0 ≤ needs.length ≤ 10,000
- 0 ≤ coats.length ≤ 10,000
- 1 ≤ needs[i], coats[j] ≤ 1,000
Start with brute force
Try every way of handing out coats: for each person, each unused coat that fits, or none, and keep the best total. The number of matchings explodes: even 20 people and 20 coats is far beyond any computer.
A gentler first idea gives each person in line the first coat in the pile that fits. It is fast but wrong: in the second example a needy person loses out because a warm coat went to someone who needed very little.
Hints
0 of 4 shown- Hint 1
In the second example, the person who needs 2 could take either coat. Which should they take so you lose as little as possible?
- Hint 2
A coat warmer than needed wastes warmth. Serve each person with the thinnest coat that works, and save warm coats for people who need them.
- Hint 3
Sort both lists. If the thinnest coat is not warm enough for the least needy person, can it be warm enough for anyone?
- Hint 4
Sort needs and coats. Walk the coats from thinnest to warmest with a pointer into the sorted needs. If the coat covers the current least needy person, they are served and the pointer moves on; otherwise skip the coat. The pointer's final position is the answer.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The greedy rule: starting from the thinnest coat, give each coat to the least needy person it can serve. A thin coat has the fewest possible owners, so spend it on the easiest person to please and keep warm coats free.
Trace the first example. Sorted needs: [3, 4, 6]. Sorted coats: [2, 4, 5].
- Coat 2: the least needy person wants 3, and 2 is less than 3, so this coat fits nobody and is skipped.
- Coat 4: the least needy person still waiting wants 3, and 4 is at least 3, so that person takes it. One person is served.
- Coat 5: the next person wants 4, and 5 is at least 4, so that person takes it. Two people are served.
- There are no coats left, so the person needing 6 goes without. The answer is 2.
Skipping a coat is safe: if it cannot cover the least needy person left, it cannot cover anyone, since everyone else needs at least as much.
function maxWarmPeople(needs, coats) {
const people = [...needs].sort((a, b) => a - b);
const pile = [...coats].sort((a, b) => a - b);
let served = 0; // index of the least needy person still waiting
for (const coat of pile) {
if (served < people.length && coat >= people[served]) {
served++; // thinnest coat that works goes to this person
}
// otherwise this coat is too thin for everyone left: skip it
}
return served;
}def max_warm_people(needs, coats):
people = sorted(needs)
pile = sorted(coats)
served = 0 # index of the least needy person still waiting
for coat in pile:
if served < len(people) and coat >= people[served]:
served += 1 # thinnest coat that works goes to this person
# otherwise this coat is too thin for everyone left: skip it
return servedRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.