# 伪代码：

class Interval{
int start; //起点
int end;  //止点
Interval (int a,int b){
start =a;
end = b;
}
}

class Point implements Comparable<Point>{
int value;//数值
int type;//点的类型，0为起点，1为止点
Point (int v,int t){
value = v;
type = t;
}
//还需要实现compareTo函数，以便排序
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;
}

# R语言

> a=matrix(c(1:16),ncol = 2, byrow = TRUE)
> a
[,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[4,]    7    8
[5,]    9   10
[6,]   11   12
[7,]   13   14
[8,]   15   16
> to <- Intervals(a,closed = c( TRUE, FALSE ),type = "R")
> #集合to
> to
Object of class Intervals
8 intervals over R:
[1, 2)
[3, 4)
[5, 6)
[7, 8)
[9, 10)
[11, 12)
[13, 14)
[15, 16)
> dim(to)
[1] 8 2
> b <- matrix(c(2.121, 8,8, 9,6, 9,11, 12,3, 3),ncol = 2, byrow = TRUE)
> b
[,1] [,2]
[1,]  2.121    8
[2,]  8.000    9
[3,]  6.000    9
[4,] 11.000   12
[5,]  3.000    3
> from <- Intervals(b,closed = c( FALSE, FALSE ),type = "R")
> # 集合from
> from
Object of class Intervals
5 intervals over R:
(2.121, 8)
(8, 9)
(6, 9)
(11, 12)
(3, 3)
> rownames(from) <- c(1:nrow(from))
> empty(to)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> empty(from)
[1] FALSE FALSE FALSE FALSE  TRUE
> b1 <- interval_overlap(from, to)
Warning message:
Some empty 'from' intervals encountered. Setting to NA...
> b1
$1 [1] 2 3 4$2
integer(0)

$3 [1] 4$4
[1] 6

\$5
integer(0)

> sum(lengths(b1))
[1] 5

#### 例子1

> b <- matrix(c(2, 8,8, 9,6, 9,11, 12,3, 3),ncol = 2, byrow = TRUE)
> b
[,1] [,2]
[1,]    2    8
[2,]    8    9
[3,]    6    9
[4,]   11   12
[5,]    3    3
> from <- Intervals(b,closed = c( T, T ),type = "R")
> from
Object of class Intervals
5 intervals over R:
[2, 8]
[8, 9]
[6, 9]
[11, 12]
[3, 3]
> b1 <- interval_overlap(from, from)
> b1
[[1]]
[1] 1 2 3 5

[[2]]
[1] 1 2 3

[[3]]
[1] 1 2 3

[[4]]
[1] 4

[[5]]
[1] 1 5

> (sum(lengths(b1))-nrow(b))/2
[1] 4

#### 例子2

> b <- matrix(c(1, 5,10, 15,5, 10,20, 30),ncol = 2, byrow = TRUE)
> b
[,1] [,2]
[1,]    1    5
[2,]   10   15
[3,]    5   10
[4,]   20   30
> from <- Intervals(b,closed = c( T, T ),type = "R")
> from
Object of class Intervals
4 intervals over R:
[1, 5]
[10, 15]
[5, 10]
[20, 30]
> b1 <- interval_overlap(from, from)
> b1
[[1]]
[1] 1 3

[[2]]
[1] 2 3

[[3]]
[1] 1 2 3

[[4]]
[1] 4

> (sum(lengths(b1))-nrow(b))/2
[1] 2

# python

这里写代码片

176 篇文章40 人订阅

0 条评论