knakul853

Untitled

Jul 15th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4. class Solution {
  5. public:
  6.     int longestConsecutive(vector<int>& nums) {
  7.        
  8.         int n = (int)nums.size();
  9.         unordered_map<int, int>st;
  10.         for(int c: nums)st[c]++;
  11.        
  12.         int ans = 0;
  13.        
  14.         for(int i=0;i<n;i++)
  15.         {
  16.             int num = nums[i];
  17.             if( !st.count(num-1) )
  18.             {
  19.                 int cnt=1;
  20.                 while(st.find(++num) != st.end())cnt++;
  21.                
  22.                 ans = max(ans, cnt);
  23.             }
  24.         }
  25.         return ans;
  26.        
  27.     }
  28. };
Add Comment
Please, Sign In to add comment