forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest-consecutive-sequence.py
More file actions
28 lines (26 loc) · 939 Bytes
/
longest-consecutive-sequence.py
File metadata and controls
28 lines (26 loc) · 939 Bytes
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
from __future__ import print_function
# Time: O(n)
# Space: O(n)
#
# 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 O(n) complexity.
#
class Solution:
# @param num, a list of integer
# @return an integer
def longestConsecutive(self, num):
result, lengths = 1, {key: 0 for key in num}
for i in num:
if lengths[i] == 0:
lengths[i] = 1
left, right = lengths.get(i - 1, 0), lengths.get(i + 1, 0)
length = 1 + left + right
result, lengths[i - left], lengths[i + right] = max(result, length), length, length
return result
if __name__ == "__main__":
print(Solution().longestConsecutive([100, 4, 200, 1, 3, 2]))