[NeetCode 150] Longest Consecutive Sequence

Longest Consecutive Sequence

Given an array of integers nums, return the length of the longest consecutive sequence of elements.

A consecutive sequence is a sequence of elements in which each element is exactly 1 greater than the previous element.

You must write an algorithm that runs in O(n) time.

Example 1:

Input: nums = [2,20,4,10,3,4,5]

Output: 4

Explanation: The longest consecutive sequence is [2, 3, 4, 5].

Example 2:

Input: nums = [0,3,2,5,4,6,1,1]

Output: 7

Constraints:

0 <= nums.length <= 1000
-10^9 <= nums[i] <= 10^9

Solution

According to example 2, we can know that “sequence” here can shuffle the original order in the input array. Therefore, the order and number of the numbers in input array is not important, and we can just store them in a set() for easy searching.

After that, we can do a Memorized Search, record the longest sequence length of each number when the number is as the start number of the sequence. We can store this in a dictionary, where the key is the number and the value is the longest sequence length. Therefore, we can directly get the length of next consecutive number if the next number has already been searched. This will reduce the time complexity of search from O ( n 2 ) O(n^2) O(n2) to O ( n ) O(n) O(n)

Code

To write the process elegantly, I realize it by DFS.

If the number has been searched, then its longest sequence value will appear in the dictionary, just return it;
If the number has not been searched, check if the next consecutive number exists.
If the next number exists, go to the next one and return 1+returned value, or just return 1.

class Solution:
    def longestConsecutive(self, nums: List[int]) -> int:
        num_set = set(nums)
        length_dict = {}
        def dfs(number: int) -> int:
            if number in length_dict:
                return length_dict[number]
            if number+1 not in num_set:
                length_dict[number] = 1
                return length_dict[number]
            length_dict[number] = dfs(number+1) + 1
            return length_dict[number]
        answer = 0
        for num in num_set:
            if num not in length_dict:
                answer = max(answer, dfs(num))
        return answer
        
        

相关推荐

  1. [NeetCode 150] Valid Sudoku

    2024-07-13 05:44:05       26 阅读
  2. [NeetCode 150] Word Ladder

    2024-07-13 05:44:05       24 阅读
  3. [NeetCode 150] Redundant Connection

    2024-07-13 05:44:05       27 阅读
  4. [NeetCode 150] Longest Consecutive Sequence

    2024-07-13 05:44:05       23 阅读
  5. [NeetCode 150] Products of Array Discluding Self

    2024-07-13 05:44:05       25 阅读
  6. [NeetCode 150] Merge K Sorted Linked Lists

    2024-07-13 05:44:05       27 阅读
  7. LeetCode 150, 112, 130

    2024-07-13 05:44:05       19 阅读
  8. DAY 10 | 1047, (20,150)

    2024-07-13 05:44:05       52 阅读
  9. 面试经典150题(96-100)

    2024-07-13 05:44:05       54 阅读
  10. 面试经典150题(108-110)

    2024-07-13 05:44:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 05:44:05       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 05:44:05       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 05:44:05       62 阅读
  4. Python语言-面向对象

    2024-07-13 05:44:05       72 阅读

热门阅读

  1. sqlserver设置端口

    2024-07-13 05:44:05       23 阅读
  2. C++:using重新定义继承时访问权限

    2024-07-13 05:44:05       31 阅读
  3. git列出提交记录的文件路径

    2024-07-13 05:44:05       25 阅读
  4. 关于对于短视频的认识-复盘与再次复盘

    2024-07-13 05:44:05       25 阅读
  5. sqlalchemy反射视图

    2024-07-13 05:44:05       22 阅读
  6. vue 组件里面的方法修改外面的数据

    2024-07-13 05:44:05       26 阅读
  7. 使用Trie树高亮关键词

    2024-07-13 05:44:05       26 阅读
  8. qt 的布局

    2024-07-13 05:44:05       29 阅读
  9. 《每天十分钟》-红宝书第4版-函数

    2024-07-13 05:44:05       25 阅读
  10. 【Scrapy】Scrapy 中间件等级设置规则

    2024-07-13 05:44:05       25 阅读
  11. 智能运维提升企业长期安全防御能力

    2024-07-13 05:44:05       24 阅读
  12. Linux上如何安装ffmpeg视频处理软件

    2024-07-13 05:44:05       25 阅读