Skip to content

Commit 234883e

Browse files
authored
Update union_find.md
1 parent 07eb507 commit 234883e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

data_structure/union_find.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ class Solution:
142142
return max_length
143143
```
144144

145+
```Python
146+
class Solution:
147+
def longestConsecutive(self, nums: List[int]) -> int:
148+
nums_set = set(nums)
149+
max_len = 0
150+
# start from the lowest
151+
for num in nums_set:
152+
if num -1 not in nums_set:
153+
length = 1
154+
cur_num = num
155+
while cur_num + 1 in nums_set:
156+
length += 1
157+
cur_num += 1
158+
max_len = max(max_len, length)
159+
return max_len
160+
161+
```
162+
145163
### Kruskal's algorithm
146164

147165
### [minimum-risk-path](https://www.lintcode.com/problem/minimum-risk-path/description)

0 commit comments

Comments
 (0)