Untitled
unknown
plain_text
2 years ago
674 B
20
Indexable
class Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> s; // hashmap store key value pair , set just store value
for(auto i:nums) s.insert(i);
int longseq=0;
for(auto i:nums){
if(s.find(i-1)==s.end()){ // its not a ending of a sequence, start checking for sequence
int curseq=1;
while(s.find(i+1)!=s.end()){
s.erase(i+1); // HERE
curseq++;
i++;
}
longseq=max(longseq,curseq);
}
}
return longseq;
}
};Editor is loading...