post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/valid-palindrome/discuss/1714544/Two-pointer-Python-solution-O(1)-space-with-comments
class Solution: def isPalindrome(self, s: str) -> bool: # Define two pointers starting at the end and beginning of the string left = 0 right = len(s) - 1 # Iterate and increment the pointers towards the middle. # Stop when left == right (odd len(s)) or left > right (...
valid-palindrome
Two pointer Python solution, O(1) space with comments
nat_5t34
1
147
valid palindrome
125
0.437
Easy
1,400
https://leetcode.com/problems/valid-palindrome/discuss/1585786/Python-Easy-Solution-or-O(n)-Approach
class Solution: def isPalindrome(self, s: str) -> bool: start = 0 s = s.lower() end = len(s)-1 while start < end: if not s[start].isalnum(): start += 1 continue if not s[end].isalnum(): end -= 1 continue if s[start] != s[end]: return False start += 1 end -= 1 return True
valid-palindrome
Python Easy Solution | O(n) Approach
leet_satyam
1
186
valid palindrome
125
0.437
Easy
1,401
https://leetcode.com/problems/valid-palindrome/discuss/1256371/Python3-simple-solution-by-two-approaches
class Solution: def isPalindrome(self, s: str) -> bool: x = '' for i in s: i = i.lower() if i.isalnum(): x += i return x == x[::-1]
valid-palindrome
Python3 simple solution by two approaches
EklavyaJoshi
1
96
valid palindrome
125
0.437
Easy
1,402
https://leetcode.com/problems/valid-palindrome/discuss/1256371/Python3-simple-solution-by-two-approaches
class Solution: def isPalindrome(self, s: str) -> bool: i = 0 j = len(s)-1 x = '' y = '' while i<=j: if x == '': if s[i].lower().isalnum(): x = s[i].lower() else: i += 1 if y == ''...
valid-palindrome
Python3 simple solution by two approaches
EklavyaJoshi
1
96
valid palindrome
125
0.437
Easy
1,403
https://leetcode.com/problems/valid-palindrome/discuss/1245962/Python3-95-time-with-list-comprehension-explained
class Solution: def isPalindrome(self, s: str) -> bool: s = [c for c in s.lower() if c.isalnum()] return s == s[::-1]
valid-palindrome
Python3 95% time, with list comprehension, explained
albezx0
1
151
valid palindrome
125
0.437
Easy
1,404
https://leetcode.com/problems/valid-palindrome/discuss/1074210/Simplest-Solution
class Solution: def isPalindrome(self, s: str) -> bool: y=''.join(c for c in s if c.isalnum()) y=y.lower() if(y==y[::-1]): return True else: return False
valid-palindrome
Simplest Solution
parasgarg395
1
129
valid palindrome
125
0.437
Easy
1,405
https://leetcode.com/problems/valid-palindrome/discuss/958812/Python-2-liner-easy-solution-with-list-comprehension!
class Solution: def isPalindrome(self, s: str) -> bool: if s == '': return True new = ''.join([x.lower() for x in s if x.isalnum()]) return new == new[::-1]
valid-palindrome
Python 2-liner, easy solution with list comprehension!
yahoo123pl
1
157
valid palindrome
125
0.437
Easy
1,406
https://leetcode.com/problems/valid-palindrome/discuss/902617/Python3-solution(Both-O(n)-space-complexity-and-O(1)-space-complexity)
class Solution: def isPalindrome(self, s: str) -> bool: if not s: return True new_s=[i for i in s.lower() if i.isalnum()] reverse=new_s[::-1] return reverse==new_s #O(1) solution class Solution: def isPalindrome(self, s: str) -> bool: if not s: return True lef...
valid-palindrome
Python3 solution(Both O(n) space complexity and O(1) space complexity)
iammyashh
1
140
valid palindrome
125
0.437
Easy
1,407
https://leetcode.com/problems/valid-palindrome/discuss/770390/Python-or-Easy-Solution-for-beginners-with-comments
class Solution(object): def isPalindrome(self, s): s = s.lower() #Converts string to lower x = '' for i in s: if i.isalnum(): # Checks if string is alphanumeric x+=(i) return x==x[::-1]
valid-palindrome
Python | Easy Solution for beginners with comments
rachitsxn292
1
189
valid palindrome
125
0.437
Easy
1,408
https://leetcode.com/problems/valid-palindrome/discuss/695623/Python3-two-approaches
class Solution: def isPalindrome(self, s: str) -> bool: lo, hi = 0, len(s)-1 while lo < hi: if not s[lo].isalnum(): lo += 1 elif not s[hi].isalnum(): hi -= 1 elif s[lo].lower() != s[hi].lower(): return False else: lo, hi = lo+1, hi-1 return T...
valid-palindrome
[Python3] two approaches
ye15
1
123
valid palindrome
125
0.437
Easy
1,409
https://leetcode.com/problems/valid-palindrome/discuss/695623/Python3-two-approaches
class Solution: def isPalindrome(self, s: str) -> bool: s = "".join(c for c in s.lower() if c.isalnum()) return s == s[::-1]
valid-palindrome
[Python3] two approaches
ye15
1
123
valid palindrome
125
0.437
Easy
1,410
https://leetcode.com/problems/valid-palindrome/discuss/695623/Python3-two-approaches
class Solution: def isPalindrome(self, s: str) -> bool: return (lambda x: x == x[::-1])([c for c in s.lower() if c.isalnum()])
valid-palindrome
[Python3] two approaches
ye15
1
123
valid palindrome
125
0.437
Easy
1,411
https://leetcode.com/problems/valid-palindrome/discuss/585988/Easy-Python-Solution
class Solution: def isPalindrome(self, s: str) -> bool: if len(s)==1: return True k="" for i in range(len(s)): if s[i].isalnum(): k+=s[i].lower() print(k) if k=="": return True ...
valid-palindrome
Easy Python Solution
Ayu-99
1
82
valid palindrome
125
0.437
Easy
1,412
https://leetcode.com/problems/valid-palindrome/discuss/2844950/Easy-understand-but-use-more-space
class Solution: def isPalindrome(self, s: str) -> bool: # Format the string temp = [letter.lower() for letter in s if letter.isalnum()] # Tow pointer to check left = 0 right = len(temp) - 1 while left <= right: if temp[left] != temp[right...
valid-palindrome
Easy understand but use more space
JennyLu
0
3
valid palindrome
125
0.437
Easy
1,413
https://leetcode.com/problems/valid-palindrome/discuss/2843588/Easy-Solution-Using-Alnum-Python
class Solution: def isPalindrome(self, s: str) -> bool: lowerS = s.lower() old = [" ".join(i for i in lowerS if i.isalnum())] if "".join(old) == "".join(old[0][::-1]): return True else: return False
valid-palindrome
Easy Solution - Using Alnum - Python
bharatvishwa
0
1
valid palindrome
125
0.437
Easy
1,414
https://leetcode.com/problems/valid-palindrome/discuss/2841854/Python-or-Two-pointer-solution.
class Solution: def isPalindrome(self, s: str) -> bool: l,r = 0, len(s)-1 while l<r: while s[l].isalnum() == False and l<r: l +=1 while s[r].isalnum() == False and l<r: r -=1 if s[l].lower() != s[r].lower(): return F...
valid-palindrome
Python | Two pointer solution.
float_boat
0
1
valid palindrome
125
0.437
Easy
1,415
https://leetcode.com/problems/valid-palindrome/discuss/2841821/Python-or-Two-pointer-solution
class Solution: def isPalindrome(self, s: str) -> bool: Snew = "" for char in s: if char.isalnum(): Snew += char.lower() l,r = 0, len(Snew)-1 while l<r: if Snew[l] != Snew[r]: return False l +=1 r -=1 ...
valid-palindrome
Python | Two pointer solution
float_boat
0
1
valid palindrome
125
0.437
Easy
1,416
https://leetcode.com/problems/valid-palindrome/discuss/2841801/Python-solution-or-Reverse-the-string
class Solution: def isPalindrome(self, s: str) -> bool: Snew = "" for char in s: if char.isalnum(): Snew += char.lower() return Snew == Snew[::-1]
valid-palindrome
Python solution | Reverse the string
float_boat
0
1
valid palindrome
125
0.437
Easy
1,417
https://leetcode.com/problems/word-ladder-ii/discuss/2422401/46ms-Python-97-Faster-Working-Multiple-solutions-95-memory-efficient-solution
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: d = defaultdict(list) for word in wordList: for i in range(len(word)): d[word[:i]+"*"+word[i+1:]].append(word) if endWord not in wordList: return [] visited1 = defaultdict(list) q...
word-ladder-ii
46ms Python 97 Faster Working Multiple solutions 95% memory efficient solution
anuvabtest
35
2,800
word ladder ii
126
0.276
Hard
1,418
https://leetcode.com/problems/word-ladder-ii/discuss/2422401/46ms-Python-97-Faster-Working-Multiple-solutions-95-memory-efficient-solution
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: res = [] edge = collections.defaultdict(set) wordList = set(wordList) for word in wordList: for i in range(len(word)): edge[word[:i] +'*'+word[i+1:]].add(word) bfsedge = {} def bfs(...
word-ladder-ii
46ms Python 97 Faster Working Multiple solutions 95% memory efficient solution
anuvabtest
35
2,800
word ladder ii
126
0.276
Hard
1,419
https://leetcode.com/problems/word-ladder-ii/discuss/1890299/Python-BFS%2BDFS-beats100
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: prefix_d = defaultdict(list) for word in wordList: for i in range(0,len(word)): prefix_d[word[0:i]+"*"+word[i+1:]].append(word) order = {beginWord...
word-ladder-ii
Python BFS+DFS beats100%
juehuil
4
335
word ladder ii
126
0.276
Hard
1,420
https://leetcode.com/problems/word-ladder-ii/discuss/2262529/python3-bidirectional-BFS-or-does-not-TLE-in-July-2022
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: list[str]) -> list[list[str]]: if endWord not in wordList: return [] lw = len(beginWord) # build variants list/graph variants: dict[str, list[str]] = defaultdict(list) for word in wordL...
word-ladder-ii
[python3] bidirectional BFS | does not TLE in July 2022
parmenio
3
532
word ladder ii
126
0.276
Hard
1,421
https://leetcode.com/problems/word-ladder-ii/discuss/1996030/Python-BFS-code-with-explanation
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: # check if endword is in wordlist if endWord not in wordList: return [] # insert a new value for the first time, the default value is an empty list nei = c...
word-ladder-ii
Python BFS code with explanation
JinlingXING
3
391
word ladder ii
126
0.276
Hard
1,422
https://leetcode.com/problems/word-ladder-ii/discuss/2422963/BFS-%2B-DFS-%2B-Memo
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: word_set = set(wordList) if endWord not in word_set: return [] # bfs build graph + dfs memo graph = defaultdict(set) queue = Deque([beginWord]) ste...
word-ladder-ii
BFS + DFS + Memo
Yan_Yichun
2
204
word ladder ii
126
0.276
Hard
1,423
https://leetcode.com/problems/word-ladder-ii/discuss/2207679/14.2MB260mspython3
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList): def similiar(l1,l2): counter = 0 for i in range(len(l1)): if l1[i] != l2[i]: counter += 1 if counter > 1: break if counte...
word-ladder-ii
14.2MB,260ms,python3
Bin123
2
124
word ladder ii
126
0.276
Hard
1,424
https://leetcode.com/problems/word-ladder-ii/discuss/1360434/Python-Easy-BFS-solution
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: "Edge Case checking" if endWord not in wordList: return [] size = len(beginWord) "create llokup got all the possible wordpatters" ...
word-ladder-ii
Python , Easy BFS solution
shankha117
2
150
word ladder ii
126
0.276
Hard
1,425
https://leetcode.com/problems/word-ladder-ii/discuss/991528/Python-BFS-%2B-DFS
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: def build_graph() -> dict: graph = defaultdict(list) for word in wordList: for i in range(N): w = word[:i] + "*" + word[i+1:] ...
word-ladder-ii
Python BFS + DFS
alexvlis_d
2
348
word ladder ii
126
0.276
Hard
1,426
https://leetcode.com/problems/word-ladder-ii/discuss/704422/Python3-simple-and-two-end-bfs-(98.6)
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: graph = dict() for word in wordList: for i in range(len(word)): graph.setdefault(word[:i] + "*" + word[i+1:], []).append(word) ans = [] ...
word-ladder-ii
[Python3] simple & two-end bfs (98.6%)
ye15
2
393
word ladder ii
126
0.276
Hard
1,427
https://leetcode.com/problems/word-ladder-ii/discuss/704422/Python3-simple-and-two-end-bfs-(98.6)
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: if endWord not in wordList: return [] graph = dict() for word in wordList: for i in range(len(word)): graph.setdefault(word[:i] + "*" + word[i+1:]...
word-ladder-ii
[Python3] simple & two-end bfs (98.6%)
ye15
2
393
word ladder ii
126
0.276
Hard
1,428
https://leetcode.com/problems/word-ladder-ii/discuss/704422/Python3-simple-and-two-end-bfs-(98.6)
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: if endWord not in wordList: return [] # edge case graph = {} for word in wordList: for i in range(len(word)): key = word[:i] + "*" + word[i+1:]...
word-ladder-ii
[Python3] simple & two-end bfs (98.6%)
ye15
2
393
word ladder ii
126
0.276
Hard
1,429
https://leetcode.com/problems/word-ladder-ii/discuss/1200831/Python-bfs-%2B-dfs-faster-than-85%2B
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: # bfs with memory wordDict = set(wordList) if endWord not in wordDict: return [] l, s, used, flag, parent = len(beginWord), {beginWord}, set(), True, defaultdict(list) ...
word-ladder-ii
Python bfs + dfs, faster than 85+%
dustlihy
1
394
word ladder ii
126
0.276
Hard
1,430
https://leetcode.com/problems/word-ladder-ii/discuss/2706979/One-small-change-to-word-ladder-1
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: letters = "abcdefghijklmnopqrstuvwxyz" wordList = set(wordList) queue = deque() queue.append([beginWord]) if beginWord in wordList: wordList.remove(beginWord) ...
word-ladder-ii
One small change to word ladder 1
shriyansnaik
0
22
word ladder ii
126
0.276
Hard
1,431
https://leetcode.com/problems/word-ladder-ii/discuss/2640330/Solution-only-work-for-3236-cases
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: if endWord not in wordList: return [] n, l = len(wordList), len(beginWord) # construct adj adj = collections.defaultdict(list) for w in wordList: ...
word-ladder-ii
Solution only work for 32/36 cases
YYYami
0
26
word ladder ii
126
0.276
Hard
1,432
https://leetcode.com/problems/word-ladder-ii/discuss/2423508/O(n)-with-bfs-%2B-building-tries-%2B-back-track-same-level-nodes
class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: """ cog + log + + dog """ def ppath(p): if d.get(p): path.append(p) if d[p][1]==-1: ...
word-ladder-ii
O(n) with bfs + building tries + back-track same-level nodes
dntai
0
89
word ladder ii
126
0.276
Hard
1,433
https://leetcode.com/problems/word-ladder/discuss/1332551/Elegant-Python-Iterative-BFS
class Solution(object): def ladderLength(self, beginWord, endWord, wordList): graph = defaultdict(list) for word in wordList: for index in range(len(beginWord)): graph[word[:index] + "_" + word[index+1:]].append(word) queue = deque() queue.append((beginW...
word-ladder
Elegant Python Iterative BFS
soma28
4
366
word ladder
127
0.368
Hard
1,434
https://leetcode.com/problems/word-ladder/discuss/2506114/python-solution-or-BFS-or-faster-than-94-solutions
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 nei = collections.defaultdict(list) wordList.append(beginWord) for word in wordList: for j in range(len(word)): pattern = word[:j] + "*" + word[j + 1:] nei[patte...
word-ladder
python solution | BFS | faster than 94% solutions
nikhitamore
2
225
word ladder
127
0.368
Hard
1,435
https://leetcode.com/problems/word-ladder/discuss/1604369/Python-Simple-BFS
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 q = [(beginWord, 1)] wordList = set(wordList) # TLE if you don't convert into a set seen = set([beginWord]) ...
word-ladder
Python Simple BFS
ranasaani
2
282
word ladder
127
0.368
Hard
1,436
https://leetcode.com/problems/word-ladder/discuss/1200737/Python-BFS-with-pruning-faster-than-95%2B
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: # bfs wordList = set(wordList) if endWord not in wordList: return 0 l = len(beginWord) begin_queue, end_queue = deque([beginWord]), deque([endWord]) level = 0 be...
word-ladder
Python BFS with pruning, faster than 95+%
dustlihy
2
367
word ladder
127
0.368
Hard
1,437
https://leetcode.com/problems/word-ladder/discuss/1200737/Python-BFS-with-pruning-faster-than-95%2B
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: # bfs wordList = set(wordList) if endWord not in wordList: return 0 l = len(beginWord) begin_queue, end_queue = deque([beginWord]), deque([endWord]) level = 0 w...
word-ladder
Python BFS with pruning, faster than 95+%
dustlihy
2
367
word ladder
127
0.368
Hard
1,438
https://leetcode.com/problems/word-ladder/discuss/1200737/Python-BFS-with-pruning-faster-than-95%2B
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: # bfs wordList = set(wordList) if endWord not in wordList: return 0 l = len(beginWord) begin_queue, end_queue = deque([beginWord]), deque([endWord]) level = 0 w...
word-ladder
Python BFS with pruning, faster than 95+%
dustlihy
2
367
word ladder
127
0.368
Hard
1,439
https://leetcode.com/problems/word-ladder/discuss/1200737/Python-BFS-with-pruning-faster-than-95%2B
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: # bfs wordList = set(wordList) if endWord not in wordList: return 0 l, s1, s2 = len(beginWord), {beginWord}, {endWord} wordList.remove(endWord) level = 0 ...
word-ladder
Python BFS with pruning, faster than 95+%
dustlihy
2
367
word ladder
127
0.368
Hard
1,440
https://leetcode.com/problems/word-ladder/discuss/1200737/Python-BFS-with-pruning-faster-than-95%2B
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: # bfs wordList = set(wordList) if endWord not in wordList: return 0 l, s1, s2 = len(beginWord), {beginWord}, {endWord} wordList.remove(endWord) level = 0 ...
word-ladder
Python BFS with pruning, faster than 95+%
dustlihy
2
367
word ladder
127
0.368
Hard
1,441
https://leetcode.com/problems/word-ladder/discuss/2755577/PYTHON-SOLUTION-USING-BFS-ALGORITHM
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: s=set(wordList) l=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z'] queue=deque([]) queue.append([beginWord,0]) w...
word-ladder
PYTHON SOLUTION USING BFS ALGORITHM
shashank_2000
1
393
word ladder
127
0.368
Hard
1,442
https://leetcode.com/problems/word-ladder/discuss/2456833/Intuitive-solution-without-the-wildcard-logic-only-9-faster-though
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: """ approach: 1. make the adjacency list 2. run bfs """ wordList.insert(0, beginWord) wordList = set(wordList) visitedDic = {} i...
word-ladder
Intuitive solution without the wildcard logic, only 9% faster though
abhineetsingh192
1
47
word ladder
127
0.368
Hard
1,443
https://leetcode.com/problems/word-ladder/discuss/704077/Python3-two-end-bfs-(99.49)
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 #shortcut graph = dict() for word in wordList: for i in range(len(word)): graph.setdefault(word[:i] + "*" + word[i+1:]...
word-ladder
[Python3] two-end bfs (99.49%)
ye15
1
130
word ladder
127
0.368
Hard
1,444
https://leetcode.com/problems/word-ladder/discuss/704077/Python3-two-end-bfs-(99.49)
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 #shortcut graph = dict() for word in wordList: for i in range(len(word)): graph.setdefault(word[:i] + "*" + word[i+1:]...
word-ladder
[Python3] two-end bfs (99.49%)
ye15
1
130
word ladder
127
0.368
Hard
1,445
https://leetcode.com/problems/word-ladder/discuss/704077/Python3-two-end-bfs-(99.49)
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 # edge case mp = {} for word in wordList: for i in range(len(word)): key = word[:i] + "*" + word[i+1:] ...
word-ladder
[Python3] two-end bfs (99.49%)
ye15
1
130
word ladder
127
0.368
Hard
1,446
https://leetcode.com/problems/word-ladder/discuss/2842662/Graph-Shortest-Path-BFS
class Node: def __init__(self, val): self.val = val self.lvl = 1 class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 d = set(wordList) wordCombo = defaultdict(set) for...
word-ladder
Graph Shortest Path BFS
Adeyinka
0
2
word ladder
127
0.368
Hard
1,447
https://leetcode.com/problems/word-ladder/discuss/2705779/Simplest-BFS-solution
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: letters = "abcdefghijklmnopqrstuvwxyz" wordList = set(wordList) queue = deque() queue.append((beginWord,1)) if beginWord in wordList: wordList.remove(beginWord) ...
word-ladder
Simplest BFS solution
shriyansnaik
0
7
word ladder
127
0.368
Hard
1,448
https://leetcode.com/problems/word-ladder/discuss/2668790/BFS-python-with-explanation
class Solution: def ladderLength(self, begin: str, end: str, word_list: List[str]) -> int: words = set(word_list) # make a set because existence query is O(1) vs O(N) for list queue = deque([begin]) distance = 1 while len(queue) > 0: n = len(queue) distance +=...
word-ladder
BFS - python with explanation
user3734a
0
3
word ladder
127
0.368
Hard
1,449
https://leetcode.com/problems/word-ladder/discuss/2454761/help-me-optimize-this-code-getting-TLE
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: """ approach: 1. make the adjacency list 2. run bfs """ wordList.insert(0, beginWord) visitedDic = {} if endWord not in wordList: return...
word-ladder
help me optimize this code, getting TLE
abhineetsingh192
0
29
word ladder
127
0.368
Hard
1,450
https://leetcode.com/problems/word-ladder/discuss/2428100/Faster-Than-98-Bidirectional-BFS
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 n = len(beginWord) h = collections.defaultdict(list) for word in wordList: for i in range(n): h[word[:i] + "*...
word-ladder
Faster Than 98 %, Bidirectional BFS
saurabh0225
0
62
word ladder
127
0.368
Hard
1,451
https://leetcode.com/problems/word-ladder/discuss/2424207/Clean-divided-into-easy-to-follow-parts-python3-solution()
class Solution: # O(n * m^2) time, n --> len(wordList), m --> len(wordList[i]) # O(n*m) space, # Approach: BFS, hashtable, string def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: vstd = set() pattern_map = {} def buildPattern() -...
word-ladder
Clean, divided into easy to follow parts, python3 solution()
destifo
0
6
word ladder
127
0.368
Hard
1,452
https://leetcode.com/problems/word-ladder/discuss/2423650/Python3-or-Easy-to-Understand-or-Efficient-or-Faster-than-99.28
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 # create adjacency list wordList.append(beginWord) neighbors = collections.defaultdict(list) for word in wordList: ...
word-ladder
✅Python3 | Easy to Understand | Efficient | Faster than 99.28%
thesauravs
0
15
word ladder
127
0.368
Hard
1,453
https://leetcode.com/problems/word-ladder/discuss/2351613/Python-BFS-Chinese
class Solution: def ladderLength(self, b: str, e: str, lists: List[str]) -> int: if e not in lists: return 0 ls = string.ascii_lowercase # 所有lowercase字母 q = collections.deque([b]) lists = set(lists) res = 1 while q: s...
word-ladder
Python BFS Chinese 中文
scr112
0
60
word ladder
127
0.368
Hard
1,454
https://leetcode.com/problems/word-ladder/discuss/2086062/Python-BFS-Solution
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: d = {} wordList.append(beginWord) ans = 1 visited = set({}) for word in wordList: for j in range(len(word)): pattern = word[:j] + '*' + word[...
word-ladder
Python BFS Solution
DietCoke777
0
102
word ladder
127
0.368
Hard
1,455
https://leetcode.com/problems/word-ladder/discuss/1249010/Python-O(beginWord.length*26*n)-solution-using-BFS
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord in wordList == False: return 0 n, ans = len(wordList), 0 mp, chars, queue = Counter(wordList), list(string.ascii_lowercase), deque() queue.append(beginWord) ...
word-ladder
Python O(beginWord.length*26*n) solution using BFS
m0biu5
0
88
word ladder
127
0.368
Hard
1,456
https://leetcode.com/problems/word-ladder/discuss/503648/Python3-BFS-solution-too-slow-even-after-all-optimizations-from-other-threads.-Help
class Solution: def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int: if endWord not in wordList: return 0 visited = {} helper = {} for word in wordList: helper[word] = [word[:i]+'*'+word[i+1:] for i in range(len(word))] que...
word-ladder
[Python3] BFS solution too slow even after all optimizations from other threads. Help?
elstersen
0
122
word ladder
127
0.368
Hard
1,457
https://leetcode.com/problems/word-ladder/discuss/238033/Python3-BFS-set
class Solution: def ladderLength(self, beginWord: 'str', endWord: 'str', wordList: 'List[str]') -> 'int': wordSet = set(wordList) if endWord not in wordSet: return 0 wordDict = {1:[beginWord]} output = 1 while True: words = wordDict[output] ...
word-ladder
Python3 BFS set
xiangyupeng1994
0
145
word ladder
127
0.368
Hard
1,458
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1109808/Python-Clean-Union-Find-with-explanation
class Node: def __init__(self, val): self.val = val self.parent = self self.size = 1 class UnionFind: def find(self, node): if node.parent != node: node.parent = self.find(node.parent) return node.parent def union(self, node1, node2): ...
longest-consecutive-sequence
[Python] Clean Union Find with explanation
l3arner
21
1,800
longest consecutive sequence
128
0.489
Medium
1,459
https://leetcode.com/problems/longest-consecutive-sequence/discuss/773690/Simple-O(n)-Python-Solution-with-Explanation
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: return 0 num_set = set(nums) longest = 0 for n in nums: if n-1 not in num_set: length = 0 while n in num_set: length...
longest-consecutive-sequence
Simple O(n) Python Solution with Explanation
user1469X
4
700
longest consecutive sequence
128
0.489
Medium
1,460
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2546572/Python-solution-keeping-track-of-current-max-and-current-count
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums = sorted(set(nums)) cur_max = 0 cur_count = 0 prev = None for i in nums: if prev is not None: if prev+1 == i: cur_count += 1 els...
longest-consecutive-sequence
📌 Python solution keeping track of current max and current count
croatoan
3
113
longest consecutive sequence
128
0.489
Medium
1,461
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2265067/Python-O(n)-Solution-using-Set
class Solution: def longestConsecutive(self, nums: List[int]) -> int: seen = set(nums) longest = 0 for n in seen: if (n-1) not in seen: length = 1 while (n+length) in seen: length += 1 longest = max(leng...
longest-consecutive-sequence
Python O(n) Solution using Set
deucesevenallin
3
204
longest consecutive sequence
128
0.489
Medium
1,462
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2239243/Python-oror-O(n)-oror-set-oror-explanation
class Solution: def longestConsecutive(self, nums: List[int]) -> int: maxLength = 0 s = set(nums) ans=0 n=len(nums) for i in range(len(nums)): # current element is starting point if (nums[i]-1) not in s: ...
longest-consecutive-sequence
Python || O(n) || set || explanation
palashbajpai214
3
176
longest consecutive sequence
128
0.489
Medium
1,463
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2686938/Python-Traversing-every-path-at-most-once
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums_set = set(nums) longest = 0 for num in nums: # If the number before current number not in set # then this path has yet to be traversed if num-1 not in nums_set: ...
longest-consecutive-sequence
[Python] Traversing every path at most once
graceiscoding
2
251
longest consecutive sequence
128
0.489
Medium
1,464
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2326350/Python-O(n)-Time-Space-with-full-working-explanation
class Solution: def longestConsecutive(self, nums: List[int]) -> int: # Time: O(n) and Space: O(n) numSet = set(nums) # will contain all the numbers from the list only once longest = 0 for n in nums: # we will take each element one at a time and check if n-1 not in numSe...
longest-consecutive-sequence
Python O(n) Time Space with full working explanation
DanishKhanbx
2
103
longest consecutive sequence
128
0.489
Medium
1,465
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2242925/Python-or-Java-Shortest-Code-or-O(N)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: s, maxi = set(nums), 0 for i in s: if i + 1 in s: continue count = 1 while i - count in s: count += 1 maxi = max(maxi, count) return maxi
longest-consecutive-sequence
✅ Python | Java Shortest Code | O(N)
dhananjay79
2
115
longest consecutive sequence
128
0.489
Medium
1,466
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1513221/Easy-Python-3-Solution
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums_set = set(nums) length =0 #longest sequence for i in nums: if i-1 not in nums_set: #check if 1 less num present in set currentNum = i currentLen = 1 while (c...
longest-consecutive-sequence
Easy Python 3 Solution
miss_sunshine
2
190
longest consecutive sequence
128
0.489
Medium
1,467
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1339712/GolangPython3-Solution-with-using-hashset
class Solution: def longestConsecutive(self, nums: List[int]) -> int: s = set(nums) d = {} max_seq_len = 0 for num in nums: cur_seq_len = 0 num_cp = num while num_cp in s: cur_seq_len += 1 s.rem...
longest-consecutive-sequence
[Golang/Python3] Solution with using hashset
maosipov11
2
116
longest consecutive sequence
128
0.489
Medium
1,468
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1339712/GolangPython3-Solution-with-using-hashset
class Solution: def longestConsecutive(self, nums: List[int]) -> int: s = set(nums) max_seq_len = 0 for num in nums: if num - 1 in s: continue cur_seq_len = 0 while num in s: cur_seq_len +=...
longest-consecutive-sequence
[Golang/Python3] Solution with using hashset
maosipov11
2
116
longest consecutive sequence
128
0.489
Medium
1,469
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2243002/Python-oror-O(n)-oror-Optimal-solution-oror-Removing-from-set
class Solution: def longestConsecutive(self, nums: List[int]) -> int: numset = set(nums) ans = 0 while not len(numset) == 0: # pick 'random' element from set el = numset.pop() numberOfConsecutiveElements = 1 # find neighbors b...
longest-consecutive-sequence
Python || O(n) || Optimal solution || Removing from set
xfiderek
1
30
longest consecutive sequence
128
0.489
Medium
1,470
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2242014/python3-or-easy-to-understand-or-clear-explanation-or-O(n)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: ans = 0 # dictionary declaration dictionary = {} # adding nums in dictionary to check as they are visited or not for num in nums: dictionary[num]=1 ...
longest-consecutive-sequence
python3 | easy to understand | clear explanation | O(n)
H-R-S
1
25
longest consecutive sequence
128
0.489
Medium
1,471
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2238773/Python-using-set.-Time%3A-O(N).-Space%3A-O(N)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums = set(nums) result = 0 while nums: n = nums.pop() count = 1 m = n while m + 1 in nums: nums.remove(m + 1) ...
longest-consecutive-sequence
Python, using set. Time: O(N). Space: O(N)
blue_sky5
1
45
longest consecutive sequence
128
0.489
Medium
1,472
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1932044/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: return 0 count = 0 res = 0 a = sorted(set(nums)) for i in range(0,len(a)-1): if a[i]+1 == a[i+1]: count+=1 print(count) res = max(cou...
longest-consecutive-sequence
Python (Simple Approach and Beginner-Friendly)
vishvavariya
1
83
longest consecutive sequence
128
0.489
Medium
1,473
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1637476/Easy-Python-Solution(99.98)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums=list(set(nums)) nums.sort() c=1 a=[0] for i in range(len(nums)-1): if nums[i]+1==nums[i+1]: c+=1 else: a.append(c) c=1 re...
longest-consecutive-sequence
Easy Python Solution(99.98%)
Sneh17029
1
412
longest consecutive sequence
128
0.489
Medium
1,474
https://leetcode.com/problems/longest-consecutive-sequence/discuss/1401188/Python-oror-O(2n)-or-easy-or-SR
class Solution: def longestConsecutive(self, nums: List[int]) -> int: """ main idea is to start from lowest number O(2n) """ #we first preprocess such that no duplicate elements are present HashSet = set(nums) max_streak = 0 for number in list(...
longest-consecutive-sequence
Python || O(2n) | easy | SR
sathwickreddy
1
218
longest consecutive sequence
128
0.489
Medium
1,475
https://leetcode.com/problems/longest-consecutive-sequence/discuss/705694/Python3-two-O(N)-approaches
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums = set(nums) ans = 0 for x in nums: if x-1 not in nums: xx = x + 1 while xx in nums: xx += 1 ans = max(ans, xx-x) return ans
longest-consecutive-sequence
[Python3] two O(N) approaches
ye15
1
176
longest consecutive sequence
128
0.489
Medium
1,476
https://leetcode.com/problems/longest-consecutive-sequence/discuss/705694/Python3-two-O(N)-approaches
class Solution: def longestConsecutive(self, nums: List[int]) -> int: lcs = dict() for x in nums: if x not in lcs: lcs[x] = lcs[x + lcs.get(x+1, 0)] = lcs[x-lcs.get(x-1, 0)] = 1 + lcs.get(x+1, 0) + lcs.get(x-1, 0) return max(lcs.values(), default=0)
longest-consecutive-sequence
[Python3] two O(N) approaches
ye15
1
176
longest consecutive sequence
128
0.489
Medium
1,477
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2843560/Python-Easy-Solution-90
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if len(nums) == 0: return 0 l = list(set(nums)) s = sorted(l) count = 1 res = [] for i in range(len(s)-1): if (s[i] + 1) == s[i+1]: count +=1 ...
longest-consecutive-sequence
Python Easy Solution 90%
Jashan6
0
3
longest consecutive sequence
128
0.489
Medium
1,478
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2842614/Simple-python-solution-with-O(n)-TC%3A-80.17
class Solution: def longestConsecutive(self, nums: List[int]) -> int: set_a = set(nums) res = 0 for i in set_a: if i-1 in set_a: continue temp = 0 while i in set_a: temp += 1 i += 1 if temp > res: ...
longest-consecutive-sequence
😎 Simple python solution with O(n) TC: 80.17%
Pragadeeshwaran_Pasupathi
0
4
longest consecutive sequence
128
0.489
Medium
1,479
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2840547/Python-or-Number-LineIntuitionSet-or-Comments
class Solution: def longestConsecutive(self, nums: List[int]) -> int: longest = 0 nums = set(nums) # convert to have a faster lookup time for num in nums: if num - 1 not in nums: # if left number not in nums, it's a min value current = 0 while num ...
longest-consecutive-sequence
🎉Python | Number Line/Intuition/Set | Comments
Arellano-Jann
0
4
longest consecutive sequence
128
0.489
Medium
1,480
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2834546/Another-approach-using-set.-O(n)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums_set = set(nums) longest = 0 while True: if len(nums_set) == 0: break num = nums_set.pop() temp_longest = 1 prev = num - 1 while prev in nums...
longest-consecutive-sequence
Another approach using set. O(n)
monkecoder
0
5
longest consecutive sequence
128
0.489
Medium
1,481
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2822805/Intuitive-solution-using-Linked-Lists
class LinkedList: def __init__(self, v) -> None: self.val = v self.prev = None self.next = None class Solution: def longestConsecutive(self, nums: List[int]) -> int: # we can just sort in nlogn then it is trivial. note order here does not matter. # we can make a linke...
longest-consecutive-sequence
Intuitive solution using Linked Lists
johnsmith999
0
2
longest consecutive sequence
128
0.489
Medium
1,482
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2805853/Python-easy-to-understand-long-solution-broken-down
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums.sort() if len(nums)>=1: count = 1 else: return 0 count, count1 = 1,1 print((nums)) for i in range(0,len(nums)-1): if (nums[i+1]-nums[i])==1: ...
longest-consecutive-sequence
Python easy to understand long solution broken down
mikeyone
0
4
longest consecutive sequence
128
0.489
Medium
1,483
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2803642/Easy-Solution
class Solution: def longestConsecutive(self, nums: List[int]) -> int: numSet = set(nums) longest = 0 for n in nums: #check if its the start of a sequence if (n - 1) not in numSet: length = 0 while (n + length) in numSet: ...
longest-consecutive-sequence
Easy Solution
swaruptech
0
6
longest consecutive sequence
128
0.489
Medium
1,484
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2772710/Simple-Python3-implementation.
class Solution: def longestConsecutive(self, nums: List[int]) -> int: max_depth = 0 queue = set(nums) # set is a hash table, looking is O(1) while len(queue) > 0: depth = self.dfs(queue, queue.pop()) if depth > max_depth: max_depth = depth retu...
longest-consecutive-sequence
Simple Python3 implementation.
dreyceyalbin
0
3
longest consecutive sequence
128
0.489
Medium
1,485
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2750869/Time-complexity-of-%22checking-if-element-in-set%22-is-Average%3A-O(1)-Worst%3A-O(n).
class Solution: def longestConsecutive(self, nums: List[int]) -> int: """TC: O(n), SC: O(n)""" nums = set(nums) # make it faster in following processes longest = 0 for elem in nums: if elem-1 not in nums: # try to find smaller one start_at = elem ...
longest-consecutive-sequence
Time complexity of "checking if element in set" is Average: O(1), Worst: O(n).
woora3
0
4
longest consecutive sequence
128
0.489
Medium
1,486
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2747500/Python-fast-straightforward-solution
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums.sort() mx = 0 c = 0 for i in range(len(nums)-1): if nums[i]+1 == nums[i+1]: c += 1 print(c) if nums[i] == nums[i+1]: continue ...
longest-consecutive-sequence
Python fast straightforward solution
asamarka
0
5
longest consecutive sequence
128
0.489
Medium
1,487
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2746643/Easy-and-simple-Set-solution-or-Python-or-Beats-90-in-speed
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if len(nums)==0:return 0; x=list(set(nums)) ans=1;fans=0 x.sort() for i in range(1,len(x)): if x[i]-x[i-1]==1:ans+=1 else:fans=max(ans,fans);ans=1 ans=max(ans,fans) r...
longest-consecutive-sequence
Easy and simple Set solution | Python | Beats 90% in speed
kamtendra
0
2
longest consecutive sequence
128
0.489
Medium
1,488
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2744497/Beat-97.-2-solutions.-O(n)-and-O(nlogn)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: return 0 nums.sort() longestStreak = 1 currentStreak = 1 for i in range(1, len(nums)): if nums[i] != nums[i-1]: if nums[i] == nums[i-1] + 1: ...
longest-consecutive-sequence
Beat 97%. 2 solutions. O(n) & O(nlogn)
mdfaisalabdullah
0
5
longest consecutive sequence
128
0.489
Medium
1,489
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2744497/Beat-97.-2-solutions.-O(n)-and-O(nlogn)
class Solution: def longestConsecutive(self, nums: List[int]) -> int: longest_streak = 0 num_set = set(nums) for num in num_set: if num - 1 not in num_set: current_num = num current_streak = 1 while current_num + 1 in num_set: ...
longest-consecutive-sequence
Beat 97%. 2 solutions. O(n) & O(nlogn)
mdfaisalabdullah
0
5
longest consecutive sequence
128
0.489
Medium
1,490
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2740718/Simple-Passing-Solution-using-a-sorted-set
class Solution: def longestConsecutive(self, nums: List[int]) -> int: elems = sorted(list(set(nums))) seq = 1 left = 0 right = 1 max_seq = seq N = len(elems) if N <= 1: return N while right < N: if elems[left] == elems[righ...
longest-consecutive-sequence
Simple Passing Solution using a sorted set
koff82
0
1
longest consecutive sequence
128
0.489
Medium
1,491
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2738781/Python-easy-understanding-solution
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: return 0 holder = {} nums = list(set(nums)) for i in nums: left = right = 0 if holder.get(i-1): left = holder[i-1] if holder.get(i+1): ...
longest-consecutive-sequence
Python easy understanding solution
shotgunner
0
4
longest consecutive sequence
128
0.489
Medium
1,492
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2730621/Python3-Weighted-Quick-Union-with-Path-Compression
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: return 0 uf = {x:x for x in nums} counts = {x:1 for x in nums} def find(id): while id != uf[id]: uf[id] = uf[uf[id]] id = uf[id] ...
longest-consecutive-sequence
Python3 Weighted Quick Union with Path Compression
Mbarberry
0
6
longest consecutive sequence
128
0.489
Medium
1,493
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2723036/Python3-code-or-Simple-Solution-or-95-memory-space-and-55-faster
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not(nums): return 0 if len(nums)==1: return 1 maxCount=1 cnt=1 nums.sort() for x in range(len(nums)-1): if nums[x+1]==(nums[x]+1): cnt+=1 ...
longest-consecutive-sequence
Python3 code | Simple Solution | 95% memory space and 55% faster
raviacts035
0
1
longest consecutive sequence
128
0.489
Medium
1,494
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2720453/python-easy-solution-faster
class Solution: def longestConsecutive(self, nums: List[int]) -> int: numset=set(nums) lseq=0 for i in nums: if(i-1 not in numset): longest=0 while(i+longest in numset): longest+=1 lseq=max(longest,lseq) ...
longest-consecutive-sequence
python easy solution faster
Raghunath_Reddy
0
7
longest consecutive sequence
128
0.489
Medium
1,495
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2702002/A-naive-solution
class Solution: def longestConsecutive(self, nums: List[int]) -> int: cnt, max = 0,0 arr = sorted(set(nums)) res = [0] if len(arr): for i in range(len(arr)-1): if arr[i+1]-arr[i] == 1: cnt+=1 if cnt>max: ...
longest-consecutive-sequence
A naive solution
AliAlthiab
0
2
longest consecutive sequence
128
0.489
Medium
1,496
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2694954/O(n)-time-easy-solution
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums = set(nums) longest = 0 for n in nums: if n-1 not in nums: length = 0 while n + length in nums: length += 1 longest = max(longest, length...
longest-consecutive-sequence
O(n) time easy solution
zananpech9
0
4
longest consecutive sequence
128
0.489
Medium
1,497
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2671769/Python-faster-than-98
class Solution: def longestConsecutive(self, nums: List[int]) -> int: nums_set = set(nums) seen = set() longest = 0 for num in nums: if num in seen: continue target = num+1 cur_len = 1 while target in nums_set: ...
longest-consecutive-sequence
Python faster than 98%
cprachaseree
0
2
longest consecutive sequence
128
0.489
Medium
1,498
https://leetcode.com/problems/longest-consecutive-sequence/discuss/2626274/Python3-very-simple-solution-using-python-dictionary
class Solution: def longestConsecutive(self, nums: List[int]) -> int: if 0 <= len(nums) < 2: return len(nums) longest = 0 d = {} for num in nums: if num not in d: left = d.get(num - 1, 0) right = d.get(num + 1, 0) ...
longest-consecutive-sequence
Python3 very simple solution using python dictionary
miko_ab
0
33
longest consecutive sequence
128
0.489
Medium
1,499