LeetCode 🏔️/Graph

128. Longest Consecutive Sequence

KB0129 2023. 7. 11. 01:15

Description:

Solution:

Sorthing is O(nlogn), so we need a solution for O(n)

if (n - 1) not in numSet, it would be the first start point of sequence. 

In while loop, we keep add the length by 1 to see how long the sequence is.

 

And then, return the longest sequence.

 

Time Complexity: O(n)

Space Complexity: O(n)