본문 바로가기
알고리즘/소스코드

LEETCODE 128. Longest Consecutive Sequence

by cjw.git 2021. 8. 5.

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

  1.  

 

 

 

cjw.git@gmail.com

댓글