Untitled
unknown
plain_text
2 years ago
626 B
10
Indexable
int longestConsecutive(std::vector<int>& nums) {
std::unordered_set<int> bin;
int global_max = 0;
for (auto i : nums) {
bin.insert(i);
}
for (auto i : bin) {
if (bin.count(i-1)) {
// not a sequence
continue;
} else {
// sequence
int current_max = 1;
while (bin.count(++i)) {
current_max++;
}
global_max = std::max(current_max, global_max);
}
}
return global_max;
}Editor is loading...