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/flatten-a-multilevel-doubly-linked-list/discuss/652275/Python3-recursive-and-iterative-solution | class Solution:
def flatten(self, head: 'Node') -> 'Node':
def fn(node, default=None):
"""Return head of (recursively) flattened linked list"""
if not node: return default
node.next = fn(node.child, fn(node.next, default))
if node.next: node.next.pre... | flatten-a-multilevel-doubly-linked-list | [Python3] recursive & iterative solution | ye15 | 3 | 118 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,600 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/652275/Python3-recursive-and-iterative-solution | class Solution:
def flatten(self, head: 'Node') -> 'Node':
prev = None
stack = [head]
while stack:
node = stack.pop()
if node:
node.prev = prev
if prev: prev.next = node
prev = node
stack.appen... | flatten-a-multilevel-doubly-linked-list | [Python3] recursive & iterative solution | ye15 | 3 | 118 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,601 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/652275/Python3-recursive-and-iterative-solution | class Solution:
def flatten(self, head: 'Node') -> 'Node':
node = head
stack = []
while node:
if node.child:
if node.next: stack.append(node.next)
node.next = node.child
node.next.prev = node
node.child = None
... | flatten-a-multilevel-doubly-linked-list | [Python3] recursive & iterative solution | ye15 | 3 | 118 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,602 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2017920/Python-Intuitive-Recursive-Approach-with-Detailed-Explanation | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return
def dfs(node):
if node.child: #if node has a child, we go down
temp = node.next # store the next node to temp
node.next = node.child # ... | flatten-a-multilevel-doubly-linked-list | Python Intuitive Recursive Approach with Detailed Explanation | lukefall425 | 2 | 67 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,603 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2552169/Python-runtime-O(n)-memory-O(n)-(92.89) | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
cur = head
while cur:
if cur.child == None:
cur = cur.next
else:
subHead = self.flatten(cur.child)
nextNode = cur.next
subHead.prev =... | flatten-a-multilevel-doubly-linked-list | Python, runtime O(n) memory O(n) (92.89%) | tsai00150 | 1 | 126 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,604 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2552169/Python-runtime-O(n)-memory-O(n)-(92.89) | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
cur = head
nextNode = []
pre = None
while cur or nextNode:
if cur:
if cur.child == None:
pre = cur
... | flatten-a-multilevel-doubly-linked-list | Python, runtime O(n) memory O(n) (92.89%) | tsai00150 | 1 | 126 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,605 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1719944/O(N)-time-O(1)-space-Python-recursive-solution | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return
traverse = head
while traverse:
if traverse.child: # if there is child, recursively call flatten() on child
child_linked_list = self.flatten(tra... | flatten-a-multilevel-doubly-linked-list | O(N) time, O(1) space Python recursive solution | winnietheflu | 1 | 52 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,606 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1551313/DFS-with-recursion-or-O(n)-O(n) | class Solution(object):
def flatten(self, head):
"""
:type head: Node
:rtype: Node
"""
nodes = []
def dfs(node):
nodes.append(node)
if node.child:
dfs(node.child)
if node.next:
dfs(node.next)
... | flatten-a-multilevel-doubly-linked-list | DFS with recursion | O(n), O(n) | nomofika | 1 | 113 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,607 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2713706/Easiest-Method-or-Beginner's-Friendly-or-Python | class Solution(object):
def flatten(self, head):
if not(head): return head
def rec(values, head):
while head:
values.append(head.val)
if head.child is not None:
rec(values, head.child)
head = head.next
... | flatten-a-multilevel-doubly-linked-list | Easiest Method | Beginner's Friendly | Python | its_krish_here | 0 | 17 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,608 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2429564/16-lines-of-python-code-greater-94-faster-%3A) | class Solution(object):
def flatten(self, head):
self.end = Node(-1) # used a global val for each recursion so that i can have the last node in each level
str1 = head
while str1:
next1 = str1.next
if str1.child != None: #checking if the the node has a chi... | flatten-a-multilevel-doubly-linked-list | 16 lines of python code => 94% faster :) | sudharshan1706 | 0 | 37 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,609 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2320700/Help-List-Not-Valid | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
current = head
tail = []
children = 0
prev = None
while current or children:
if not current and children:
child_tail = tail.pop()
child_tail.prev = prev
... | flatten-a-multilevel-doubly-linked-list | Help - List Not Valid | fissioner | 0 | 17 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,610 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2148873/Python-O(N)-Solution-Using-Stack | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
stack = []
curr = head
prev = None
while curr:
curr.prev = prev
if curr.next:
stack.append(curr.next)
if curr.child:
stack.appen... | flatten-a-multilevel-doubly-linked-list | Python O(N) Solution Using Stack | bretticus-mc | 0 | 34 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,611 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/2029186/Python-DFS-simple-solution | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head: return None
ans = []
self.dfs(head, ans)
for i in range(len(ans)-1): # Build the new linked list
ans[i].next = ans[i+1]
ans[i].child = None
ans[i+1].prev = ... | flatten-a-multilevel-doubly-linked-list | Python DFS simple solution | FlorinnC1 | 0 | 63 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,612 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1984213/Python-oror-Intuitive-Iterative-Solution | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
prev = Node(0)
prev.next = head
curr = head
while curr:
if curr.child:
curr.child.prev = curr
temp = curr.next
curr.next = curr.child
child = curr.child
while child.next:
child = child.next
child.nex... | flatten-a-multilevel-doubly-linked-list | Python || Intuitive Iterative Solution | morpheusdurden | 0 | 51 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,613 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1897624/Python-straight-forward-solution-with-a-few-comment | class Solution:
def flatten(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
def get_head_and_tail(node): # Once there exist a child, we adjust node.next to child_head,
head = node # and child_tail.next to node... | flatten-a-multilevel-doubly-linked-list | Python straight - forward solution with a few comment | byroncharly3 | 0 | 59 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,614 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1551536/Python-3-oror-Stack-oror-Easy-Understanding | class Solution:
def flatten(self, head: 'Node') -> 'Node':
if not head:
return head
stack = []
temp = head
while True:
if not temp:
break
if not temp.next:
if stack:
node = stack.pop()
... | flatten-a-multilevel-doubly-linked-list | Python 3 || Stack || Easy Understanding | Sheersendu | 0 | 50 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,615 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1551436/Python3-solution | class Solution:
def flatten(self, head: 'Node') -> 'Node':
if not head: return head
def dfs(node):
visited.append(node)
if node.child and node.child not in visited:
dfs(node.child)
if node.next and node.next not in visited:
dfs(nod... | flatten-a-multilevel-doubly-linked-list | Python3 solution | dalechoi | 0 | 17 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,616 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1551151/Python-Two-version-of-solution-with-using-stack | class Solution:
def flatten(self, head: 'Node') -> 'Node':
if not head:
return None
dummy = Node(0, None, None, None)
stack = [head]
prev = dummy
while stack:
cur = stack.pop()
cur.prev = prev
prev.next = cur
... | flatten-a-multilevel-doubly-linked-list | [Python] Two version of solution with using stack | maosipov11 | 0 | 9 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,617 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1551151/Python-Two-version-of-solution-with-using-stack | class Solution:
def flatten(self, head: 'Node') -> 'Node':
dummy = Node(0, None, head, None)
stack = []
prev = None
while head or stack:
if not head and stack:
_next = stack.pop()
prev.next = _next
... | flatten-a-multilevel-doubly-linked-list | [Python] Two version of solution with using stack | maosipov11 | 0 | 9 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,618 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1056478/Python-DFS-stack-construct-DLL-from-list-(28ms) | class Solution:
def flatten(self, head: 'Node') -> 'Node':
if not head: return head
d = []
stack = [head]
while stack:
n = stack.pop()
d.append(n.val)
if n.next:
stack.append(n.next)
if n.child:... | flatten-a-multilevel-doubly-linked-list | Python - DFS stack - construct DLL from list (28ms) | kysr007 | 0 | 95 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,619 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/537234/Python3-simple-solution | class Solution:
def flatten(self, head: 'Node') -> 'Node':
self.walk(head)
return head
def walk(self, node: 'Node') -> 'Node':
if node is None:
return None
if node.child is not None:
child_tail = self.walk(node.child)
child_tail.next =... | flatten-a-multilevel-doubly-linked-list | Python3 simple solution | tjucoder | 0 | 54 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,620 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/469610/python-recursive-dfs | class Solution:
def flatten(self, head: 'Node') -> 'Node':
if head is None:
return None
return self.dfs(head)
def dfs(self, node):
original_next = node.next
tail = node
if node.child:
node.next = self.dfs(node.child)
node.next.prev =... | flatten-a-multilevel-doubly-linked-list | python recursive dfs | anonymouscoder1555 | 0 | 33 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,621 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769493/SIMPLE-PYTHON-SOLUTION-USING-BFS | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
dic=defaultdict(lambda :0)
lst=[[start,0]]
dic[start]=1
while lst:
x,d=lst.pop(0)
if x==end:
return d
for i in range(len(bank)):
ct... | minimum-genetic-mutation | SIMPLE PYTHON SOLUTION USING BFS | beneath_ocean | 2 | 148 | minimum genetic mutation | 433 | 0.52 | Medium | 7,622 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2097023/Python-3.10%3A-DFS-BFS.-VERY-SHORT. | class Solution:
def minMutation(self, start: str, end: str, bank: list[str]) -> int:
bank = set(bank) | {start}
def dfs(st0, cnt):
if st0 == end:
return cnt
bank.remove(st0)
for i, ch0 in enumerate(st0):
for ch1 in "ACGT":
... | minimum-genetic-mutation | Python 3.10: DFS, BFS. VERY SHORT. | miguel_v | 2 | 156 | minimum genetic mutation | 433 | 0.52 | Medium | 7,623 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2097023/Python-3.10%3A-DFS-BFS.-VERY-SHORT. | class Solution:
def minMutation(self, start: str, end: str, bank: list[str]) -> int:
bank = set(bank)
dq = deque([(start, 0)])
while dq:
st0, cnt = dq.popleft()
if st0 == end:
return cnt
for i, ch0 in enumerate(st0):
for c... | minimum-genetic-mutation | Python 3.10: DFS, BFS. VERY SHORT. | miguel_v | 2 | 156 | minimum genetic mutation | 433 | 0.52 | Medium | 7,624 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/1511660/Well-Coded-oror-Easy-to-understand-oror-91-faster-oror-Beginners | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
if end not in bank:
return -1
q = deque()
q.append((start,0))
while q:
tochk,limit = q.popleft()
if tochk == end:
return limit
w = 0
while w<len(ban... | minimum-genetic-mutation | 📌📌 Well-Coded || Easy-to-understand || 91% faster || Beginners 🐍 | abhi9Rai | 2 | 224 | minimum genetic mutation | 433 | 0.52 | Medium | 7,625 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2768754/Python3-Simple-BFS-for-beginners-with-explanation-step-by-step | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
bank = set(bank)
if end not in bank: return -1
queue = deque()
queue.append((start,0))
while queue:
word,steps = queue.popleft()
if word == end: return steps
... | minimum-genetic-mutation | [Python3] Simple BFS for beginners with explanation step by step | shriyansnaik | 1 | 50 | minimum genetic mutation | 433 | 0.52 | Medium | 7,626 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/1184400/Python-Get's-the-job-done | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
def is_mutation(s, e):
s = collections.Counter([(i, c) for i, c in enumerate(s)])
e = collections.Counter([(i, c) for i, c in enumerate(e)])
return sum(((s |... | minimum-genetic-mutation | Python, Get's the job done | dev-josh | 1 | 214 | minimum genetic mutation | 433 | 0.52 | Medium | 7,627 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/1184400/Python-Get's-the-job-done | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
graph = {s : collections.Counter([(i, c) for i, c in enumerate(s)]) for s in [start, end] + bank}
mutation = lambda s, e: sum(((graph[s] | graph[e]) - (graph[s] & grap... | minimum-genetic-mutation | Python, Get's the job done | dev-josh | 1 | 214 | minimum genetic mutation | 433 | 0.52 | Medium | 7,628 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/704135/Python3-bi-directional-bfs-(97.38) | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
graph = {}
for gene in bank:
for i in range(8):
graph.setdefault(gene[:i] + "*" + gene[i+1:], []).append(gene)
ans = 0 # count of mutations
fwd, bwd = ... | minimum-genetic-mutation | [Python3] bi-directional bfs (97.38%) | ye15 | 1 | 165 | minimum genetic mutation | 433 | 0.52 | Medium | 7,629 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2797529/BFS-Search-with-Tuples! | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
def canMutate(gene1, gene2):
mutationsNeeded = 0
for i in range(len(gene1)):
if gene1[i] != gene2[i]:
mutationsNeeded += 1
return mutationsNeeded
... | minimum-genetic-mutation | BFS Search with Tuples! | mephiticfire | 0 | 2 | minimum genetic mutation | 433 | 0.52 | Medium | 7,630 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2784832/python-solution-with-comments | class Solution:
def minMutation(self, startGene: str, endGene: str, bank: List[str]) -> int:
#searching in set is faster than in array
bank = set(bank)
#since each valid gene should be in bank
#so if endgene is not in the bank hence we cannot acheive it ever
if endGene not i... | minimum-genetic-mutation | python solution with comments | user9781TM | 0 | 2 | minimum genetic mutation | 433 | 0.52 | Medium | 7,631 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2771640/Python3-or-BFS-with-bitmasks | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
def distanceOne(gene1: str, gene2: str) -> bool:
differenceFound = False
for char1, char2 in zip(gene1, gene2):
if char1 != char2:
if not differenceFound:
... | minimum-genetic-mutation | Python3 | BFS with bitmasks | sr_vrd | 0 | 3 | minimum genetic mutation | 433 | 0.52 | Medium | 7,632 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2771631/Python-DFS-Solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
if end not in bank:
return 0 if (start == end) else -1
def difference(A, B):
c = 0
for i in range(len(A)):
if A[i] != B[i]:
c+=1
r... | minimum-genetic-mutation | Python DFS Solution | dyxuki | 0 | 3 | minimum genetic mutation | 433 | 0.52 | Medium | 7,633 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2771184/Python-BFS-Solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
if start not in bank:
bank.append(start)
queue = [(start, 0)]
visited = set()
while len(queue) > 0:
curGene, mutation = queue.pop(0)
if curGene not in vis... | minimum-genetic-mutation | Python BFS Solution | gequalspisquared | 0 | 25 | minimum genetic mutation | 433 | 0.52 | Medium | 7,634 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2770773/Pyhton-BFS | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
n = len(start)
q = deque([(start, 0)])
visited = set([start])
while q:
node, dist = q.popleft()
if node == end:
return dist
for i in r... | minimum-genetic-mutation | Pyhton, BFS | blue_sky5 | 0 | 6 | minimum genetic mutation | 433 | 0.52 | Medium | 7,635 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2770549/BFS-Fast-and-Easy-Solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
bank = set(bank)
if end not in bank:
return -1
q = deque([(start, 0)])
visited = set(start)
while q:
gene, no_of_mutation = q.popleft()
if gene == end:
... | minimum-genetic-mutation | BFS - Fast and Easy Solution | user6770yv | 0 | 4 | minimum genetic mutation | 433 | 0.52 | Medium | 7,636 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2770446/Python-DFS | class Solution(object):
def dfs(self, current, end, mutations, remaining_bank):
if current == end:
return mutations
if len(remaining_bank) == 0:
return -1
mutation_list = []
for gene in remaining_bank:
genes_separated = s... | minimum-genetic-mutation | Python DFS | fhormel | 0 | 18 | minimum genetic mutation | 433 | 0.52 | Medium | 7,637 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2770377/Python-(Faster-than-96)-or-BFS-solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
count = 0
visited = set()
visited.add(start)
queue = deque()
queue.append(start)
while queue:
for _ in range(len(queue)):
word = queue.popleft()
... | minimum-genetic-mutation | Python (Faster than 96%) | BFS solution | KevinJM17 | 0 | 6 | minimum genetic mutation | 433 | 0.52 | Medium | 7,638 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2770182/USEPYTHON | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
def checkNeighbor(a,b):
return sum([1 for i in range(len(a)) if a[i]!=b[i]]) == 1
q = deque([start])
visited = {start}
### use extra variable to store mutations
mutation... | minimum-genetic-mutation | USEPYTHON | iamdiinesh | 0 | 5 | minimum genetic mutation | 433 | 0.52 | Medium | 7,639 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2770019/Deque-oror-Python3 | class Solution(object):
def minMutation(self, start, end, bank):
bfsQueue = deque([(start, 0)])
validGene = set(bank)
while bfsQueue:
curGene, curStep = bfsQueue.popleft()
if curGene == end:
return curStep
for... | minimum-genetic-mutation | Deque || Python3 | joshua_mur | 0 | 3 | minimum genetic mutation | 433 | 0.52 | Medium | 7,640 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769697/Python-or-Simple-and-fast-BFS-solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
st = defaultdict(set)
def calc_dist(a, b):
count = 0
for i in range(len(a)):
if a[i] != b[i]:
count += 1
return count
... | minimum-genetic-mutation | Python | Simple and fast BFS solution | LordVader1 | 0 | 20 | minimum genetic mutation | 433 | 0.52 | Medium | 7,641 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769562/python-solution-or-BFS | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
def CountDiff(gene1, gene2):
SumOfDiff = 0
for i in range(8):
if gene1[i] != gene2[i]:
SumOfDiff += 1
return SumOfDiff
... | minimum-genetic-mutation | python solution | BFS | maomao1010 | 0 | 14 | minimum genetic mutation | 433 | 0.52 | Medium | 7,642 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769470/python-straight-foward-solution | class Solution:
def dif_check(self, s1,s2):
return sum([1 if s1[i] != s2[i] else 0 for i in range(len(s1))])
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
bank = set(bank + [start])
visited = set()
q = collections.deque([start])
nodes = collect... | minimum-genetic-mutation | python straight-foward solution | gkpani97 | 0 | 14 | minimum genetic mutation | 433 | 0.52 | Medium | 7,643 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769340/Python3-or-DFS-solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
bank = [start]+bank
n = len(bank)
adjList = defaultdict(list)
for i in range(n):
for j in range(i+1,n):
counter = 0
for k in range(len(start)):
... | minimum-genetic-mutation | Python3 | DFS solution | ty2134029 | 0 | 4 | minimum genetic mutation | 433 | 0.52 | Medium | 7,644 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769204/Python-oror-Easy-Solution-oror-All-Outputs | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
queue = [[start, 0]]
ans = float('inf')
dp = {}
while queue:
p = queue.pop(0)
start = p[0]
if start not in dp:
dp[start] = True
i... | minimum-genetic-mutation | Python || Easy Solution || All Outputs | Rahul_Kantwa | 0 | 5 | minimum genetic mutation | 433 | 0.52 | Medium | 7,645 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769177/py3-code | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
# [1] initialize queue with starting gene
q, bank = collections.deque([(start,0)]), set(bank)
while q:
g, m = q.popleft()
if g == end : return m
# [... | minimum-genetic-mutation | py3 code | rupamkarmakarcr7 | 0 | 5 | minimum genetic mutation | 433 | 0.52 | Medium | 7,646 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2769060/python3-solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
def checkNeighbor(a,b):
return sum([1 for i in range(len(a)) if a[i]!=b[i]]) == 1
q = deque([start])
### initialize the empty visited set
visited = set()
mutations = 0
... | minimum-genetic-mutation | python3 solution | avs-abhishek123 | 0 | 7 | minimum genetic mutation | 433 | 0.52 | Medium | 7,647 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2768777/Python3-Convert-States-To-Numbers | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
v = {"ACGT"[i]: i for i in range(4)}
c = lambda x: sum([v[x[i]] * (4**i) for i in range(len(x))])
d = {c(x): inf for x in bank}
st = c(start)
en = c(end)
if en not in d:
... | minimum-genetic-mutation | Python3 Convert States To Numbers | godshiva | 0 | 5 | minimum genetic mutation | 433 | 0.52 | Medium | 7,648 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2768716/python3-graph-sol-for-reference | class Solution:
def oneCharDiff(self, a, b):
diffcnt = 0
for i in range(len(a)):
if a[i] != b[i]:
diffcnt += 1
return diffcnt == 1
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
nb = len(bank)
graph = defaultdict(list)
... | minimum-genetic-mutation | [python3] graph sol for reference | vadhri_venkat | 0 | 4 | minimum genetic mutation | 433 | 0.52 | Medium | 7,649 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2768694/python-solution | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
queue = []
queue.append((start, 0))
availableSequences = set(bank)
while queue:
sequence, steps = queue.pop(0)
if sequence == end:
return steps
... | minimum-genetic-mutation | python solution | MuraliChrishna | 0 | 16 | minimum genetic mutation | 433 | 0.52 | Medium | 7,650 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2238205/Python3-Solution-with-using-bfs | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
q = collections.deque()
q.append((start, 0))
bank = set(bank)
while q:
cur_mut, step = q.popleft()
if cur_mut == end:
return step
... | minimum-genetic-mutation | [Python3] Solution with using bfs | maosipov11 | 0 | 35 | minimum genetic mutation | 433 | 0.52 | Medium | 7,651 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/2035074/Python-easy-to-read-and-understand-or-BFS | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
if end not in bank:
return -1
visit = set()
visit.add(start)
q = [start]
steps = 0
bank.append(start)
choice = ['A', 'C', 'G', 'T']
while q:
n... | minimum-genetic-mutation | Python easy to read and understand | BFS | sanial2001 | 0 | 71 | minimum genetic mutation | 433 | 0.52 | Medium | 7,652 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/1692825/WEEB-DOES-PYTHON-BFS | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
bank = set(bank)
if end not in bank: return -1
queue = deque([(start, 0)])
while queue:
curStr, steps = queue.popleft()
if curStr == end:
return steps
for i in range(len(curStr)):
for char in ["A", "C", "... | minimum-genetic-mutation | WEEB DOES PYTHON BFS | Skywalker5423 | 0 | 71 | minimum genetic mutation | 433 | 0.52 | Medium | 7,653 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/1491211/Python-or-BFS | class Solution:
def minMutation(self, start: str, end: str, bank) -> int:
if end not in bank:
return -1
q=[(start,0)]
visit=[start]
while q:
gene,d=q.pop(0)
if gene==end:
return d
for g in bank:
if g not ... | minimum-genetic-mutation | Python | BFS | heckt27 | 0 | 55 | minimum genetic mutation | 433 | 0.52 | Medium | 7,654 |
https://leetcode.com/problems/minimum-genetic-mutation/discuss/1383864/Python-Solution-using-Recursion | class Solution:
def minMutation(self, start: str, end: str, bank: List[str]) -> int:
res = []
def is_valid_mutate(a, b):
return sum(1 if a[i] != b[i] else 0 for i in range(len(a))) == 1
def helper(start, bank, count, res):
if not bank:
return
... | minimum-genetic-mutation | Python Solution using Recursion | zhouquan0x16 | 0 | 98 | minimum genetic mutation | 433 | 0.52 | Medium | 7,655 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1015806/One-Line-python-Solution | class Solution:
def countSegments(self, s: str) -> int:
return len([i for i in s.split(" ") if i!=""]) | number-of-segments-in-a-string | One Line python Solution | moazmar | 3 | 464 | number of segments in a string | 434 | 0.377 | Easy | 7,656 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2603152/Python-91-Faster-Simple-Solution | class Solution:
def countSegments(self, s: str) -> int:
#create a list based on a space split
slist = list(s.split(" "))
#return the len of list minus empty item
return(len(slist)-slist.count("")) | number-of-segments-in-a-string | Python 91% Faster - Simple Solution | ovidaure | 1 | 86 | number of segments in a string | 434 | 0.377 | Easy | 7,657 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2794202/Simple-solution-for-the-problem | class Solution:
def countSegments(self, s: str) -> int:
if s=="":
return(0)
j = s.split(" ")
print(j)
c=0
for it in j:
if it!="":
c+=1
return(c) | number-of-segments-in-a-string | Simple solution for the problem | harshgupta204016 | 0 | 2 | number of segments in a string | 434 | 0.377 | Easy | 7,658 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2759882/Python-1-line-code | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | Python 1 line code | kumar_anand05 | 0 | 3 | number of segments in a string | 434 | 0.377 | Easy | 7,659 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2741121/Two-Lines-Python-Solution | class Solution:
def countSegments(self, s: str) -> int:
l= s.split()
return len(l) | number-of-segments-in-a-string | Two Lines Python Solution | dnvavinash | 0 | 4 | number of segments in a string | 434 | 0.377 | Easy | 7,660 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2739908/Python-3-Solution | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | Python 3 Solution | mati44 | 0 | 1 | number of segments in a string | 434 | 0.377 | Easy | 7,661 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2675456/Easy-to-Understand-or-Beginner's-Friendly-or-Python | class Solution(object):
def countSegments(self, s):
if not(s): return 0
ans, temp = 0, ''
for ch in s:
if ch == ' ' and temp != '':
ans += 1
temp = ''
if ch != ' ': temp += ch
return ans + 1 if temp != '' else ans | number-of-segments-in-a-string | Easy to Understand | Beginner's Friendly | Python | its_krish_here | 0 | 13 | number of segments in a string | 434 | 0.377 | Easy | 7,662 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2647575/One-line-solution | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | One line solution | shubhamshinde245 | 0 | 2 | number of segments in a string | 434 | 0.377 | Easy | 7,663 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2601057/python-or-without-the-usage-of-split | class Solution:
def countSegments(self, s: str) -> int:
s += " "
count = 0
for i in range(len(s)):
if s[i] == " " and i != 0:
if s[i - 1] != " ":
count += 1
return count | number-of-segments-in-a-string | python | without the usage of split | sproq | 0 | 10 | number of segments in a string | 434 | 0.377 | Easy | 7,664 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2502140/*-Python-oror-One-Liner-* | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | • Python || One Liner • | keertika27 | 0 | 16 | number of segments in a string | 434 | 0.377 | Easy | 7,665 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2371915/1-liner-no-brainer | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | 1 liner no brainer | sunakshi132 | 0 | 19 | number of segments in a string | 434 | 0.377 | Easy | 7,666 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/2145213/Using-.split()-method | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | Using .split() method | thanhinterpol | 0 | 41 | number of segments in a string | 434 | 0.377 | Easy | 7,667 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1914779/Python3-2-Solutions | class Solution:
def countSegments(self, s: str) -> int:
return len([i for i in s.split(' ') if i != '']) | number-of-segments-in-a-string | [Python3] 2 Solutions | abhijeetmallick29 | 0 | 52 | number of segments in a string | 434 | 0.377 | Easy | 7,668 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1914779/Python3-2-Solutions | class Solution:
def countSegments(self, s: str) -> int:
count = 0
for i in range(len(s)):
if (i == 0 or s[i-1] == ' ') and s[i] != ' ':
count += 1
return count; | number-of-segments-in-a-string | [Python3] 2 Solutions | abhijeetmallick29 | 0 | 52 | number of segments in a string | 434 | 0.377 | Easy | 7,669 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1760427/Python-oror-Runtime-faster-than-97.5-oror-Memory-less-than-99 | class Solution:
def countSegments(self, s: str) -> int:
if not s:
return 0
return len(s.split()) | number-of-segments-in-a-string | Python || Runtime faster than 97.5% || Memory less than 99% | Akhilesh_Pothuri | 0 | 31 | number of segments in a string | 434 | 0.377 | Easy | 7,670 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1739701/simple-python-solution | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | simple python solution | vijayvardhan6 | 0 | 33 | number of segments in a string | 434 | 0.377 | Easy | 7,671 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1512440/Python-O(n)-time-O(1)-space-complex-solution-without-strip() | class Solution:
def countSegments(self, s: str) -> int:
n = len(s)
cnt = 0
start = 0
end = n-1
if n == 0:
return 0
while start <= n-1 and s[start] == ' ':
start += 1
if start == n:
ret... | number-of-segments-in-a-string | Python O(n) time, O(1) space complex solution without strip() | byuns9334 | 0 | 86 | number of segments in a string | 434 | 0.377 | Easy | 7,672 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1495185/python-3 | class Solution:
def countSegments(self, s: str) -> int:
count=0
if s=="":
return 0
lst=list(s.split(" "))
print(lst)
for ch in lst:
if ch != '':
count+=1
return count | number-of-segments-in-a-string | python 3 | minato_namikaze | 0 | 60 | number of segments in a string | 434 | 0.377 | Easy | 7,673 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1473062/Python-Stack | class Solution:
def countSegments(self, s: str) -> int:
result, stack = 0, []
for c in s:
if c == ' ':
if stack:
result += 1
stack.clear()
else:
stack += [c]
return ... | number-of-segments-in-a-string | [Python] Stack | dev-josh | 0 | 38 | number of segments in a string | 434 | 0.377 | Easy | 7,674 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1473062/Python-Stack | class Solution:
def countSegments(self, s: str) -> int:
result, seen = 0, False
for c in s:
if c == ' ':
result += int(seen)
seen = False
continue
seen = True
return result + int(seen) | number-of-segments-in-a-string | [Python] Stack | dev-josh | 0 | 38 | number of segments in a string | 434 | 0.377 | Easy | 7,675 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1267642/Easy-Python-Solution(93.08) | class Solution:
def countSegments(self, s: str) -> int:
h=0
c=0
# s=' '+s
if not s:
return 0
for i in range(len(s)):
if(s[i]!=' '):
h+=1
elif(h>0):
c+=1
h=0
if(h>0):
c+=1
... | number-of-segments-in-a-string | Easy Python Solution(93.08%) | Sneh17029 | 0 | 170 | number of segments in a string | 434 | 0.377 | Easy | 7,676 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1262177/Python3-simple-%22one-liner%22-solution | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | Python3 simple "one-liner" solution | EklavyaJoshi | 0 | 41 | number of segments in a string | 434 | 0.377 | Easy | 7,677 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1159186/This-shows-why-Python-is-awesome | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | This shows why Python is awesome | fadista | 0 | 29 | number of segments in a string | 434 | 0.377 | Easy | 7,678 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/336553/Solution-in-Python-3-(beats-~100)-(-one-line-) | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split())
- Python 3
- Junaid Mansuri | number-of-segments-in-a-string | Solution in Python 3 (beats ~100%) ( one line ) | junaidmansuri | 0 | 335 | number of segments in a string | 434 | 0.377 | Easy | 7,679 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1425480/EASY-PYTHON-1-LINE-99 | class Solution:
def countSegments(self, s: str) -> int:
return len(s.split()) | number-of-segments-in-a-string | EASY PYTHON 1 LINE 99% | Umarabdullah101 | -1 | 76 | number of segments in a string | 434 | 0.377 | Easy | 7,680 |
https://leetcode.com/problems/number-of-segments-in-a-string/discuss/1159362/Code-for-Every-Language | class Solution:
def countSegments(self, s: str) -> int:
count = 0
for i in range(len(s)):
if s[i] != " " and (i==0 or s[i-1]== " "):
count+=1
return count | number-of-segments-in-a-string | Code for Every Language | asifrasool573 | -1 | 100 | number of segments in a string | 434 | 0.377 | Easy | 7,681 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1896849/Python-easy-to-read-and-understand-or-sorting | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: x[1])
n = len(intervals)
ans, curr = 1, intervals[0]
for i in range(n):
if intervals[i][0] >= curr[1]:
ans += 1
curr = interva... | non-overlapping-intervals | Python easy to read and understand | sorting | sanial2001 | 2 | 196 | non overlapping intervals | 435 | 0.499 | Medium | 7,682 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/2519472/Clean-8-Lines-Python3-or-99-Time-and-Memory-or-Greedy | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda k: k[1])
removed, last_end = 0, float('-inf')
for start, end in intervals:
if start < last_end:
removed += 1
else:
last_end ... | non-overlapping-intervals | Clean 8 Lines Python3 | 99% Time & Memory | Greedy | ryangrayson | 1 | 71 | non overlapping intervals | 435 | 0.499 | Medium | 7,683 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/2386577/Python-Greedy-Beats-92-with-full-working-explanation | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: # Time: O(nlogn) and Space: O(1)
intervals.sort()
res = 0
prevEnd = intervals[0][1]
for start, end in intervals[1:]: # we will start from 1 as we already had taken 0 as a base value
... | non-overlapping-intervals | Python [Greedy / Beats 92%] with full working explanation | DanishKhanbx | 1 | 106 | non overlapping intervals | 435 | 0.499 | Medium | 7,684 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1832287/Python-Solution | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals = sorted(intervals)
ans = 0
endDate = intervals[0][1]
for currentInterval in range(1, len(intervals)):
if intervals[currentInterval][0] < endDate :
ans +... | non-overlapping-intervals | Python Solution | DietCoke777 | 1 | 64 | non overlapping intervals | 435 | 0.499 | Medium | 7,685 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1570424/Python-Greedy-easy-understanding | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# greedy
# always pick the earlist end time because it provide more capacity to the later intervals
intervals.sort(key = lambda x : x[1])
# use fit to record the order or fitting intervals and compare with the next one
# to ... | non-overlapping-intervals | Python Greedy, easy understanding | Msparks | 1 | 96 | non overlapping intervals | 435 | 0.499 | Medium | 7,686 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1510947/Python3-Solution-with-using-sorting | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals = sorted(intervals, key=lambda interval: interval[1])
last_valid_interval_idx = 0
cnt = 0
for cur_idx in range(1, len(intervals)):
if intervals[cur_idx][0] < in... | non-overlapping-intervals | [Python3] Solution with using sorting | maosipov11 | 1 | 109 | non overlapping intervals | 435 | 0.499 | Medium | 7,687 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1468599/Be-Greedy-Sometime-or-Python | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
n = len(intervals)
if n<=0:
return 0
intervals.sort()
ans = 0
end = intervals[0][1]
print(intervals)
for i in range(1, n):
if intervals[i][0] < end:
... | non-overlapping-intervals | Be Greedy Sometime | Python | Sanjaychandak95 | 1 | 107 | non overlapping intervals | 435 | 0.499 | Medium | 7,688 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/2801061/Greedy-approach-with-Python3 | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
s_int=sorted(intervals, key=lambda s:s[0]*100000+s[1])
new_int=[]
for i in range(len(s_int)):
if len(new_int)>0 and (s_int[i][0]==s_int[i-1][0]):
pass
else:
... | non-overlapping-intervals | Greedy approach with Python3 | guankiro | 0 | 7 | non overlapping intervals | 435 | 0.499 | Medium | 7,689 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/2673676/Python-Easy-Solution | class Solution:
def eraseOverlapIntervals(self, arr: List[List[int]]) -> int:
arr.sort(key=lambda x:x[1])
print(arr)
count=1
prevS , prevE=arr[0]
for s ,e in arr:
if s<prevE:
continue
else:
prevS , prevE = s , e
... | non-overlapping-intervals | Python Easy Solution | pranjalmishra334 | 0 | 67 | non overlapping intervals | 435 | 0.499 | Medium | 7,690 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/2558963/Python-Easy-Solution | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda x : x[1])
res = 0
lastNonOverlap = 0
for i in range(1, len(intervals)):
if intervals[i][0] < intervals[lastNonOverlap][1]:
... | non-overlapping-intervals | Python Easy Solution | lokeshsenthilkumar | 0 | 127 | non overlapping intervals | 435 | 0.499 | Medium | 7,691 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/2438263/short-Python-solution-using-stack | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
st = []
intervals.sort(key= lambda x:x[0])
ans = 0
for inte in intervals:
if not st or inte[0] >= st[-1][1]:
st.append(inte)
continue
i... | non-overlapping-intervals | short Python solution using stack | 96sayak | 0 | 25 | non overlapping intervals | 435 | 0.499 | Medium | 7,692 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1921277/Python-Intervals-solution-explained | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# we require the intervals to be
# in sorted order otherwise the intervals
# would all be scattered around
# for e.g. intervals = [1,2],[3,5],[2,3]
# the third interval should be merged with
... | non-overlapping-intervals | [Python] Intervals solution explained | buccatini | 0 | 78 | non overlapping intervals | 435 | 0.499 | Medium | 7,693 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1573386/Greedy-Approach-oror-Well-Explained-and-Coded-oror-Easy-to-understand | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key = lambda x:x[0])
st = []
for a,b in intervals:
if len(st)!=0:
if a<st[-1]:
st.append(min(st.pop(),b))
else:
st.append(b)
else:
... | non-overlapping-intervals | 📌📌 Greedy Approach || Well-Explained & Coded || Easy to understand 🐍 | abhi9Rai | 0 | 195 | non overlapping intervals | 435 | 0.499 | Medium | 7,694 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1570809/Python-O(nlogn)-time | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort()
res = 0
end = intervals[0][1]
for i in range(1, len(intervals)):
if intervals[i][0] < end:
end = min(end, intervals[i][1])
res += 1
... | non-overlapping-intervals | Python O(nlogn) time | dereky4 | 0 | 287 | non overlapping intervals | 435 | 0.499 | Medium | 7,695 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/983107/Simple-Python-solution | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
curr_interval, count = None, 0
for i in sorted(intervals, key=lambda a: (a[1])):
if not curr_interval or curr_interval[1] <= i[0]:
curr_interval = i
count += 1
retu... | non-overlapping-intervals | Simple Python solution | cj1989 | 0 | 202 | non overlapping intervals | 435 | 0.499 | Medium | 7,696 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/793411/Python3-greedy | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
ans, prev = 0, -inf #end of previous interval
for x, y in sorted(intervals, key=lambda x: x[1]):
if x < prev: ans += 1 #current start is smaller than previous end => overlapping
else: prev = y... | non-overlapping-intervals | [Python3] greedy | ye15 | 0 | 69 | non overlapping intervals | 435 | 0.499 | Medium | 7,697 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/793411/Python3-greedy | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
ans, prev = 0, -inf # previous ending
for x, y in sorted(intervals):
if prev <= x: prev = y
else:
prev = min(prev, y)
ans += 1
return ans | non-overlapping-intervals | [Python3] greedy | ye15 | 0 | 69 | non overlapping intervals | 435 | 0.499 | Medium | 7,698 |
https://leetcode.com/problems/non-overlapping-intervals/discuss/1443051/Simple-Python-O(n)-greedy-solution | class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
# removing minimum number of intervals is equivalent to
# keeping the maximum number of intervals, which can be
# solved greedily. We can sort by finish time and schedule by
# earliest non-conflicting... | non-overlapping-intervals | Simple Python O(n) greedy solution | Charlesl0129 | -3 | 277 | non overlapping intervals | 435 | 0.499 | Medium | 7,699 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.