Question:
There are n interval segments on the one-dimensional axis, and the two interval segments with the longest coincidence interval are obtained.
Analysis:
This question is similar to the time schedule in the beauty of programming. It can be represented by a queue. First, it is sorted by the Left number of each interval, and then entered at the end of the queue in sequence, if you encounter the right number of intervals, find the corresponding left number from the queue header. If the queue header is not the corresponding left number, it indicates that this interval is completely surrounded by other intervals. On the contrary, if it is the corresponding left number and finds another left number behind it, the difference between them is the maximum coincidence that can be formed by the range. The final calculation is the biggest, which is a bit confusing, check the Code directly.
I saw a description from a netizen on the Internet, which is better than my description. I directly reprinted it:
---------------------------------------------
Each interval contains one record, and the start time and end time are extracted for sorting (of course, the two types of moments must be distinguished)
A. Start a. End B. Start B. End C. Start C. End
Then scan the elements one by one. If it is "start", you can directly join the team. If it is "end", you can check the start items of the team head, if it is not its own, the start time is its own start time (completely included by another range); otherwise, the first start time (followed by the start time, and the end point of this interval is after it). The End Time is the current value, and the obtained range is the maximum range that can be formed by the current interval and other intervals, then, the start time of the record is displayed. When all the start time is displayed, the maximum possible range between all the ranges and other ranges is considered;
---------------------------------------------
The key to implementation is how to effectively delete any element in the queue and use vector.
The Code is as follows: