Solution to Longest Consecutive Sequence by LeetCode

13 Jul

Question: https://oj.leetcode.com/problems/longest-consecutive-sequence/

Question Name: Longest Consecutive Sequence

The requirement should be more accurate: your algorithm should run in O(n) complexity in average condition. I googled the other solutions. All of them used the hash table. But hash table’s time complexity is O(1) in average, and O(n) in worst case. So this solution have time complexity of O(n) in average, and O(n^2) in worst. In contrast, the sort-then-search solution can guarantee the worst complexity be O(nlogn).

Leave a Reply

Your email address will not be published. Required fields are marked *

Please put your code into a <pre>YOUR CODE</pre> section. Thanks and Happy Coding!