[Algorithm] interval coincidence judgment

Source: Internet
Author: User

Description:

Given a source interval [x, y] and N unordered target intervals [X1, Y1], [X2, y2],... [XN, Y,]: determines that the given source range [x, y] is not in the target range.

For example, if the source interval [1 6] and the target interval [1 2] [2 4] [4 9] are given, the interval [1 6] is considered to be within the target interval, because the union of the source interval is [1 9].

Imagine that in such a set of target intervals, You Need To frequently query whether an interval is in the set. How can we reduce the complexity of a single query?

Complexity. Preprocessing. The pre-processing of intervals can meet such requirements.

Direct Method:

Step 1: First merge the intervals (that is, make the range merge a set of non-Intersecting intervals)

Step 2: Search for the source interval in the interval after processing. Of course, binary search can have a good effect, so you can sort the interval.


So: 1 sorts the intervals by their starting points.

2 merge intersection Interval

You can search for the source interval in the third-byte mode.


The Code is as follows:





[Algorithm] interval coincidence judgment

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.