-
Notifications
You must be signed in to change notification settings - Fork 0
/
128. Longest Consecutive Sequence.py
72 lines (50 loc) · 1.7 KB
/
128. Longest Consecutive Sequence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
https://leetcode.com/problems/longest-consecutive-sequence/
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Example 1:
Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
"""
class Solution(object):
def longestConsecutive(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
hashset = set(nums)
longest = 0
for num in nums:
if num - 1 not in hashset:
length = 1
while num + length in hashset:
length += 1
longest = max(length, longest)
return longest
"""
решение которое в 10 раз быстрее верхнего и потребляющее меньше памяти, но по скорости не О(n):
class Solution:
def longestConsecutive(self, nums):
nums = sorted(nums)
if len(nums) == 0:
return 0
lcs = 1
temp = 1
for i in range(1, len(nums)):
if nums[i-1] == nums[i]:
continue
if nums[i-1]+1 == nums[i]:
temp += 1
else:
temp = 1
if temp > lcs:
lcs = temp
return lcs
"""
s = Solution()
assert s.longestConsecutive([100, 4, 200, 1, 3, 2]) == 4
assert s.longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]) == 9