Skip to content

Commit d503348

Browse files
committed
longest consecutive sequence solution
1 parent 0a69d43 commit d503348

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def longestConsecutive(self, nums):
3+
4+
if not nums:
5+
return 0
6+
7+
num_set = set(nums)
8+
max_length = 0
9+
10+
for num in num_set:
11+
if num - 1 not in num_set:
12+
length = 1
13+
while num + length in num_set:
14+
length += 1
15+
max_length = max(max_length, length)
16+
17+
return max_length
18+
19+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from collections import Counter
2+
import heapq
23

34

45
class Solution(object):
56
def topKFrequent(self, nums, k):
67
counter = sorted(Counter(nums).items(), key=lambda item: -item[1])
78
return list(num for num, count in counter[:k])
9+

two-sum/devyejin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ def twoSum(self, nums, target):
1212
left += 1
1313
else:
1414
right -= 1
15+

0 commit comments

Comments
 (0)