Zoo feeding round
MediumOnline Assessment
Plan a zoo's feeding round: decide which keepers handle each scheduled feeding, and which feedings have to be retried.
keepers[i] = [shiftStart, shiftEnd, species] is keeper i (numbered from 0): the minutes at which the shift starts and ends, and the list of species the keeper is certified to feed. feedings lists the scheduled feedings as [time, duration, species], in non-decreasing order of time. dangerous lists the species whose feedings need two keepers at once; every other feeding needs one.
A keeper can take a feeding that runs from t to t + duration only if all of these hold:
shiftStart <= tandt + duration <= shiftEnd;- the keeper is certified for the species;
- the keeper is free: their previous feeding ended at least
turnaroundminutes beforet(a keeper with no previous feeding is free).
When a feeding is handled, collect every keeper who can take it and prefer those who have fed fewest animals so far (each feeding a keeper has taken counts as 1), breaking ties by the lower keeper number. Take as many as the feeding needs. If fewer keepers can take it than it needs, nobody is assigned and the feeding is missed. A missed feeding is retried exactly once, 15 minutes later with the same duration and species, under the same rules. If the retry is missed too it is dropped.
Feedings are handled in order of time. At the same minute, original feedings come first (in their listed order), then retries (in the order of the feedings they retry). A retry is handled at its own time and sees everything assigned before it.
Input: keepers, feedings, dangerous, turnaround.
Output: a list with one entry per feeding, in the listed order: [start, assigned], where start is the minute the feeding actually happened (its time, or its time plus 15 if it was a retry) and assigned is the list of keeper numbers in increasing order. A dropped feeding is [-1, []].
Example 1
- Input:
- [[[0,300,["otter","penguin"]],[0,300,["penguin","otter"]]],[[0,10,"otter"],[15,10,"penguin"]],[],5]
- Output:
- [[0,[0]],[15,[1]]]
- Explanation
- With
turnaround = 5, keeper 0 feeds the otters from 0 to 10. At 15 both keepers are free (keeper 0 finished 5 minutes ago, exactly the turnaround), but keeper 0 has already fed 1 animal and keeper 1 none, so keeper 1 takes the penguins. The result is[[0, [0]], [15, [1]]].
Example 2
- Input:
- [[[0,200,["lion","otter"]],[0,200,["otter"]],[0,35,["lion"]]],[[0,20,"lion"],[22,10,"otter"],[30,10,"lion"]],["lion"],5]
- Output:
- [[0,[0,2]],[22,[1]],[-1,[]]]
- Explanation
- Lions are dangerous and need two keepers. At 0 keepers 0 and 2 are both certified and free, so they take it together. At 22 keeper 0 is not free yet (their feeding ended at 20 and the turnaround is 5), so the otters go to keeper 1. At 30 the lion feeding lasts until 40: keeper 0 is free, but keeper 2's shift ends at 35, so only one keeper can take a two-keeper feeding and nobody is assigned. The retry at 45 fails too (keeper 2 is off shift), so the feeding is dropped:
[-1, []].
Example 3
- Input:
- [[[0,200,["otter"]],[0,200,["otter"]]],[[0,50,"otter"],[0,50,"otter"],[40,10,"otter"],[55,10,"otter"]],[],5]
- Output:
- [[0,[0]],[0,[1]],[55,[1]],[55,[0]]]
- Explanation
- Keepers 0 and 1 take the two long feedings at 0 (ties go to the lower number). The feeding at 40 finds both busy and is missed, so it is retried at 55. At minute 55 the original feeding listed there comes first: both keepers have just become free and have 1 feeding each, so keeper 0 takes it. Then the retry sees only keeper 1 free and gets
[55, [1]].
Constraints
1 <= keepers.length <= 8,0 <= feedings.length <= 2000 <= shiftStart < shiftEnd <= 1,440;0 <= time <= 1,440,1 <= duration <= 120,0 <= turnaround <= 60- species are lowercase words; a dangerous feeding needs exactly two keepers
- feedings are given in non-decreasing order of
time