Gets the longest contiguous sequence of elements in an array. For example, given [31,6,32,1,3,2], the longest contiguous sequence of elements is [1,2,3], returning its length 3

Source: Internet
Author: User

Problem-solving analysis determines whether the current node I belongs to a sequence. If the array[i]-1 exists in the array, then the array[i] can be added as a successor sequence. Similarly, if the array[i]+1 exists in the array, then array[i] can be added as a precursor to the sequence.
we find that processing the current node needs to depend on previous partial results: To determine if array[i]-1,array[i]+1 exists in the array. How to save the previous processing results. You can use a hash table.
Obviously, key keys correspond to numeric values.
Key keys are used to quickly determine if there are any elements in the hash table that we need, and value values provide the results we need. In this subject, we need to know what sequence is now formed. Because a sequence is a series of consecutive integers, the sequence can be uniquely determined as long as the minimum and maximum values of the sequence are known. The so-called "as a successor to join the sequence", "as a precursor to join the sequence", is to update the maximum value, the minimum value. Therefore, the value of a hash table is a structure that can record the maximum and the minimum value, which describes the longest sequence that the current node participates in.
Complexity analysis can obtain results as long as the array is scanned, so the time complexity is O (n) and the space complexity is O (n).

struct bound{int low;

    int high;
        Bound (int l,int high) {low = L;
    High = h;

    } int lengthofconsecutivesequence (vector<int> &nums) {unordered_map<int, bound> MP;
    int maxlen = 0;

    int local;

        for (int i=0;i<nums.size (); i++) {if (Mp.count (nums[i)) continue;
        local = Nums[i];

        int low = local, high = local;
        if (Mp.count (LOCAL-1)) {low = Mp[local-1].low;
        } if (Mp.count (local+1)) {high = Mp[local+1].high;
        } Mp[low].high = Mp[local].high = high;

        Mp[high].low = Mp[local].low = low;
        if (high-low+1>maxlen) maxlen = high-low+1;

    return maxlen; }
}

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.