Leetcode Note: Longest Consecutive Sequence

Source: Internet
Author: User

Leetcode Note: Longest Consecutive Sequence

I. Description

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given [100, 4,200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm shocould run in O (n) complexity.

Ii. Question Analysis

There are two tips for this question:

The time complexity of inserting and searching a hash table is O(1)Therefore, you can use a hash table to save the array to ensure that the search for elements in the array is constant time; a number and its adjacent numbers are in the same continuous subsequence, therefore, when a number starts to judge the longest continuous subsequence, it can mark the elements in the same continuous subsequence as the start of the judgment, because the continuous subsequence where the element is located has been processed, this can greatly reduce the number of comparisons and reduce the computing complexity;

Because the hash table in the C ++ implementation is of an unordered dictionary type, you can use the value of an array element as a keyword. In Tip 2, each element's tag bit is used as the value of each keyword.

For each element in the array, first determine whether the continuous sub-sequence in which it is located has been processed. If it has already been processed, it will directly process the next element; if it has not been processed, take it as the center, and look to the left to the right to see if there are any adjacent numbers in the array. If so, add 1 to the length and mark the position of the element, it indicates that the continuous subsequence of the element has been processed and the query continues until the adjacent number does not exist in the array. Record the sequence length and process the next element. In this method, each element is accessed only once during the longest consecutive subsequence search, so the computing complexity isO(n).

When creating a hash table, the computing complexity is alsoO(n)Therefore, the time complexity of the entire algorithm isO(n)+O(n)=O(n).

Iii. Sample Code

class Solution{public:    int longestConsecutive(vector
  
    &num)    {        // get the size of the num        int Size = num.size();        // build the hash_table        unordered_map
   
     HashTable;        for (int Index = 0; Index < Size; Index++)        {            int Tmp = num[Index];            HashTable[Tmp] = false;        }        // find the longest consecutive sequence        int LongestNumber = 1;        for (int Index = 0; Index < Size; Index++)        {            int Tmp = num[Index];            if (HashTable[Tmp])            {                continue;            }            int TmpSequence = 1;            while (HashTable.find(++Tmp) != HashTable.end())            {                HashTable[Tmp] = true;                TmpSequence++;            }            Tmp = num[Index];            while (HashTable.find(--Tmp) != HashTable.end())            {                HashTable[Tmp] = true;                TmpSequence++;            }            if (LongestNumber < TmpSequence)            {                LongestNumber = TmpSequence;            }        }        return LongestNumber;    }};
   
  

Iv. Summary

This topic can mark all the elements in the continuous subsequence during a maximum continuous subsequence search, reducing the process of searching for the longest continuous subsequence and reducing the computational complexity, the computing complexity of this search process is O (n ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.