Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Longest Consecutive Sequence

aero2146 Apr 10th, 2020 (edited) 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution:
  2.     def longestConsecutive(self, nums):
  3.         longest_streak = 0
  4.         num_set = set(nums)
  5.  
  6.         for num in num_set:
  7.             if num - 1 not in num_set:
  8.                 current_num = num
  9.                 current_streak = 1
  10.  
  11.                 while current_num + 1 in num_set:
  12.                     current_num += 1
  13.                     current_streak += 1
  14.  
  15.                 longest_streak = max(longest_streak, current_streak)
  16.  
  17.         return longest_streak
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top