算法练习:重叠区间个数

一、题目描述

给定多个可能重叠的区间,找出重叠区间的个数。

举例如下:

输入:[15][1015][510][2030]

输出:2

 

说明:题意应该是找出重叠区间中区间的最大个数,当没有区间重叠时,重叠个数最大为1,比如

输入为:[15][1015],则输出为1

输入为:[12][23][34][45],则输出为2重叠区间相互之间都要有交集);

输入为:[17][25][34][815][917][2025],则输出为3

 

二、题目分析

此题解题方法比较简单,只要将区间分隔成各个点,每个点有两个属性,一个是值,一个是标志(0起点,1止点),然后对这些点排序,最后,从头开始扫描排序的结果,遇到起点重叠个数加1,遇到止点重叠个数减1,并且记录好重叠个数的最大值。

本算法的时间复杂度为O(nlogn),因为算法时间主要消耗在排序上。

 

三、算法实现

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <algorithm>  
  4. #include <cmath>  
  5. #include <Windows.h>  
  6. using namespace std;  
  7.   
  8.   
  9. //区间定义  
  10. class Interval  
  11. {  
  12. public:  
  13.     Interval( int iStart, int iEnd)  
  14.         :m_iStart( iStart), m_iEnd(iEnd){}  
  15.     int m_iStart;  
  16.     int m_iEnd;  
  17. };  
  18.   
  19. typedef vector<Interval> IntervalVec;  
  20.   
  21.   
  22. //区间拆分的点定义  
  23. class PointComparable  
  24. {  
  25. public:  
  26.     PointComparable( int iVal, int iType )  
  27.         :m_iVal( iVal ), m_iType( iType ){}  
  28.   
  29.     //重载小于操作符,排序使用  
  30.     bool operator < ( const PointComparable& pcPoint )  
  31.     {  
  32.         if ( this->m_iVal == pcPoint.m_iVal )  
  33.         {  
  34.             return this->m_iType < pcPoint.m_iType;  
  35.         }  
  36.         return this->m_iVal < pcPoint.m_iVal;  
  37.     }  
  38.   
  39.     int m_iVal;  
  40.     int m_iType;//点类型,0为起点,1为终点  
  41. };  
  42.   
  43. int GetOverlappedIntervalMaxCount( const IntervalVec& intvVec )  
  44. {  
  45.     vector<PointComparable> pcVec;  
  46.     for ( IntervalVec::const_iterator it = intvVec.begin();  
  47.         it != intvVec.end(); ++it )  
  48.     {  
  49.         pcVec.push_back( PointComparable( it->m_iStart, 0 ) );  
  50.         pcVec.push_back( PointComparable( it->m_iEnd, 1 ) );  
  51.     }  
  52.   
  53.     sort( pcVec.begin(), pcVec.end() );  
  54.   
  55.   
  56.   
  57.     int iMaxCount = 0;  
  58.     int iCurCount = 0;  
  59.     for ( vector<PointComparable>::iterator itTemp = pcVec.begin();  
  60.         itTemp != pcVec.end(); ++itTemp )  
  61.     {  
  62.         cout << itTemp->m_iVal << " " << itTemp->m_iType << endl;  
  63.         if ( itTemp->m_iType == 0 )  
  64.         {  
  65.             iCurCount++;  
  66.             iMaxCount = __max( iCurCount, iMaxCount );  
  67.         }  
  68.         else  
  69.         {  
  70.             iCurCount--;  
  71.         }  
  72.     }  
  73.   
  74.     return iMaxCount;  
  75. }  
  76.   
  77. int main()  
  78. {  
  79.     IntervalVec intvVec;  
  80. //  intvVec.push_back( Interval(1,5) );  
  81. //  intvVec.push_back( Interval(5,10) );  
  82.   
  83. //  intvVec.push_back( Interval(1,7) );  
  84. //  intvVec.push_back( Interval(2,5) );  
  85. //  intvVec.push_back( Interval(3,6) );  
  86. //  intvVec.push_back( Interval(8,15) );  
  87. //  intvVec.push_back( Interval(9,17) );  
  88. //  intvVec.push_back( Interval(20,25) );  
  89.   
  90.     intvVec.push_back( Interval(1,2) );  
  91.     intvVec.push_back( Interval(2,3) );  
  92.     intvVec.push_back( Interval(3,4) );  
  93.     intvVec.push_back( Interval(4,5) );  
  94.   
  95.   
  96.       
  97.   
  98.     cout << "最大重叠区间个数:" << GetOverlappedIntervalMaxCount( intvVec )    
[cpp]  view plain  copy
  1. <span style="white-space:pre">    </span>cout << endl;  
  2.     return 0;  
  3. }  

系列文章说明:
1.本系列文章[算法练习],仅仅是本人学习过程的一个记录以及自我激励,没有什么说教的意思。如果能给读者带来些许知识及感悟,那是我的荣幸。
2.本系列文章是本人学习陈东锋老师《进军硅谷,程序员面试揭秘》一书而写的一些心得体会,文章大多数观点均来自此书,特此说明!
3.文章之中,难免有诸多的错误与不足,欢迎读者批评指正,谢谢.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值