Link : https://leetcode.com/problems/longest-consecutive-sequence/
Python
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Solution:
def DFS(self, start, length):
if start not in self.memory or self.memory[start]:
return length
self.memory[start] = True
return self.DFS(start + 1, length + 1)
def longestConsecutive(self, nums: list[int]) -> int:
self.memory = dict()
for n in nums:
self.memory[n] = False
if len(nums) == 0:
return 0
result = self.DFS(min(nums), 0)
while True:
temp = [data for data in self.memory if not self.memory[data]]
if len(temp) == 0:
break
var = self.DFS(min(temp), 0)
if var > result:
result = var
return result
|
cs |
FeedBack
cjw.git@gmail.com
'알고리즘 > 소스코드' 카테고리의 다른 글
PROGRAMMERS 72411: 메뉴 리뉴얼 (0) | 2021.12.31 |
---|---|
LEETCODE 287: Find the Duplicate Number (0) | 2021.08.05 |
LEETCODE 46: PERMUTATIONS (0) | 2021.08.05 |
PROGRAMMERS 49189: 가장 먼 노드 (0) | 2021.07.27 |
PROGRAMMERS 42861: 섬 연결하기 (0) | 2021.07.18 |
댓글