There are n interval segments on the one-dimensional axis, and the two interval segments with the longest coincidence interval are obtained.

Source: Internet
Author: User
Tags ranges

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.