Php 查找连续时间段的最大集合
我有一个给定日期的时间段数组,其中有些重叠,如(开始、结束): 我想找出连续时间段的最大集合。在上面的示例中,有3组连续时间(如下所示),但由于第一组是第二组的较小“备选方案”,因此应忽略它,因此我们只剩下2和3Php 查找连续时间段的最大集合,php,algorithm,Php,Algorithm,我有一个给定日期的时间段数组,其中有些重叠,如(开始、结束): 我想找出连续时间段的最大集合。在上面的示例中,有3组连续时间(如下所示),但由于第一组是第二组的较小“备选方案”,因此应忽略它,因此我们只剩下2和3 11.00-11.30、11.30-11.45、11.45-12.00 11.00-11.30、11.30-11.45、11.45-12.15、12.15-12.45 14.15-14.35、14.35-14.40 我必须补充一点:我希望能够指定一个定义连续含义的“容差”。在上述示例中
bool comp(A,B){
如果(A.start==B.start)返回A.end>B.end;
返回A.startcurRight+TOLERANCE){//start新集合
curLeft=periods[i]。开始,curRight=periods[i]。结束;
}
else{//展开当前集合
curRight=max(curRight,句点[i].end);
}
最长周期=最大值(最长周期,电流-卷曲);
}
回归期最长;
}
这些时间是否总是在同一天按顺序排列,或者是在不同的顺序和跨天排列?是的,它们都在同一天。对于像13.30-13.40和13.30-13.45这样的时间段,我猜“有序”意味着最小的优先?不管怎样,把它们按某种顺序排列都不难:)这真的很有帮助,谢谢。我已经实现了您提供的最后一个链接上给出的算法,但我无法调整它以获得实际路径。它说我必须引入“前置数组”,但我不确定它们的意思是什么?
(10.00, 10.15) (11.00, 11.30) (11.30, 11.45) (11.45, 12.00)
(11.45, 12.15) (12.15, 12.45) (13.20, 13.30) (14.15, 14.35) (14.35, 14.40)
bool comp(A, B) {
if(A.start == B.start) return A.end > B.end;
return A.start < B.start;
}
function getLongestSet( periods ) { //convert the times to integer first, period is pair of integers
sort ( periods, comp);
longestPeriod = 0;
for(i = 0; i < periods.length; i++) {
if(periods[i].start > curRight + TOLERANCE) { //start new set
curLeft = periods[i].start, curRight = periods[i].end;
}
else { //expand current set
curRight = max(curRight, periods[i].end);
}
longestPeriod = max(longestPeriod, curRight - curLeft);
}
return longestPeriod;
}