Paranoid Cows
InterviewTime limit1sMemory limit1024 MB
Given N time intervals, find the largest K such that the first K intervals contain no pair where one strictly contains another.
- Level
Medium5 of 10
- Topics
- Sorting, Binary search
- Solved
- No attempts yet
Paranoid Cows
Farmer John has cows () who are self-conscious about their milk output. A cow who does not produce much milk is likely to be teased by the other cows.
FJ has set a milking schedule in which each cow () is assigned a time interval (). Cow enters the barn at time to be milked and leaves at time . The barn door is narrow and fits only one cow at a time, so FJ made sure that no two cows enter or leave at the same time.
If the interval of cow contains the interval of cow , that is, , the two intervals nest. Nesting is bad. Cow stays in the barn for the whole of cow 's milking and can spy on cow to learn her milk output. Cows do not want others to know their output, so FJ wants the schedule to contain no nested intervals.
Help FJ find the largest index () such that the intervals of cows through contain no nesting.