Ori*_*ion 5 arrays algorithm perl merge intervals
我有两组范围,由[start,stop]值表示.一些范围重叠,这意味着一个范围的开始位于另一个范围的[开始,停止]之间.我想制作一组没有这种重叠的新范围,并且不包括范围内的任何新值.
范围看起来像这样:
@starts @ends
5 108
5 187
44 187
44 229
44 236
64 236
104 236
580 644
632 770
Run Code Online (Sandbox Code Playgroud)
我期望的输出是这样的:
@starts @ends
5 236
580 770
Run Code Online (Sandbox Code Playgroud)
这是因为前七个范围与5 => 236的间隔重叠,后两个与632 => 770的间隔重叠.
这是我试过的代码:
$fix = 0;
foreach (@ends) {
if ($starts[$fix + 1] < $ends[$fix]) {
splice(@ends, $fix, $fix);
splice(@starts, $fix + 1, $fix + 1);
} else {
$fix += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以自己打印出这些值,我只需要帮助合并算法.
这会就地编辑数组,当它们重叠时简单地折叠边界。
# Since they're sorted by @starts, accept the 0th interval, start at 1
for (1..$#starts) {
# extra check on array bounds, since we edit in-place
last unless $_ < @starts;
# don't need to collapse if no overlap with previous end
next unless $starts[$_] <= $ends[$_-1];
# delete this start and the previous end
splice(@starts,$_,1);
splice(@ends,$_-1,1);
# rerun this loop for the same value of $_ since it was deleted
redo;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1111 次 |
| 最近记录: |