Problem: Given a plurality of possibly overlapping sections, to identify the maximum number of overlapping sections. Intervals are defined as follows:
public class Interval{
int start; //起点
int end; //止点
Interval(int a,int b){
start=a;
end=b;
}
}
Ideas: The boundary zone is converted into points, to sort the results of a scan to sort all points. When confronted origin, plus a number of overlapping, and recorded the maximum number of overlapping; when encountered dead center, the overlapping number minus one.
java code:
class Point implements Comparable<Point>{
int value;
int type;
Point(int v,int t){
value=v;
type=t;
}
public int compareTo(Point p){
if(this.value==p.value){
return 0;
}
else if(this.value>p.value){
return 1;
}
else{
return -1;
}
}
}
int getOverlappingCount(Interval[] A){
int max=0,count=1;
if(A==null||A.length==0)
return max;
Point[] points=new Point[A.length*2];
for(int i=0;i<A.length;i++){
points[2*i]=new Point(A[i].start,0);
points[2*i+1]=new Point(A[i].end,1);
}
Collections.sort(points);
for(int i=0;i<points.length;i++){
if(points[i].type==0){
count++;
max=Math.max(max,count);
}
else{
count--;
}
}
return max;
}