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; }
}