[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence

124. Longest Consecutive Sequence/128. Longest Consecutive Sequence

  • 本题难度: Medium/Hard
  • Topic: Data Structure

Description

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Example

Given [100, 4, 200, 1, 3, 2],

The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Clarification

Your algorithm should run in O(n) complexity.

```

我的代码

class Solution:
    """
    @param num: A list of integers
    @return: An integer
    """
    def longestConsecutive(self, num):
        # write your code here
        dic = dict()
        res = 0
        for item in num:
            if dic.get(item, 0)==0:
                left = dic.get(item - 1, 0)
                right = dic.get(item + 1, 0)
                new = left + right + 1
                if new>res:
                    res = new
                dic[item] =  dic[item - left] = dic[item + right] = new
        return res

思路

因为需要时间复杂度为O(n),所以不能用排序做。

查找肯定不能用list,要用set或者dictionary这种hash table实现的情况。

可以扫描有限次。

没什么思路。

Leetcode分到了,Union Find。

查集(Union-Find Algorithm)

看了一下Leetcode的discussion: My really simple Java O(n) solution - Accepted

[100, 4, 200, 1, 3, 2]

New hash table []

for num in nums:

if num in hash table: continue

get left #右边靠近n的连续子列(left-left_sum,left),不存在时设为0

get right #左边靠近n的连续子列(right,right+right_sum),不存在时设为0

key(n) = key(left)+key(right)+1

形成一个新的连续子列,(n-left_sum,n+right_sum),它们新的sum为key(n)

之后的num只可能在这个子列之外,所以子列中的key无需更新。

Example

[100, 4, 200, 1, 3, 2]

# 100
[100,1]
# 4
[100,1] [4,1]
#200
[100,1] [200,1] [4,1]
# 1
[100,1] [200,1] [4,1] [1,1]
#3
[100,1] [200,1] [4,2] [3,2] [1,1]
#2
[100,1] [200,1] [4,3] [3,2] [2,3] [1,3]
  • 时间复杂度 O(n) (使用hash table降低了复杂度)
  • 空间复杂度 O(n)

原文地址:https://www.cnblogs.com/siriusli/p/10388011.html

时间: 2024-12-28 16:09:39

[Lintcode]124. Longest Consecutive Sequence/[Leetcode]128. Longest Consecutive Sequence的相关文章

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

[LeetCode] 128. Longest Consecutive Sequence

传送门 Description Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorith

Leetcode 128. Longest Consecutive Sequence (union find)

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. Example: Input: [100, 4, 200, 1, 3, 2] Output: 4 Explanation: The longest consecutive elements sequence i

[并查集] leetcode 128 Longest Consecutive Sequence

problem:https://leetcode.com/problems/longest-consecutive-sequence/ 使用并查集,时间不是严格的O(n) class Solution { public: unordered_map<int, int> p; int Find(int x) { while (x != p[x]) { x = p[x]; } return x; } void Union(int x, int y) { int px = Find(x); int

LeetCode开心刷题五十六天——128. Longest Consecutive Sequence

最近刷题进展尚可,但是形式变化了下,因为感觉眼睛会看瞎,所以好多写在纸上.本来想放到文件夹存储起来,但是太容易丢了,明天整理下,赶紧拍上来把 今晚是周末,这一周都在不停的学学学,我想下周怕是不能睡午觉了,中午回去床对我的诱惑太大了,我得想办法,一进门先把被褥收起来,再放个欢快的歌,中午少吃点,加油小可爱 之前欠下的烂帐,把太多简单题做完,导致剩下的都是难题,所以万万记住一点捷径都不要走 128看花花酱大神的解法,发现对hashtable的了解十分不足,甚至一些常见函数都不知道是干什么的 这道题涉

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

【LeetCode】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

Longest Consecutive Sequence leetcode java

题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should r

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al