Longest Consecutive Sequence #leetcode #blind75

preview_player
Показать описание
Explaining Sum Root to Leaf Numbers from leetcode in Python!

LeetCode 128

Can also solve this using dfs:)

@1:02 - Example + Explanation
@3:53 - Code
@5:32 - Space and Time Complexity
@6:11 - Code Walkthrough with Example

Music: Bensound

Lemme know if you have any comments or questions!:)))

Socials:

Playlists:

Рекомендации по теме
Комментарии
Автор

just one word INCREDIBLE explanation, I understand better after explanation part! Thanks a lot and please keep going.

mirolimovv_
Автор

I LOVE your videos! Amazing explanation and very good presentation. Please don't stop making videos <3

malakbadawy
Автор

Lovely explanation from a lovely person.

deepakjyoti
Автор

# using BFS
from collections import deque

from typing import List
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
return 0

# Convert nums to set for O(1) lookup
num_set = set(nums)
visited = set()
max_length = 0

def bfs(num):
queue = deque([num])
visited.add(num)
sequence_length = 1

# Check both directions (left and right)
left = right = num

# Expand to left
while left - 1 in num_set and left - 1 not in visited:
left -= 1
queue.append(left)
visited.add(left)
sequence_length += 1

# Expand to right
while right + 1 in num_set and right + 1 not in visited:
right += 1
queue.append(right)
visited.add(right)
sequence_length += 1

return sequence_length

# Try BFS from each number that hasn't been visited
for num in nums:
if num not in visited:
max_length = max(max_length, bfs(num))

return max_length

nekdo_pac
welcome to shbcf.ru