给出2个具有int开始,int结束和布尔状态的间隔,如下所示组合2个间隔
i1和i2为2 ArrayList<Intreval>
,并且res应该包含合并间隔的输出。
示例:
-INF --------- 6 ------- 10 --------- 30 --------- INF
F T T F
-INF --- 5 ------------------- 20 --------- 35 ---- INF
T T F F
输出:
-INF ---5 ---- 6 -------- 10 -- 20 ---- 30 -- 35 --- INF
F F T T F F F
代码创建{1}的i1和i2。
ArrayList<Intervals>
和
i1 has [[-INF,6,false],[6,10,true],[10,30,true],[30,INF,false]]
和
i2 has [[-INF,5,true],[5,20,true],[20,35,false],[35,INF,false]]
代码:
res should contain [[-INF,5,false],[5,6,false],[6,10,true],[10,20,true],[20,30,false],[30,35,false],[35,INF,false]]
答案 0 :(得分:1)
间隔涵盖了所有轴,因此我们只能将左端与T / F字段一起考虑。
末端已排序,因此请应用合并过程(如合并排序中的一个)。
ia = 0
ib = 0
//Start the first output interval code
while ia < ACount and B < BCount
if A[ia].Position <= B[ib].Position
ia++
AActive = True
else if A[ia].Value >= B[ib].Value //reuse == case for simplicity
// and incrementing both indexes
ib++
AActive = False
State = A[ia].BooleanField && B[ib].BooleanField
Pos = A[ia].Position if AActive else B[ib].Position
CloseOutputInterval(Pos)
StartOutputInterval(Pos, State)
continue with the rest (A or B left)