Sleep Pattern
InterviewTime limit1sMemory limit1024 MB
Given weekday sleep intervals on a Mon-Fri timeline, compute the minimum weekend hours needed so total weekly sleep reaches T, or report that even 48 hours is not enough.
- Level
Medium4 of 10
- Topics
- Implementation, Math, Sorting, Simulation
- Solved
- No attempts yet
Statement
Minty, who got used to staying home because of social distancing and a leave of absence, is worried that their sleep pattern keeps breaking down.
For their health, Minty has decided to sleep at least hours a week from now on. Sleeping and waking at the same time every day is still too hard, so Minty writes down the times they slept on weekdays and plans to catch up on the weekend. Once Minty falls asleep, they sleep at least 1 hour, and sometimes they are so sleepy that they fall right back asleep the moment they wake up.
Now the weekdays are over, and Minty has a 48-hour weekend to sleep as much as they want. Minty looks at the records written on the weekdays and tries to compute how many hours to sleep over the weekend.
Given the times Minty fell asleep and woke up during the weekdays, output the minimum number of hours Minty has to sleep on the weekend.
Input
The first line gives the integer , the number of hours to sleep per week, and the integer , the number of times Minty slept during the weekdays.
The next lines give the times Minty slept. Each line has , , , in that order: the time Minty fell asleep and the time Minty woke up. is the day of falling asleep, is the hour of falling asleep, is the day of waking up, and is the hour of waking up. A day is one of Mon, Tue, Wed, Thu, Fri, meaning Monday, Tuesday, Wednesday, Thursday, Friday. An hour is an integer from 0 to 23, meaning the hours from 0 o'clock to 23 o'clock.
The sleeps do not overlap each other, but since Minty can fall right back asleep after waking, the falling-asleep time of one sleep can equal the waking time of another. The start and end of every sleep are always on weekdays, and the input is given sorted by falling-asleep time in increasing order. Note that Minty may not sleep at all during the weekdays.
Output
Output the number of additional hours Minty has to sleep on the weekend. If sleeping no hours on the weekend is enough, output 0; if sleeping the entire weekend is not enough, output -1.