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 should run in O (n) complexity.
Idea: First the method of the great God is illuminated
Use a hash map to store boundary information of consecutive sequence for each element; There 4 cases when a new element I reached:
1) Neither I+1 nor I-1 has been seen:m[i]=1;
2) both i+1 and i-1 having been seen:extend M[i+m[i+1]] and m[i-m[i-1]] to all other;
3) Only i+1 have been seen:extend m[i+m[i+1]] and m[i] to all other;
4) Only i-1 have been seen:extend M[i-m[i-1]] and m[i] to all other.
intLongestconsecutive (vector<int> &num) {Unordered_map<int,int>m; intR =0; for(inti:num) { if(M[i])Continue;//Skip Repeat R= Max (r, M[i] = m[i + m[i +1]] = m[i-m[i-1]] = M[i +1] + m[i-1] +1); If the new number is connected to the left and right, the first number of the connected sequence (M[i-m[i-1]] and the last digit M[i + m[i + 1]] is set to the new length! } returnR;}
Here is my own writing, the History of blood and tears.
First O (n) indicates that it must not be sorted, and how to obtain information about adjacency. You must use a hash. The result took 2 hours to write a super-complicated code. Although AC, but ..... Alas...................................................
intLongestconsecutive (vector<int> &num) { //to repeatunordered_set<int>s; for(inti =0; I < num.size (); i++) { if(S.find (num[i]) = =s.end ()) S.insert (Num[i]); Elsenum.erase (Num.begin ()+ (i--)); } intMaxLen =0; Unordered_map<int,int> First;//record where the first digit of each successive sequence is in the recordunordered_map<int,int> Last;//record where the last digit of each successive sequence is in the recordvector<vector<int>> record;//record the first and last number of each successive sequence for(inti =0; I < num.size (); i++) { intPre, Post; Unordered_map<int,int>::iterator f = first.find (Num[i] +1); Unordered_map<int,int>::iterator L = last.find (Num[i]-1); if(f! = First.end () && l! =Last.end ()) {Pre= l->second; Post= f->second; //Modify the hash tableLast.erase (record[pre][1]); First.erase (record[post][0]); last[record[post][1]] =Pre; //Modify the interval of a recordrecord[pre][1] = record[post][1]; MaxLen= (MaxLen > record[pre][1]-record[pre][0] +1) ? maxlen:record[pre][1]-record[pre][0] +1; Record[post].clear (); } Else if(F! =First.end ()) {Post= f->second; //Modify HashFirst.erase (record[post][0]); First[num[i]]=Post; //Modify Intervalrecord[post][0] =Num[i]; MaxLen= (MaxLen > record[post][1]-record[post][0] +1) ? maxlen:record[post][1]-record[post][0] +1; } Else if(L! =Last.end ()) {Pre= l->second; Last.erase (record[pre][0]); Last[num[i]]=Pre; record[pre][1] =Num[i]; MaxLen= (MaxLen > record[pre][1]-record[pre][0] +1) ? maxlen:record[pre][1]-record[pre][0] +1; } Else{record.push_back (vector<int> (2, Num[i])); First[num[i]]= Record.size ()-1; Last[num[i]]= Record.size ()-1; MaxLen= (MaxLen >1) ? MaxLen:1; } } returnMaxLen; }
"Leetcode" longest consecutive Sequence (hard) ☆