일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 315. Count of Smaller Numbers After Self
- Decorator
- 밴픽
- 컴퓨터의 구조
- data science
- Python Code
- Convert Sorted List to Binary Search Tree
- kaggle
- 30. Substring with Concatenation of All Words
- 시바견
- Python
- DWG
- attribute
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- Protocol
- concurrency
- Regular Expression
- Class
- 715. Range Module
- iterator
- Python Implementation
- 프로그래머스
- 43. Multiply Strings
- 파이썬
- shiba
- t1
- Generator
- 운영체제
- Substring with Concatenation of All Words
Archives
- Today
- Total
Scribbling
LeetCode: 128. Longest Consecutive Sequence 본문
Computer Science/Coding Test
LeetCode: 128. Longest Consecutive Sequence
focalpoint 2021. 11. 8. 21:36O(N) 알고리즘.
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
longest = 0
nums = set(nums)
for num in nums:
if num - 1 not in nums:
cnt = 1
while num + 1 in nums:
num += 1
cnt += 1
longest = max(longest, cnt)
return longest
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
numSet = set(nums)
longest = 0
while nums:
cnt = 1
l = r = nums.pop()
numSet.discard(l)
while l - 1 in numSet or r + 1 in numSet:
if l - 1 in numSet:
numSet.discard(l-1)
cnt += 1
l -= 1
if r + 1 in numSet:
numSet.discard(r+1)
cnt += 1
r += 1
longest = max(longest, cnt)
return longest
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 129. Sum Root to Leaf Numbers (0) | 2021.11.10 |
---|---|
프로그래머스: 오픈채팅방 (0) | 2021.11.08 |
프로그래머스: 방의 개수 (0) | 2021.11.08 |
LeetCode: 127. Word Ladder (0) | 2021.11.07 |
프로그래머스: 순위 (0) | 2021.11.05 |