Beauty of programming-2.19-interval coincidence judgment

Source: Internet
Author: User

1. Brief Introduction

Given a source interval [x, y] (Y> = x) and N unordered target intervals [X1, Y1] [X2, y2] [X3, Y3]... [XN] [YN]: determines whether [x, y] is within the target range.

2. Ideas

This is relatively simple. Merge the target interval and determine whether the source interval is within the target interval. The specific process is as follows:
Step 1: first sort the target intervals.
Step 2: starting from the first interval, traverse first to find a [XI, Yi] So that xi <= x. If it cannot be found, it means it is not in the target interval, if Yi> = y is found, the work is finished. The source interval is within the target interval. If Yi <Y is found, traverse the source interval to step 3.
Step 3, continue traversing [XI, Yi], if xi> Y (I-1), then the interval is broken, the source interval is not within the target interval, otherwise if Yi> = Y, found, the source range is within the target range; otherwise, the search continues.
Step 4: If the traversal ends, but the source interval is not found to be within the target interval, it means that the source interval is not within the target interval.

Step 1: N * logn, step 2 + step 3, step N, step 4, and step 1. Complexity: O (n logn)

3. Reference

Programming beauty, 2.19, 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.