Posted on

Description

Submission

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> st;

        for(auto x: nums) {
            st.insert(x);
        }

        int ret = 0;
        for(auto x: nums) {
            if(!st.count(x)) continue;
            st.erase(x);
            int cur = 1;

            for(int i = x + 1; st.count(i); ++i) {
                st.erase(i);
                ++cur;
            }

            for(int i = x - 1; st.count(i); --i) {
                st.erase(i);
                ++cur;
            }

            ret = max(cur, ret);
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *