Skip to content

Commit 8fc250f

Browse files
authored
pull request for creating python code for longest consecutive sequence by issue number TheAlgorithms#9436
1 parent 95345f6 commit 8fc250f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Longestconsecutivesequence.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def longest_consecutive_sequence(arr):
2+
heap = []
3+
for num in arr:
4+
heapq.heappush(heap, num)
5+
max_len = 0
6+
curr_len = 1
7+
while heap:
8+
num = heapq.heappop(heap)
9+
if heap and num == heap[0]-1:
10+
curr_len += 1
11+
elif curr_len > 1:
12+
max_len = max(max_len, curr_len)
13+
curr_len = 1
14+
if curr_len > 1:
15+
max_len = max(max_len, curr_len)
16+
return max_len

0 commit comments

Comments
 (0)