Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
626 B
2
Indexable
Never
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;
}