Cleaning Shifts
InterviewTime limit1sMemory limit128 MB
Cover shifts 1 through T with the fewest intervals, where each interval covers a contiguous range of shifts. Return the minimum number of intervals or -1.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Intervals, Two pointers
- Solved
- No attempts yet
Problem
Farmer John is assigning some of his cows () to cleaning chores around the barn. He always wants at least one cow on cleaning duty, and has divided the day into shifts (), numbered from shift up to shift .
Each cow is only available for cleaning during a single interval of the day. Any cow selected for cleaning duty works for the entirety of her interval.
Help Farmer John assign cows to shifts so that (i) every shift is covered by at least one assigned cow, and (ii) the number of cows involved is as small as possible. If it is impossible to cover every shift, print .
Input
- Line : Two space-separated integers and .
- Lines : Each line contains the start and end times of the interval during which a cow can work. A cow works from the shift at its start time through the shift at its end time, inclusive.
Output
- Line : The minimum number of cows Farmer John needs, or if it is impossible to cover every shift.