Skip to content

Commit a907534

Browse files
author
MJ Kang
committed
use num set for time limit exceeded
1 parent 90318a2 commit a907534

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

longest-consecutive-sequence/mandoolala.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ class Solution:
44
def longestConsecutive(self, nums: List[int]) -> int:
55
num_set = set(nums)
66
longest = 0
7-
8-
for num in nums:
9-
if num - 1 not in num_set:
10-
cnt = 1
11-
next_num = num + 1
12-
while next_num in num_set:
13-
cnt += 1
14-
next_num += 1
15-
longest = max(longest, cnt)
7+
for num in num_set:
8+
if num - 1 in num_set:
9+
continue
10+
length = 1
11+
while num + length in num_set:
12+
length += 1
13+
longest = max(length, longest)
1614
return longest

0 commit comments

Comments
 (0)