My Blog List

[Leetcode Solution] Longest Consecutive Sequence

DIDN"T COME UP WITH THE ANSWER ALONE

Analysis

  • The time complexity requirement is O(n) thus any sort cannot be applied, hash is the obvious selection
  • Using a hash table to store if a integer appears 
  • For each integer in the input vector, searching the longest sequence by extending to bi-direction and use hash table to detect if the corresponding element appears in the list. Erase the element when searching which makes the time complexity remains O(n) because each element can be visited only once

Note

  • The difference between map and unordered_map can be found here

Code


No comments:

Post a Comment

Enter you comment