Interview question selection (69): Maximum overlap interval size

Source: Internet
Author: User

Description: Please writeProgramFind the maximum overlap interval in the input data file described in "input data and format" below.

For a positive integer N, if n is between two positive integers (assume a and B) in a row in the data file, that is, if a <= n <= B or a> = n> = B, n belongs to the row. If n belongs to both I and J, there is an overlap between I and j; the size of the overlap interval is the number of integers belonging to both row I and J.
For example, the overlapping ranges of rows (10 20) and (12 25) are [12 20], the size is 9, rows (20 10) and (20 30) the size of the overlap interval is 1.

 

 

 

 

Ideas:

Use N * log (n) First, sort the order by the Left endpoint of the interval (the smaller number), and then traverse it with O (n!

The traversal process only records the maximum value of the right endpoint (the larger number), the overlap interval = the maximum value-the current left endpoint value, and the maximum overlap interval generated is recorded!

You can also use the line segment tree, but it is the same!

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.