Algorithm exercise: number of overlapping intervals

Source: Internet
Author: User

I. Description of the topic

Given a number of possible overlapping intervals, find out the number of overlapping intervals.

Examples are as follows:

Input:[1,5],[ten],[5, Ten],[ +]

Output:2

Note: Test instructions should be to find the maximum number of intervals in the overlap interval, when there is no interval overlap, the maximum number of overlaps is 1, such as

The input is:[1,5],[ 1 ],then the output is ;

Inputs are:[1,2],[2,3],[3, 4] , [4,5] , the output is 2 ( overlapping intervals must intersect with each other );

Enter as:[1,7],[2,5],[3,4],[8,[],[9,+ ],[,), the output is3.

Second, the problem analysis

This problem method is relatively simple, as long as the interval is separated into points, each point has two attributes, one is a value, one is a flag (0 starting point,1 stop), and then the points sorted, and finally, Scan the result of the sorting from the beginning, encounter the starting point overlap number plus 1, encounter the number of stops overlap minus 1, and record the maximum number of overlapping.

The time complexity of this algorithm is O (NLOGN), because the algorithm time is mainly consumed in the sort.

Third, the implementation of the algorithm

#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include < windows.h>using namespace std;//interval definition class interval{public:interval (int iStart, int iend): M_istart (IStart), M_iend ( Iend) {}int m_istart;int m_iend;}; typedef vector<interval> intervalvec;//Interval Split point definition class pointcomparable{public:pointcomparable (int ival, int IType): M_ival (ival), M_itype (iType) {}//overloaded less than operator, sort using bool operator < (const pointcomparable& pcpoint) {if (thi S->m_ival = = pcpoint.m_ival) {return this->m_itype < Pcpoint.m_itype;} return This->m_ival < Pcpoint.m_ival;} int M_ival;int m_itype;//Point type, 0 is the starting point, 1 is the end point};int getoverlappedintervalmaxcount (const intervalvec& intvvec) {vector <PointComparable> pcvec;for (Intervalvec::const_iterator it = Intvvec.begin (); It! = Intvvec.end (); ++it) { Pcvec.push_back (pointcomparable (it->m_istart, 0));p cvec.push_back (Pointcomparable (it->m_iend, 1));} Sort (Pcvec.begin (), Pcvec.end ()), int IMAxcount = 0;int Icurcount = 0;for (Vector<pointcomparable>::iterator ittemp = Pcvec.begin (); ItTemp! = PcVec.end (); ++ittemp) {cout << ittemp->m_ival << "" << ittemp->m_itype << endl;if (ittemp->m_itype = = 0) {Icurcount++;imaxcount = __max (Icurcount, imaxcount);} else{icurcount--;}} return imaxcount;} int main () {Intervalvec intvvec;//intvvec.push_back (Interval (1,5));//Intvvec.push_back (Interval (5,10));// Intvvec.push_back (Interval (1,7));//Intvvec.push_back (Interval (2,5));//Intvvec.push_back (Interval (3,6));// Intvvec.push_back (Interval (8,15));//Intvvec.push_back (Interval (9,17));//Intvvec.push_back (Interval (20,25)); Intvvec.push_back (Interval); Intvvec.push_back (Interval (2,3)); Intvvec.push_back (Interval (3,4)); Intvvec.push_back (Interval (4,5)) cout << "Maximum overlap Interval number:" << getoverlappedintervalmaxcount (Intvvec)  
<span style= "White-space:pre" ></span>cout << Endl;return 0;}

Series Article Description:
1. This series of articles [algorithmic exercises] is just a record of my learning process and self-motivated, and there is no didactic meaning. It is my pleasure to bring a little knowledge and sentiment to the reader.
2. This series of articles is I learn contacts teacher "into Silicon Valley, programmer interview Secret" a book and write some experience, the majority of the article views from this book, hereby explain!
3. In the article, there are inevitably many errors and shortcomings, welcome readers to criticize, thank you.


Hill son
Reprint Please indicate the source, thank you. Original address:http://blog.csdn.net/s634772208/article/details/46492651


Algorithm exercise: number of overlapping intervals

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.