I want to hire a babysitter to watch over my baby 24 hours a day. I've gotten $n$ responses. Assume that all 24 hours can be covered. A babysitter is willing to work any part or parts of their availability. Each babysitter is asking for a different pay of $p$ dollars per hour spent watching over the baby. What is an efficient algorithm in $O(n)$ where I can find the times of the day where a new babysitter comes in, paying the cheapest amount to hire all babysitters?
I've tried to sort the babysitters by starting time but the runtime would go to $O(n\log n)$, was wondering if there was a better way to run this in linear time.
For each hour, loop over all $n$ babysitters to find the cheapest available for that hour. $O(24n)=O(n)$.
Alternatively, take babysitters as the outer loop, and record the available times for which the current babysitter under consideration is cheaper than the incumbent.