nathanwailes

LeetCode 128 - Longest Consecutive Sequence - 2023.10.02 solution

Oct 1st, 2023
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. class Solution:
  2.     def longestConsecutive(self, nums: List[int]) -> int:
  3.         """ If it was sorted, we could just track the longest sequence as
  4.        we went.  But sorting is O(nlogn) time.  To get O(n) time we may need
  5.        to use extra memory, like by using a dict. Also it's important to
  6.        remember that we can go through the list multiple times and still have
  7.        an O(n) solution.
  8.        The question to ask yourself is: what would you want to know, when
  9.        looking at each element, to know if it's the element you need to
  10.        return?
  11.        In our case, we'd want to know what the length of the sequence is of
  12.        which this element is a part.  And then after iterating through the
  13.        entire list and storing a variable to keep track of the longest
  14.        sequence we've seen we'd have our answer.
  15.        So the next question is: how could we determine the length of the sequence of
  16.        each element as we came to that element? A: If we knew, for each element,
  17.        whether there existed in the list the previous element and next
  18.        element, we could traverse each sequence as we came to each element
  19.        to determine its length.
  20.        
  21.        But we don't actually need to check *every* element as we come to
  22.        it, but rather just
  23.        one per sequence, and we'd get the answer (the length of the
  24.        longest sequence). So which element of each sequence should we
  25.        check?  The only ones we know will exist in every sequence are
  26.        the first and last elements of the sequence because we can't just say
  27.        'oh we'll use the second element' if we don't know if all the
  28.        sequences are at least 2 long.
  29.        So if we're only checking the length of a sequence for the first
  30.        element, then we need to know, when we get to each element in the
  31.        original list, whether it's the first of a sequence or not. And we
  32.        can do that by using our dict to check if the number one less than it
  33.        is in the list or not.
  34.  
  35.        So, what we need to know:
  36.        - For each element, the index of an element with a value of 1 less
  37.          than it, if such an element exists
  38.        - For each element, the index of an element with a value 1 more than
  39.          it, if it exists.
  40.        - We could do this with one dict mapping from value to index.
  41.        """
  42.         num_set = set(nums)
  43.         longest_length = 0
  44.         for n in nums:
  45.             if n - 1 in num_set:
  46.                 continue
  47.             length = 0
  48.             j = n
  49.             while j in num_set:
  50.                 length += 1
  51.                 j += 1
  52.             longest_length = max(length, longest_length)
  53.         return longest_length
  54.  
Advertisement
Add Comment
Please, Sign In to add comment