Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- **/
- class Solution {
- public:
- int longestConsecutive(vector<int>& nums) {
- int n = (int)nums.size();
- unordered_map<int, int>st;
- for(int c: nums)st[c]++;
- int ans = 0;
- for(int i=0;i<n;i++)
- {
- int num = nums[i];
- if( !st.count(num-1) )
- {
- int cnt=1;
- while(st.find(++num) != st.end())cnt++;
- ans = max(ans, cnt);
- }
- }
- return ans;
- }
- };
Add Comment
Please, Sign In to add comment