Advertisement
YEZAELP

LeetCode: Longest Consecutive Sequence

Oct 30th, 2021 (edited)
967
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.  
  4.     int longestConsecutive(vector<int>& nums){
  5.        
  6.         int mx = 0;
  7.         unordered_map <int, int> mp;
  8.        
  9.         for(auto b: nums){
  10.             if(mp[b] != 0) continue;
  11.             int a = b - 1;
  12.             int c = b + 1;
  13.             mp[b] = mp[a] + mp[c] + 1;
  14.             mx = max(mx, mp[b]);
  15.             mp[b - mp[a]] = mp[b];
  16.             mp[b + mp[c]] = mp[b];
  17.         }
  18.        
  19.         return mx;
  20.     }
  21.  
  22. };
Advertisement
RAW Paste Data Copied
Advertisement