无序数组中最长连续序列

来源:互联网 发布:d80军刀图纸数据 编辑:程序博客网 时间:2024/05/22 05:17

http://stackoverflow.com/questions/7453248/longest-consecutive-sequence-in-an-unsorted-array

1. Question:
You are given an Array of numbers and they are unsorted/random order. You are supposed to find the longest sequence of consecutive numbers in the array. 
Note the sequence need not be in sorted order within the array. Here is an example : Input : A[] = {10,21,45,22,7,2,67,19,13,45,12,11,18,16,17,100,201,20,101}  
Output is : {16,17,18,19,20,21,22}  The solution needs to be of O(n) complexity. 

2. Solution:
You could have two tables:
Start table: (start-point, length) 
End table: (ending-point, length) 

3. Step:
When adding a new item, you would check:
 1) Does value + 1 exist in start table?
a) If so, delete it and create a new entry of (value, length + 1) 
where length is the "current" length.
b) You'd also update the end tablewith the same end point but greater length. 

 2) Does value - 1 exist in the end table?
a) If so, delete it and create a new entry of (value, length + 1),
b) and this time update the start table (the starting position will be the same,but the length will be increased) 

 3) If both conditions hold, then you're effectively stitching two existing sequences
together - replace the four existing entries with two new entries, representing the single longer sequence.

 4) If neither condition holds, you just create a new entry of length 1 in both tables.
After all the values have been added, you can just iterate over the start table to  find the key with the largest value.

对于当前值value,
1)判断value + 1是否存在于start表中。
a)  如果存在,删除相应的条目,创建一个新条目(value,length + 1),
b)  同时更新end表相应条目,结束数不变,该对应长度加一。
2)判断value - 1是否存在于end表中。
a)  如果存在,删除相应的条目,创建一个新条目(value,length + 1),
b)  同时更新start表相应条目,开始数不表,该对应长度加一。
3)如果在两个表中都存在,则合并两个已经存在的连续序列为一个。将四个条目删除,新建两个条目,每两个条目代表一个连续序列。
4)如果都不存在,则只需要在两个表中创建一个新的长度为1的条目。一直这样等到数组中所有元素处理完毕,然后扫描start表寻找length值最大的那个即可。

{10,21,45,22,7,2,67,19,13,45,12, 11,18,16,17,100,201,20,101}

10
start={(10,1)}
end  ={(10,1)}

21
start={(10,1),(21,1)}
end  ={(10,1),(21,1)}

45
start={(10,1),(21,1),(45,1)}
end  ={(10,1),(21,1),(45,1)}

22
start={(10,1),(21,1),(45,1),(22,1)}
end  ={(10,1),(21,1),(45,1),(22,1)}

7
start={(10,1),(21,1),(45,1),(22,1),(7,1)}
end  ={(10,1),(21,1),(45,1),(22,1),(7,1)}

2
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1)}
end  ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1)}


67
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1)}
end  ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1)}

19
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1)}
end  ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1)}

13
start={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}
end  ={(10,1),(21,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}

45
start={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}
end  ={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,1)}

12: 12+1=13
start={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(12,2)}
end  ={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,2)}


11:11+1=12;11:11-1=10
start={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(11,3)}//11+1=12,需要更新起始数
end  ={(10,1),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,3)}//11+1=12,结束数仍为13,但是长度加1


start={(10,2),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(11,3)}//11-1=10,起始数据不变,现在有10,11两个数了
end  ={(11,2),(21,1),(45,1),(45,1),(22,1),(7,1),(2,1),(67,1),(19,1),(13,3)}//11-1=0,结束数加1,长度加1


start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,1)}//两个条件都满足,说明这个数是一个连接点,可以将start和end表中的数据合并
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,1)}


18:18+1=19
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(18,2)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,2)}


16
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(18,2),(16,1)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,2),(16,1)}


17: 17+1=18,17-1=16
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(17,3),(16,1)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,3),(16,1)}
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(17,3),(16,2)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,3),(17,2)}
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4)}


100
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4),(100,1)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4),(100,1)}


201
start={(10,4),(21,2),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4),(100,1),(201,1)}
end  ={(13,4),(22,2),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4),(100,1),(201,1)}


20:20+1=21;20-1=19;
start={(10,4),(20,3),(45,1),(45,1),(7,1),(2,1),(67,1),(16,4),(100,1),(201,1)}
end  ={(13,4),(22,3),(45,1),(45,1),(7,1),(2,1),(67,1),(19,4),(100,1),(201,1)}
start={(10,4),(20,3),(45,1),(45,1),(7,1),(2,1),(67,1),(16,5),(100,1),(201,1)}
end  ={(13,4),(22,3),(45,1),(45,1),(7,1),(2,1),(67,1),(20,5),(100,1),(201,1)}
start={(10,4),(20,7),(45,1),(45,1),(7,1),(2,1),(67,1),(100,1),(201,1)}
end  ={(13,4),(22,7),(45,1),(45,1),(7,1),(2,1),(67,1),(100,1),(201,1)}


101: 101-1=100;
start={(10,4),(20,7),(45,1),(45,1),(7,1),(2,1),(67,1),(100,2),(201,1)}
end  ={(13,4),(22,7),(45,1),(45,1),(7,1),(2,1),(67,1),(101,2),(201,1)}

0 0
原创粉丝点击