Interval coincidence judgment

Source: Internet
Author: User

Given a source interval [x, y] (Y> = x) and N unordered target intervals [X1, Y1] [X2, y2] [X3, Y3]… [XN, yn]: determines if the source interval [x, y] is within the target interval? 

For example, if the source interval [] and a group of unordered target intervals [] [], it can be considered that the range [] is within the range [2, 3] [] [], because the target range is actually []. 

Analysis and Solution

Solution 1

The essence of the problem lies in the processing of the target interval. A more direct idea is that the source interval [x, y] (Y> = x) and N unordered target intervals [X1, Y1] [X2, y2] [X3, y3]… [XN, yn] projects one by one on the coordinate axis, only the unoverwritten part of the source interval is investigated. If all the target intervals are completely projected and the source intervals are not covered, the source intervals are not within the target intervals.

Take [] and [] [] [] [] as examples to check whether [] is within [2, 3:

If the source range is [], {[]} is not covered at first, and the target range [2, 3] [1, 2] [3, 9] is investigated in sequence.

Project the target range [2, 3] To the coordinate axis. Then, the unoverwritten part {[]} is changed to {[1, 2], [3, 6]}.

Project the target range [1, 2] to the coordinate axis. The unoverwritten part {[1, 2], [3, 6]} will be changed to {[3, 6]}.

Project the target range [3, 9] To the coordinate axis. The unoverwritten part {[3, 6]} will be changed to {o }, [] is in [2, 3] [1, 2] [3, 9.

From the above steps, we can see that the size of the unoverwritten range array increases by 1 (which may be reduced) at most each operation, and a new target interval is projected for each projection, calculate the time complexity of O (logn) required for overwriting the source interval array, but the time complexity of O (n) is required for updating the unoverwritten range array, therefore, the total time complexity is O (n ^ 2 ).

This solution is highly complex. If you want to query the source ranges of K groups, the time complexity will increase by K times of a single query. Is there a better solution to reduce the time complexity of a single query and reduce the time complexity of K queries to 1/K times the time complexity of a single query?

Solution 2

One idea that is worth trying and has been used many times in this book is to pre-process existing arrays (such as merging and sorting ), combine unordered target intervals into several ordered intervals to compare the intervals.

Therefore, the problem is how to convert these unordered arrays into a target range.

First, you can initialize the data. Because the target range array is unordered, You can merge it to make it orderly. That is to say, first sort the array of the target range from X axis coordinates to large (you can use quick sorting for sorting ), for example, [2, 3] [1, 2] [3, 9]-> [1, 2] [2, 3] [3, 9]; then scan the sorted target range array, merge These intervals into several different intervals, for example, [1, 2] [2, 3] [3, 9]-> [1, 9].

Then, on the basis of data initialization, use binary search (Why ?) To determine whether the source interval [x, y] is included in the merged complementary intersection intervals. If [] is included in [], it indicates that [] is within [2, 3.

This approach is relatively simple, and the time complexity is calculated as follows:

Time complexity of sorting: O (N * logn) (N is the number of destination intervals );

Merging time complexity: O (N );

Time complexity of a single query: logn;

Therefore, the total time complexity is O (n * logn) + O (n) + O (K * logn) = O (N * logn) + O (K * logn ), k indicates the number of queries. Only one initialization data operation is required to merge the target range array.

This not only reduces the time complexity of a single query, but also makes it much easier to process K> N.

Summary

Solution 1 uses the target interval to separate the source interval, which increases the storage space. solution 2 uses the merge method, which is simple and saves space.

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.