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/find-n-unique-integers-sum-up-to-zero/discuss/2287459/Python-fastest-solution
class Solution: def sumZero(self, n: int) -> List[int]: if n==1: return [0] if n==2: return[-1,1] x=[] for i in range(n-1): x.append(i) x.append(-sum(x)) return x
find-n-unique-integers-sum-up-to-zero
Python fastest solution
yagnic40
1
57
find n unique integers sum up to zero
1,304
0.771
Easy
19,400
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2005000/easy-solution
class Solution: def sumZero(self, n: int) -> List[int]: a=[] if(n%2==0): pass else: a.append(0) for i in range(1,n//2+1): a.append(i) a.append(-i) return a
find-n-unique-integers-sum-up-to-zero
easy solution
Durgavamsi
1
59
find n unique integers sum up to zero
1,304
0.771
Easy
19,401
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1150132/Python3-Simple-Solution-in-O(n)-runtime-Runtime-beats-93-of-Python3-submissions
class Solution: def sumZero(self, n: int) -> List[int]: if(n % 2 == 1): arr = [0] else: arr = [] for i in range(1 , n // 2 + 1): arr.extend([i , -i]) return arr
find-n-unique-integers-sum-up-to-zero
[Python3] Simple Solution in O(n) runtime , Runtime beats 93% of Python3 submissions
Lolopola
1
69
find n unique integers sum up to zero
1,304
0.771
Easy
19,402
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1096029/Easy-Python3-Solution
class Solution: def sumZero(self, n: int) -> List[int]: ans = [] num = 1 if n == 1: ans.append(0) return ans if n % 2 != 0: ans.append(0) length = n // 2 for i in range(0, length): ans.append(num) ans.append(num * (-1)) num += 1 return ans
find-n-unique-integers-sum-up-to-zero
Easy Python3 Solution
nematov_olimjon
1
116
find n unique integers sum up to zero
1,304
0.771
Easy
19,403
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/463935/Python3-one-liner
class Solution: def sumZero(self, n: int) -> List[int]: return list(range(n-1)) + [-(n-1)*(n-2)//2]
find-n-unique-integers-sum-up-to-zero
[Python3] one-liner
ye15
1
77
find n unique integers sum up to zero
1,304
0.771
Easy
19,404
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2844203/Simple-Python-Solution
class Solution: def sumZero(self, n: int) -> List[int]: answer = [] if n % 2 != 0: # Number is odd, append 0 to answer answer.append(0) for num in range(1, (n // 2) + 1): answer.append(num) answer.append(-num) return answer
find-n-unique-integers-sum-up-to-zero
Simple Python Solution
corylynn
0
2
find n unique integers sum up to zero
1,304
0.771
Easy
19,405
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2800532/Python-Solution
class Solution: def sumZero(self, n: int) -> List[int]: l=[] if(n%2==0): for i in range(1,(n//2)+1): l.append(i) l.append(-i) else: l.append(0) for i in range(1,(n//2)+1): l.append(i) l.append(-i) return l
find-n-unique-integers-sum-up-to-zero
Python Solution
CEOSRICHARAN
0
5
find n unique integers sum up to zero
1,304
0.771
Easy
19,406
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2796581/python-super-easy
class Solution: def sumZero(self, n: int) -> List[int]: i = 1 arr = [] while n > 1: arr.append(-i) arr.append(i) i+=1 n-=2 if n == 1: arr.append(0) return arr
find-n-unique-integers-sum-up-to-zero
python super easy
harrychen1995
0
5
find n unique integers sum up to zero
1,304
0.771
Easy
19,407
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2755960/Python3-Solution-oror-Symmetric-Filling
class Solution: def sumZero(self, n: int) -> List[int]: rang, remind = n//2, n%2 if remind == 0: new = [] else: new = [0] for i in range(1, rang+1): new.append(-i) new.append(i) return new
find-n-unique-integers-sum-up-to-zero
Python3 Solution || Symmetric Filling
shashank_shashi
0
3
find n unique integers sum up to zero
1,304
0.771
Easy
19,408
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2750430/Python3-One-Line-Fast-Solution
class Solution: def sumZero(self, n: int) -> List[int]: return [i for i in range (1,n//2+1)]+[-i for i in range (1,n//2+1)]+([0] if n%2!=0 else [])
find-n-unique-integers-sum-up-to-zero
[Python3] One-Line Fast Solution
keioon
0
6
find n unique integers sum up to zero
1,304
0.771
Easy
19,409
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2708793/easy-Python
class Solution: def sumZero(self, n: int) -> List[int]: return list(range(1,n//2+1))+[0]*(n&1)+list(range(-(n//2),0))
find-n-unique-integers-sum-up-to-zero
easy Python
MaryLuz
0
3
find n unique integers sum up to zero
1,304
0.771
Easy
19,410
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2705100/Python-simple-approach
class Solution: def sumZero(self, n: int) -> List[int]: l1 = [] if n%2 == 1: l1.append(0) for i in range(n//2): l1.extend([i+1, -(i+1)]) return l1
find-n-unique-integers-sum-up-to-zero
Python simple approach
swiftv99
0
5
find n unique integers sum up to zero
1,304
0.771
Easy
19,411
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2703988/Python-naive-brute-forse
class Solution: def sumZero(self, n: int) -> List[int]: if n==1: return [0] if n%2==0: out=[] else: out=[0] for i in range(1,n//2+1): out.extend([i,-i]) return out
find-n-unique-integers-sum-up-to-zero
Python, naive brute forse
Leox2022
0
1
find n unique integers sum up to zero
1,304
0.771
Easy
19,412
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2692924/Easy-and-simple-solution
class Solution: def sumZero(self, n: int) -> List[int]: res=[] if(n%2==1): res.append(0) for i in range(1,n//2+1): res.append(i) res.append(-i) return res
find-n-unique-integers-sum-up-to-zero
Easy and simple solution
Raghunath_Reddy
0
7
find n unique integers sum up to zero
1,304
0.771
Easy
19,413
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2660724/Python-solution
class Solution: def sumZero(self, n: int) -> List[int]: res = [0] if n % 2 == 1 else [] cur = 1 while len(res) < n: res.append(cur) res.append(-1*cur) cur += 1 return res
find-n-unique-integers-sum-up-to-zero
Python solution
Mark5013
0
31
find n unique integers sum up to zero
1,304
0.771
Easy
19,414
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2622651/SIMPLE-PYTHON3-SOLUTION-easy-approach-explained
class Solution: def sumZero(self, n: int) -> List[int]: if n == 1: return [0] res = [0 for x in range(n)] # fill array with 0 c = 1 # Fill array with disctinc numbers in the following pattern . [-1,-2,0, 2,1] left, right = 0, len(res)-1 while left < right: res[left] = c * -1 res[right] = c c +=1 left +=1 right -=1 if len(res) % 2 != 0: mid = len(res) // 2 res[mid] = 0 return res
find-n-unique-integers-sum-up-to-zero
✅✔ SIMPLE PYTHON3 SOLUTION ✅✔ easy approach explained
rajukommula
0
44
find n unique integers sum up to zero
1,304
0.771
Easy
19,415
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2601420/Unique-Integer-sum-to-0-oror-Python-oror-Easy-and-simple-approach
class Solution: def sumZero(self, n: int) -> List[int]: ans = [] limit = int(n/2) for i in range(1, limit+1): ans.append(-i) ans.append(i) if(n%2 == 1): ans.append(0) return ans
find-n-unique-integers-sum-up-to-zero
Unique Integer sum to 0 || Python || Easy and simple approach
vanshika_2507
0
13
find n unique integers sum up to zero
1,304
0.771
Easy
19,416
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2550244/Python3-or-Two-Pointers
class Solution: def sumZero(self, n: int) -> List[int]: l,r=1,-1 ans=[] while l<=n//2: ans.append(l) ans.append(r) l+=1 r-=1 if n%2!=0: ans.append(0) return ans
find-n-unique-integers-sum-up-to-zero
[Python3] | Two Pointers
swapnilsingh421
0
18
find n unique integers sum up to zero
1,304
0.771
Easy
19,417
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2426830/Python-easy-O(n)-solution
class Solution: def sumZero(self, n: int) -> List[int]: result = [] for num in range(n//2): result.append(num + 1) result.append(-num - 1) if n % 2 != 0: result.append(0) return result
find-n-unique-integers-sum-up-to-zero
Python easy O(n) solution
samanehghafouri
0
33
find n unique integers sum up to zero
1,304
0.771
Easy
19,418
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2092131/PYTHON-or-Super-simple-python-solution
class Solution: def sumZero(self, n: int) -> List[int]: sum = 0 res = [] for i in range(1, n): res.append(i) sum += i res.append(-sum) return res
find-n-unique-integers-sum-up-to-zero
PYTHON | Super simple python solution
shreeruparel
0
129
find n unique integers sum up to zero
1,304
0.771
Easy
19,419
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/2080772/O(N)-simple-solution
class Solution: def sumZero(self, n: int) -> List[int]: res = [] for i in range(1, n // 2 + 1): res.extend([i, -i]) if n % 2: res.append(0) return res
find-n-unique-integers-sum-up-to-zero
O(N) simple solution
andrewnerdimo
0
76
find n unique integers sum up to zero
1,304
0.771
Easy
19,420
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1954197/Python-Easy-And-Fast-Solution
class Solution: def sumZero(self, n: int) -> List[int]: result = [] for i in range(1, int(abs(n/2)) + 1): result.extend([i, -i]) if len(result) != n: result.append(0) return result
find-n-unique-integers-sum-up-to-zero
Python Easy And Fast Solution
hardik097
0
52
find n unique integers sum up to zero
1,304
0.771
Easy
19,421
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1927896/easy-python-code
class Solution: def sumZero(self, n: int) -> List[int]: output = [] if n%2 == 0: for i in range(1,(n//2)+1): output.append(i) output.append(i-2*i) else: output.append(0) for i in range(1,(n//2)+1): output.append(i) output.append(i-2*i) return output
find-n-unique-integers-sum-up-to-zero
easy python code
dakash682
0
61
find n unique integers sum up to zero
1,304
0.771
Easy
19,422
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1887183/Python-solution-using-consecutive-integers
class Solution: def sumZero(self, n: int) -> List[int]: res = [] if n % 2 == 0: res.extend([x for x in range(1, (n // 2) + 1)]) res.extend([y for y in range(-(n // 2), 0, 1)]) return sorted(res) res.append(0) res.extend([x for x in range(1, (n // 2) + 1)]) res.extend([y for y in range(-(n // 2), 0, 1)]) return sorted(res)
find-n-unique-integers-sum-up-to-zero
Python solution using consecutive integers
alishak1999
0
91
find n unique integers sum up to zero
1,304
0.771
Easy
19,423
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1873738/python-simpe-short-onliner-or-rangeorwell-explainedorbruteforce-and-best-solution
class Solution: def sumZero(self, n: int) -> List[int]: k=[i+1 for i in range(n//2)] k+=[-(i+1) for i in range(n//2)] return k if n%2==0 else k+[0]
find-n-unique-integers-sum-up-to-zero
python simpe short onliner | range|well explained|bruteforce and best solution
YaBhiThikHai
0
63
find n unique integers sum up to zero
1,304
0.771
Easy
19,424
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1873738/python-simpe-short-onliner-or-rangeorwell-explainedorbruteforce-and-best-solution
class Solution: def sumZero(self, n: int) -> List[int]: return range(1-n,n,2) # intution # n=0 [0] # n=1 [-1,1] obeservation: n[-1]-n[0]=2=step # n=3 [-2,0,2] # n=4 [-3,-1,1,3] # n=5 [-4,-2,0,2,4] note: start=1-n and end=n-1
find-n-unique-integers-sum-up-to-zero
python simpe short onliner | range|well explained|bruteforce and best solution
YaBhiThikHai
0
63
find n unique integers sum up to zero
1,304
0.771
Easy
19,425
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1856397/Python-Solution
class Solution: def sumZero(self, n: int) -> List[int]: ans = [] counter = 1 if n % 2 == 1: ans.append(0) n -= 1 times = n // 2 for i in range(times): ans.append(-1 * counter) ans.append(counter) counter += 1 return ans
find-n-unique-integers-sum-up-to-zero
Python Solution
DietCoke777
0
70
find n unique integers sum up to zero
1,304
0.771
Easy
19,426
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1849902/Python-One-Line!-Simple-and-Elegant
class Solution(object): def sumZero(self, n): return range(-n//2+n%2,0) + range(0,n%2) + range(1,n//2+1)
find-n-unique-integers-sum-up-to-zero
Python - One Line! Simple and Elegant
domthedeveloper
0
61
find n unique integers sum up to zero
1,304
0.771
Easy
19,427
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1809367/3-Lines-Python-Solution-oror-94-Faster(32ms)-oror-Memory-less-than-80
class Solution: def sumZero(self, n: int) -> List[int]: ans = [] for i in range(1,n//2+1): ans.extend({i,-i}) return ans + [0]*(n%2)
find-n-unique-integers-sum-up-to-zero
3-Lines Python Solution || 94% Faster(32ms) || Memory less than 80%
Taha-C
0
68
find n unique integers sum up to zero
1,304
0.771
Easy
19,428
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1754946/Faster-than-90-Python
class Solution: def sumZero(self, n: int) -> List[int]: ans = [] if n % 2 == 0: b = int(-n/2) for i in range(0,n): if i == n/2: b = 1 ans.append(b) b += 1 return ans b = int((n//2)*-1) for i in range(0,n): ans.append(b) b += 1 return ans
find-n-unique-integers-sum-up-to-zero
Faster than 90% Python
ra6115
0
97
find n unique integers sum up to zero
1,304
0.771
Easy
19,429
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1730008/Python-dollarolution
class Solution: def sumZero(self, n: int) -> List[int]: l, count = [], 0 if n % 2 == 0: for i in range(1,n//2+1): l += [i,-i] else: for i in range(n-1): l.append(i) count += i l.append(-count) return l
find-n-unique-integers-sum-up-to-zero
Python $olution
AakRay
0
72
find n unique integers sum up to zero
1,304
0.771
Easy
19,430
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1687269/Trivial-O(n)-python
class Solution: def sumZero(self, n: int) -> List[int]: arr = [] for i in range(n//2): arr.append(i + 1) arr.append(-1 * (i + 1)) if len(arr) < n: arr.append(0) return arr
find-n-unique-integers-sum-up-to-zero
Trivial O(n) python
snagsbybalin
0
35
find n unique integers sum up to zero
1,304
0.771
Easy
19,431
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1290501/Python-3-%3A-Simple-and-easy-to-understand
class Solution: def sumZero(self, n: int) -> List[int]: if n % 2 == 0 : return [ i for i in range(-(n//2),(n//2)+1) if i != 0 ] else : return [ i for i in range(-(n//2),(n//2)+1) ]
find-n-unique-integers-sum-up-to-zero
Python 3 : Simple and easy to understand
rohitkhairnar
0
248
find n unique integers sum up to zero
1,304
0.771
Easy
19,432
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1230078/Python
class Solution: def sumZero(self, n: int) -> List[int]: result = [] for i in range(1, (n // 2)+1): result += [i, -i] if len(result) < n: result += [0] return result
find-n-unique-integers-sum-up-to-zero
Python
dev-josh
0
208
find n unique integers sum up to zero
1,304
0.771
Easy
19,433
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1167891/Python-pythonic
class Solution: def sumZero(self, n: int) -> List[int]: result = [x for i in range(1, n//2+1) for x in {i, -i}] if n % 2: result.append(0) return result
find-n-unique-integers-sum-up-to-zero
[Python] pythonic
cruim
0
98
find n unique integers sum up to zero
1,304
0.771
Easy
19,434
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1159253/simple-to-understand
class Solution: def sumZero(self, n: int) -> List[int]: res=[] i,j=0,0 if n%2!=0: res.append(0) while n-1 : res.append(i+1) res.append(j-1) n-=2 i+=1 j-=1 else: while n: res.append(i+1) res.append(j-1) n-=2 i+=1 j-=1 res.sort() return(res)
find-n-unique-integers-sum-up-to-zero
simple to understand
janhaviborde23
0
78
find n unique integers sum up to zero
1,304
0.771
Easy
19,435
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/1027862/Python3-simple-solution
class Solution: def sumZero(self, n: int) -> List[int]: l = [] if n % 2 == 0: for i in range(-(n//2),n//2+1,1): l.append(i) l.remove(0) return l else: for i in range(-(n//2),n//2+1,1): l.append(i) return l
find-n-unique-integers-sum-up-to-zero
Python3 simple solution
EklavyaJoshi
0
113
find n unique integers sum up to zero
1,304
0.771
Easy
19,436
https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/discuss/909368/Easiest-solution-Python-O(N)-100-space
class Solution: def sumZero(self, n: int) -> List[int]: l = [] if n % 2 == 0: for i in range(1, n//2 + 1): l.append(i * 2) l.append(-i * 2) else: for i in range(-n//2 + 1, n//2 + 1): l.append(i) return l
find-n-unique-integers-sum-up-to-zero
Easiest solution Python O(N), 100% space
vanigupta20024
0
179
find n unique integers sum up to zero
1,304
0.771
Easy
19,437
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/523589/python-only-2-lines-easy-to-read-with-explanation.-Can-it-be-any-shorter
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: h = lambda r: h(r.left) + [r.val] + h(r.right) if r else [] return sorted( h(root1) + h(root2) )
all-elements-in-two-binary-search-trees
python, only 2 lines, easy to read, with explanation. Can it be any shorter?
rmoskalenko
2
155
all elements in two binary search trees
1,305
0.798
Medium
19,438
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/708517/Easy-Python-O(n)-Merge-Sorted-Lists
class Solution: # Time: O(n) # Space: O(n) def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: l1 = self.dfs(root1, []) l2 = self.dfs(root2, []) res = [] while l1 or l2: if not l1: res.append(l2.pop(0)) elif not l2: res.append(l1.pop(0)) else: res.append(l1.pop(0) if l1[0] < l2[0] else l2.pop(0)) return res def dfs(self, node, vals): if not node: return self.dfs(node.left, vals) vals.append(node.val) self.dfs(node.right, vals) return vals
all-elements-in-two-binary-search-trees
Easy Python O(n) Merge Sorted Lists
whissely
1
162
all elements in two binary search trees
1,305
0.798
Medium
19,439
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/464010/Python-3-(DFS)-(beats-100)-(eight-lines)
class Solution: def getAllElements(self, R1: TreeNode, R2: TreeNode) -> List[int]: A = [] def dfs(T): A.append(T.val) if T.left != None: dfs(T.left) if T.right != None: dfs(T.right) if R1 != None: dfs(R1) if R2 != None: dfs(R2) return sorted(A) - Junaid Mansuri - Chicago, IL
all-elements-in-two-binary-search-trees
Python 3 (DFS) (beats 100%) (eight lines)
junaidmansuri
1
323
all elements in two binary search trees
1,305
0.798
Medium
19,440
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/2832705/Python3-Solution
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def bstToArrayInorder(root, arr): if root is None: return bstToArrayInorder(root.left, arr) arr.append(root.val) bstToArrayInorder(root.right, arr) return arr = [] bstToArrayInorder(root1, arr) bstToArrayInorder(root2, arr) return sorted(arr)
all-elements-in-two-binary-search-trees
Python3 Solution
sipi09
0
2
all elements in two binary search trees
1,305
0.798
Medium
19,441
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/2701853/This-solution-is-much-faster
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: result = [] def inorder(root: TreeNode): if root: inorder(root.left) result.append(root.val) inorder(root.right) inorder(root1) inorder(root2) return sorted(result)
all-elements-in-two-binary-search-trees
This solution is much faster
namashin
0
3
all elements in two binary search trees
1,305
0.798
Medium
19,442
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/2701814/Python-and-Golang-step-by-step-Solution
class Solution: def inorder_generator(self, root: TreeNode) -> Generator: if root: yield from self.inorder_generator(root.left) yield root.val yield from self.inorder_generator(root.right) def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: # Get root1 and root2 all elements # by generator in order to save memory. root1_list = [val for val in self.inorder_generator(root1)] root2_list = [val for val in self.inorder_generator(root2)] # Merge, Sort it and Return return sorted(root1_list + root2_list)
all-elements-in-two-binary-search-trees
Python and Golang step by step Solution
namashin
0
1
all elements in two binary search trees
1,305
0.798
Medium
19,443
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1782710/Python3-solution-using-sort
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def dfs(root): if not root: return dfs(root.left) s.append(root.val) dfs(root.right) s=[] dfs(root1) dfs(root2) return sorted(s)
all-elements-in-two-binary-search-trees
Python3 solution using sort
Karna61814
0
31
all elements in two binary search trees
1,305
0.798
Medium
19,444
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1722548/python-easy-O(n%2Bm)-time-O(n%2Bm)-space-solution-using-inorder-traversal
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: res = [] def inorder(node): if node: inorder(node.left) res.append(node.val) inorder(node.right) inorder(root1) res.append(float('-inf')) inorder(root2) ans = [] n = len(res) i, j = 0, res.index(float('-inf'))+1 k = res.index(float('-inf')) while i < k and j < n: if i < k and j < n and res[i] == res[j]: ans.append(res[i]) ans.append(res[i]) i += 1 j += 1 elif i < k and j< n and res[i] < res[j]: ans.append(res[i]) i += 1 elif i< k and j< n and res[i] > res[j]: # res[i] > res[j] ans.append(res[j]) j += 1 if i < k: while i < k: ans.append(res[i]) i +=1 elif j < n: while j < n: ans.append(res[j]) j += 1 return ans
all-elements-in-two-binary-search-trees
python easy O(n+m) time, O(n+m) space solution using inorder traversal
byuns9334
0
23
all elements in two binary search trees
1,305
0.798
Medium
19,445
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1722061/Easy-Python-using-Inorder-Traversal-or-Faster-than-100
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: result = [] self.helper(root1, result) self.helper(root2, result) return sorted(result) def helper(self, root, result): if not root: return if root.left: self.helper(root.left, result) result.append(root.val) if root.right: self.helper(root.right, result)
all-elements-in-two-binary-search-trees
Easy Python using Inorder Traversal | Faster than 100%
Fyzzys
0
14
all elements in two binary search trees
1,305
0.798
Medium
19,446
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1721958/Python-orRecursion-or-Simple-Solution-or-TreeToList-O(n)or-Beginner
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def treeToList(root): if not root: return [] return treeToList(root.left)+[root.val]+treeToList(root.right) def mergeLists(l1, l2): p1 = 0 p2 = 0 res = list() while(p1 < len(l1) and p2 < len(l2)): if l1[p1] > l2[p2]: res.append(l2[p2]) p2 += 1 else: res.append(l1[p1]) p1 +=1 while(p1 < len(l1)): res.append(l1[p1]) p1 +=1 while(p2 < len(l2)): res.append(l2[p2]) p2 +=1 return res t1 = treeToList(root1) t2 = treeToList(root2) return mergeLists(t1,t2)
all-elements-in-two-binary-search-trees
Python |Recursion | Simple Solution | TreeToList O(n)| Beginner
letyrodri
0
17
all elements in two binary search trees
1,305
0.798
Medium
19,447
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1721869/python-with-preorder-traverse-method-(faster-than-68.01)
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def preorder_(root, node_list): if not root: return node_list.append(root.val) preorder_(root.left, node_list) preorder_(root.right, node_list) ans = [] preorder_(root1, ans) preorder_(root2, ans) return sorted(ans) ```
all-elements-in-two-binary-search-trees
python with preorder traverse method (faster than 68.01%)
ctw01
0
10
all elements in two binary search trees
1,305
0.798
Medium
19,448
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1721466/Python-3-Just-flatten-the-trees-into-lists-and-sort-them-(312ms-18MB).
class Solution: def flatten_tree(self, root: TreeNode) -> List[int]: result = [] trees = [root] while trees: tree_copies = trees[:] trees = [] for subtree in tree_copies: if subtree: result.append(subtree.val) trees.append(subtree.left) trees.append(subtree.right) if not any(trees): break return result def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: list1 = self.flatten_tree(root1) list2 = self.flatten_tree(root2) return sorted(list1 + list2)
all-elements-in-two-binary-search-trees
[Python 3] Just flatten the trees into lists and sort them (312ms, 18MB).
seankala
0
15
all elements in two binary search trees
1,305
0.798
Medium
19,449
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720820/Python3-oror-Recursive-Solution
class Solution: def getList(self, root,lst): if root: lst.append(root.val) self.getList(root.left,lst) self.getList(root.right,lst) return lst def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: lst=self.getList(root1,[]) lst=self.getList(root2,lst) return sorted(lst)
all-elements-in-two-binary-search-trees
[ ✅ ✅ ✅ Python3 || Recursive Solution ] 🆗 🆗 🆗
alchimie54
0
16
all elements in two binary search trees
1,305
0.798
Medium
19,450
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720684/Python-Simple-Python-Solution-Using-Preorder-Traversal-and-Sorting
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: ans=[] def PrintTree(node): if node != None: ans.append(node.val) PrintTree(node.left) PrintTree(node.right) PrintTree(root1) PrintTree(root2) ans=sorted(ans) return ans
all-elements-in-two-binary-search-trees
[ Python ] ✔✔ Simple Python Solution Using Preorder Traversal and Sorting
ASHOK_KUMAR_MEGHVANSHI
0
19
all elements in two binary search trees
1,305
0.798
Medium
19,451
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720407/Python-3-Solution-Linear-Time-Recursive-and-Iterative-Inorder-Traversal
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: list1, list2 = [], [] self.inOrder(root1, list1) self.inOrder(root2, list2) return self.mergeSortedList(list1, list2) def inOrder(self, node, res): if node is None: return self.preOrder(node.left, res) res.append(node.val) self.preOrder(node.right, res) def mergeSortedList(self, list1, list2): i, j = 0, 0 res = [] while i < len(list1) and j < len(list2): if list1[i] <= list2[j]: res.append(list1[i]) i += 1 else: res.append(list2[j]) j += 1 while i < len(list1): res.append(list1[i]) i += 1 while j < len(list2): res.append(list2[j]) j += 1 return res
all-elements-in-two-binary-search-trees
[Python 3] Solution Linear Time Recursive & Iterative Inorder Traversal
gcobs0834
0
15
all elements in two binary search trees
1,305
0.798
Medium
19,452
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720407/Python-3-Solution-Linear-Time-Recursive-and-Iterative-Inorder-Traversal
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: stack1, stack2, res = [], [], [] tree1Node = self.getNextNode(stack1, root1) tree2Node = self.getNextNode(stack2, root2) while tree1Node and tree2Node and len(stack1) and len(stack2): if tree1Node.val <= tree2Node.val: stack1.pop() res.append(tree1Node.val) tree1Node = self.getNextNode(stack1, tree1Node.right) else: stack2.pop() res.append(tree2Node.val) tree2Node = self.getNextNode(stack2, tree2Node.right) while tree1Node and len(stack1): stack1.pop() res.append(tree1Node.val) tree1Node = self.getNextNode(stack1, tree1Node.right) while tree2Node and len(stack2): stack2.pop() res.append(tree2Node.val) tree2Node = self.getNextNode(stack2, tree2Node.right) return res def getNextNode(self, stack, node): while node is not None: stack.append(node) node = node.left return stack[-1] if stack else None
all-elements-in-two-binary-search-trees
[Python 3] Solution Linear Time Recursive & Iterative Inorder Traversal
gcobs0834
0
15
all elements in two binary search trees
1,305
0.798
Medium
19,453
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720319/Python-3-EASY-Intuitive-Solution
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: res = [] def BST_to_list(root: TreeNode) -> None: nonlocal res if root: res.append(root.val) BST_to_list(root.left) BST_to_list(root.right) BST_to_list(root1) BST_to_list(root2) res.sort() return res
all-elements-in-two-binary-search-trees
✅ [Python 3] EASY Intuitive Solution
JawadNoor
0
10
all elements in two binary search trees
1,305
0.798
Medium
19,454
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720259/Python-3-simple-dfs
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def dfs(root): if root is None: return [] return dfs(root.left) + [root.val] + dfs(root.right) list1, list2 = dfs(root1), dfs(root2) n1, n2 = len(list1), len(list2) i = j = 0 res = [] while i < n1 and j < n2: if list1[i] <= list2[j]: res.append(list1[i]) i += 1 else: res.append(list2[j]) j += 1 for k in range(i, n1): res.append(list1[k]) for k in range(j, n2): res.append(list2[k]) return res
all-elements-in-two-binary-search-trees
Python 3 simple dfs
dereky4
0
35
all elements in two binary search trees
1,305
0.798
Medium
19,455
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720152/Python-O(n)-Solution
class Solution: def inorder(self, node, arr): if node: self.inorder(node.left, arr) arr.append(node.val) self.inorder(node.right, arr) def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: l = [] r = [] self.inorder(root1, l) self.inorder(root2, r) n = len(l) m = len(r) res = [0] * (m + n) i, j, k = n - 1, m - 1, m + n - 1 while i >= 0 and j >= 0: if l[i] > r[j]: res[k] = l[i] i -= 1 else: res[k] = r[j] j -= 1 k -= 1 while i >= 0: res[k] = l[i] i -= 1 k -= 1 while j >= 0: res[k] = r[j] j -= 1 k -= 1 return res
all-elements-in-two-binary-search-trees
Python O(n) Solution
imrajeshberwal
0
50
all elements in two binary search trees
1,305
0.798
Medium
19,456
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1720133/Python-Solution-DFS%2BSort
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: items = [] def dfs(root): if root is None: return items.append(root.val) dfs(root.left) dfs(root.right) dfs(root1) dfs(root2) items.sort() return items
all-elements-in-two-binary-search-trees
Python Solution, DFS+Sort
pradeep288
0
30
all elements in two binary search trees
1,305
0.798
Medium
19,457
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/830861/All-elements-in-2-BST%3A-naive-99-python3-brute-force-solution-(324ms)
class Solution: def push(self, root, result): if root.left: self.push(root.left, result) result.append(root.val) if root.right: self.push(root.right, result) def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: result = [] if root1: self.push(root1, result) if root2: self.push(root2, result) result.sort() return result
all-elements-in-two-binary-search-trees
All elements in 2 BST: naive 99% python3 brute force solution (324ms)
leetavenger
0
44
all elements in two binary search trees
1,305
0.798
Medium
19,458
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/722507/python3Java-BFS
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: ans=[] stack=[root1,root2] while stack: temp=stack.pop(0) if not temp:continue ans.append(temp.val) if temp.left:stack.append(temp.left) if temp.right:stack.append(temp.right) return sorted(ans) ···
all-elements-in-two-binary-search-trees
python3/Java BFS
752937603
0
96
all elements in two binary search trees
1,305
0.798
Medium
19,459
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/668088/Python-3.-All-Elements-in-Two-Binary-Trees.-Easy-DFS-Solution.-Beats-93.
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def traverse(node): if not node: return None traverse(node.left) listBoth.append(node.val) traverse(node.right) listBoth=[] traverse(root1) traverse(root2) listBoth.sort() return listBoth
all-elements-in-two-binary-search-trees
[Python 3]. All Elements in Two Binary Trees. Easy DFS Solution. Beats 93%.
tilak_
0
93
all elements in two binary search trees
1,305
0.798
Medium
19,460
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/463908/Python3-inorder-traversal-and-merge
class Solution: def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: def fn(node): ans, stack = [], [] while node or stack: if node: stack.append(node) node = node.left else: node = stack.pop() ans.append(node.val) node = node.right return ans vals1 = fn(root1) vals2 = fn(root2) ans = [] i = j = 0 while i < len(vals1) or j < len(vals2): if j == len(vals2) or i < len(vals1) and vals1[i] < vals2[j]: ans.append(vals1[i]) i += 1 else: ans.append(vals2[j]) j += 1 return ans
all-elements-in-two-binary-search-trees
[Python3] inorder traversal & merge
ye15
0
80
all elements in two binary search trees
1,305
0.798
Medium
19,461
https://leetcode.com/problems/all-elements-in-two-binary-search-trees/discuss/1284721/Python-Faster-Than-92-Recursive-In-Order
class Solution: def __init__(self): self.l1 = [] self.l2 = [] def inOrder(self,root, s): if root == None: return self.inOrder(root.left, s) s.append(root.val) self.inOrder(root.right, s) def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]: self.inOrder(root1, self.l1) self.inOrder(root2, self.l2) l = self.l1 + self.l2 l.sort() return l
all-elements-in-two-binary-search-trees
Python Faster Than 92% Recursive In Order
paramvs8
-1
96
all elements in two binary search trees
1,305
0.798
Medium
19,462
https://leetcode.com/problems/jump-game-iii/discuss/571683/Python3-3-Lines-DFS.-O(N)-time-and-space.-Recursion
class Solution: def canReach(self, arr: List[int], i: int) -> bool: if i < 0 or i >= len(arr) or arr[i] < 0: return False arr[i] *= -1 # Mark visited return arr[i] == 0 or self.canReach(arr, i - arr[i]) or self.canReach(arr, i + arr[i])
jump-game-iii
[Python3] 3 Lines DFS. O(N) time and space. Recursion
jimmyyentran
5
245
jump game iii
1,306
0.631
Medium
19,463
https://leetcode.com/problems/jump-game-iii/discuss/2732996/Easy-python-solution-using-BFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: n=len(arr) visited=[0]*n lst=[start] visited[start]=1 while lst: x=lst.pop(0) if arr[x]==0: return True if x+arr[x]<n and visited[x+arr[x]]==0: lst.append(x+arr[x]) visited[x+arr[x]]=1 if x-arr[x]>=0 and visited[x-arr[x]]==0: lst.append(x-arr[x]) visited[x-arr[x]]=1 return False
jump-game-iii
Easy python solution using BFS
beneath_ocean
2
99
jump game iii
1,306
0.631
Medium
19,464
https://leetcode.com/problems/jump-game-iii/discuss/1968978/Python3-oror-BFS-oror-Faster-than-89.62
class Solution: def canReach(self, arr: List[int], start: int) -> bool: vis = [0]*len(arr) pos = [start] while pos: nextPos = [] while pos: x = pos.pop() if arr[x] == 0: return True vis[x] = 1 for y in (x-arr[x], x+arr[x]): if 0 <= y < len(arr) and not vis[y]: nextPos.append(y) pos = nextPos return False
jump-game-iii
Python3 || BFS || Faster than 89.62%
bvian
2
58
jump game iii
1,306
0.631
Medium
19,465
https://leetcode.com/problems/jump-game-iii/discuss/1389143/Python-BFS-Approach
class Solution: def canReach(self, arr: List[int], start: int) -> bool: queue = [start] visited = set() while queue: u = queue.pop(0) if arr[u] == 0: return True visited.add(u) nextjump = u + arr[u] if nextjump < len(arr) and nextjump not in visited: if arr[nextjump] == 0: return True visited.add(nextjump) queue.append(nextjump) nextjump = u - arr[u] if nextjump >= 0 and nextjump not in visited: if arr[nextjump] == 0: return True visited.add(nextjump) queue.append(nextjump) return False
jump-game-iii
[Python] BFS Approach
mizan-ali
2
120
jump game iii
1,306
0.631
Medium
19,466
https://leetcode.com/problems/jump-game-iii/discuss/463924/Python-traversal-(DFS)
class Solution: def canReach(self, arr: List[int], start: int) -> bool: stack = [start] arr[start] *= -1 # mark "visited" while stack: i = stack.pop() if arr[i] == 0: return True for ii in i - arr[i], i + arr[i]: if 0 <= ii < len(arr) and arr[ii] >= 0: stack.append(ii) arr[ii] *= -1 return False
jump-game-iii
[Python] traversal (DFS)
ye15
2
86
jump game iii
1,306
0.631
Medium
19,467
https://leetcode.com/problems/jump-game-iii/discuss/2449212/python-perfect-answer-BFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: qu=deque([start]) vis=set() while qu: r=len(qu) for i in range(r): temp=qu.popleft() vis.add(temp) if arr[temp]==0: return True if temp+arr[temp] in range(len(arr)) and temp+arr[temp] not in vis: qu.append(temp+arr[temp]) if temp-arr[temp] in range(len(arr)) and temp-arr[temp] not in vis: qu.append(temp-arr[temp]) return False
jump-game-iii
python perfect answer BFS
benon
1
63
jump game iii
1,306
0.631
Medium
19,468
https://leetcode.com/problems/jump-game-iii/discuss/2025140/3-Lines-Python-Solution-oror-85-Faster-oror-Memory-less-than-90
class Solution: def canReach(self, A: List[int], start: int) -> bool: seen=set() ; queue=deque([start]) while queue: i=queue.popleft() if A[i]==0: return True seen.add(i) for x in {i-A[i],i+A[i]}: if x not in seen and 0<=x<len(A): queue.append(x)
jump-game-iii
3-Lines Python Solution || 85% Faster || Memory less than 90%
Taha-C
1
60
jump game iii
1,306
0.631
Medium
19,469
https://leetcode.com/problems/jump-game-iii/discuss/2025140/3-Lines-Python-Solution-oror-85-Faster-oror-Memory-less-than-90
class Solution: def canReach(self, A: List[int], i: int) -> bool: if i<0 or i>=len(A) or A[i]<0: return False A[i]*=-1 return A[i]==0 or self.canReach(A,i+A[i]) or self.canReach(A,i-A[i])
jump-game-iii
3-Lines Python Solution || 85% Faster || Memory less than 90%
Taha-C
1
60
jump game iii
1,306
0.631
Medium
19,470
https://leetcode.com/problems/jump-game-iii/discuss/1619642/Python3-5-lines-or-Short-and-Clean-or-DFS-or-O(n)-Time-or-O(n)-Space
class Solution: def canReach(self, arr: List[int], start: int) -> bool: if not 0 <= start < len(arr) or arr[start] < 0: return False arr[start] *= -1 return not arr[start] or self.canReach(arr, start - arr[start]) or self.canReach(arr, start + arr[start])
jump-game-iii
[Python3] 5 lines | Short and Clean | DFS | O(n) Time | O(n) Space
PatrickOweijane
1
102
jump game iii
1,306
0.631
Medium
19,471
https://leetcode.com/problems/jump-game-iii/discuss/1619531/Simple-Python-DFS-solution-with-trace-image-and-comments
class Solution: def canReach(self, arr: List[int], start: int) -> bool: def isValidMove(idx): # to check if a particular move is allowed or not if idx >= 0 and idx < len(arr): return True return False visited = set() def canReachHelper(start, end=0): # visited set to solve cycles if start in visited: return False # found valid solution if arr[start] == end: return True steps = arr[start] visited.add(start) # jump right side of array new_positive_move = start + steps # jump left side of array new_negative_move = start - steps if isValidMove(new_positive_move): if canReachHelper(new_positive_move): # early return incase a path is found return True if isValidMove(new_negative_move): return canReachHelper(new_negative_move) return canReachHelper(start)
jump-game-iii
Simple Python DFS solution with trace image and comments
vineeth_moturu
1
102
jump game iii
1,306
0.631
Medium
19,472
https://leetcode.com/problems/jump-game-iii/discuss/1619219/Python3-Solution-DFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: if start >= 0 and start < len(arr) and arr[start] >= 0: if arr[start] == 0: return True arr[start] = -arr[start] return self.canReach(arr, start + arr[start]) or self.canReach(arr, start - arr[start]) return False
jump-game-iii
Python3 Solution - DFS
Cheems_Coder
1
79
jump game iii
1,306
0.631
Medium
19,473
https://leetcode.com/problems/jump-game-iii/discuss/529895/Python3-simple-solution-using-a-while()-loop
class Solution: def canReach(self, arr: List[int], start: int) -> bool: seen, temp = set(), [start] while temp: i = temp.pop() if arr[i] == 0: return True else: seen.add(i) if 0 <= i - arr[i] < len(arr) and i - arr[i] not in seen: temp.append(i - arr[i]) if 0 <= i + arr[i] < len(arr) and i + arr[i] not in seen: temp.append(i + arr[i]) return False
jump-game-iii
Python3 simple solution using a while() loop
jb07
1
70
jump game iii
1,306
0.631
Medium
19,474
https://leetcode.com/problems/jump-game-iii/discuss/2837860/Python-(Simple-DFS)
class Solution: def canReach(self, arr, start): n, stack, visited = len(arr), [start], set() visited.add(start) while stack: idx = stack.pop(0) if arr[idx] == 0: return True if 0 <= idx + arr[idx] < n and idx + arr[idx] not in visited: stack.append(idx + arr[idx]) visited.add(idx + arr[idx]) if 0 <= idx - arr[idx] < n and idx - arr[idx] not in visited: stack.append(idx - arr[idx]) visited.add(idx - arr[idx]) return False
jump-game-iii
Python (Simple DFS)
rnotappl
0
4
jump game iii
1,306
0.631
Medium
19,475
https://leetcode.com/problems/jump-game-iii/discuss/2825476/python-super-easy-BFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: queue = [start] visit = set() visit.add(start) while queue: size = queue for i in range(len(size)): curr = queue.pop(0) if arr[curr] == 0: return True if curr + arr[curr] < len(arr) and curr + arr[curr] not in visit: queue.append(curr+arr[curr]) visit.add(curr+arr[curr]) if curr - arr[curr] >= 0 and curr - arr[curr] not in visit: queue.append(curr-arr[curr]) visit.add(curr-arr[curr]) return False
jump-game-iii
python super easy BFS
harrychen1995
0
3
jump game iii
1,306
0.631
Medium
19,476
https://leetcode.com/problems/jump-game-iii/discuss/2713608/Recursion
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visited = set() def solve(idx): if idx < 0 or idx > len(arr)-1 or idx in visited or len(visited) == len(arr): return False if arr[idx] == 0: return True visited.add(idx) if solve(idx+arr[idx]) or solve(idx-arr[idx]): return True return False return solve(start)
jump-game-iii
Recursion
mukundjha
0
3
jump game iii
1,306
0.631
Medium
19,477
https://leetcode.com/problems/jump-game-iii/discuss/2618850/Easy-DFS-or-BFS-explanation-90%2B
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visited = set() q = deque() q.append(start) while(q): idx = q.popleft() visited.add(idx) if arr[idx] == 0: # win condition return True nxt = [(idx+arr[idx]), (idx-arr[idx])] for x in nxt: if x >= 0 and x < len(arr) and x not in visited: q.append(x) return False
jump-game-iii
Easy DFS | BFS explanation 90%+
gabrielpetrov99
0
21
jump game iii
1,306
0.631
Medium
19,478
https://leetcode.com/problems/jump-game-iii/discuss/2618850/Easy-DFS-or-BFS-explanation-90%2B
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visited = set() def dfs(i): visited.add(i) if arr[i] == 0: # win condition return True nxt = [(i+arr[i]), (i-arr[i])] for x in nxt: if x >= 0 and x < len(arr) and x not in visited and dfs(x): return True return False return dfs(start)
jump-game-iii
Easy DFS | BFS explanation 90%+
gabrielpetrov99
0
21
jump game iii
1,306
0.631
Medium
19,479
https://leetcode.com/problems/jump-game-iii/discuss/2601366/Jump-Game-3-oror-Easy-solution-oror-O(n)-Time-and-O(1)-Space-complexity-oror-Python
class Solution: def canReach(self, arr: List[int], start: int) -> bool: def traverse_path(idx, arr): # Return if not a valid index if(idx < 0 or idx >= len(arr)): return False # Return if index is already visited if(arr[idx] < 0): return False # Return if we found destination if(arr[idx] == 0): return True jumps = arr[idx] # marking value as negative so that it won't be visited again arr[idx] = -arr[idx] # from every index we have 2 options left jump or right jump return traverse_path(idx + jumps, arr) or traverse_path(idx - jumps, arr) return traverse_path(start, arr)
jump-game-iii
Jump Game 3 || Easy solution || O(n) Time and O(1) Space complexity || Python
vanshika_2507
0
23
jump game iii
1,306
0.631
Medium
19,480
https://leetcode.com/problems/jump-game-iii/discuss/2564897/Recursive-solution
class Solution: def canReach(self, arr: List[int], start: int) -> bool: n = len(arr) def recurse(start: int, seen: set) -> bool: if start in seen: return False seen.add(start) if start < 0 or start >= n: return False if arr[start] == 0: return True return recurse(start - arr[start], seen) or recurse(start + arr[start], seen) return recurse(start, set())
jump-game-iii
Recursive solution
mansoorafzal
0
20
jump game iii
1,306
0.631
Medium
19,481
https://leetcode.com/problems/jump-game-iii/discuss/2550601/Simplest-Approach
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visited = set() dp = {} def solve(start, visited): if start in dp: return dp[start] if start in visited: return visited.add(start) if start<0 or start>=len(arr): return False if arr[start] == 0: return True dp[start] = solve(start-arr[start],visited) or solve(start+arr[start],visited) return dp[start] return solve(start,visited)
jump-game-iii
Simplest Approach
Abhi_009
0
22
jump game iii
1,306
0.631
Medium
19,482
https://leetcode.com/problems/jump-game-iii/discuss/2465683/Straightforward-bfs-python3-solution
class Solution: # O(n) time, # O(1) space, # Approach: BFS, def canReach(self, arr: List[int], start: int) -> bool: n = len(arr) def getNeighbours(i:int) -> List[int]: n = len(arr) neighbours = [] # print(i) if (i + arr[i]) < n: neighbours.append(i + arr[i]) if (i - arr[i]) > -1: neighbours.append(i - arr[i]) return neighbours qu = deque() qu.append(start) vstd = set() vstd.add(start) while qu: m = len(qu) for i in range(m): nod = qu.popleft() if arr[nod] == 0: return True nbrs = getNeighbours(nod) for nb in nbrs: if nb in vstd: continue vstd.add(nb) qu.append(nb) return False
jump-game-iii
Straightforward bfs python3 solution
destifo
0
8
jump game iii
1,306
0.631
Medium
19,483
https://leetcode.com/problems/jump-game-iii/discuss/2365033/Python3-or-Solved-using-BFS-%2B-Queue-%2B-Model-Problem-as-Directed-Graph
class Solution: #n = len(arr) #Time-Complexity: O(n), in worst case we visit each and every index position and finds out #there's no indices with integer 0 ! #Space-Complexity: O(n + n), by same argument as T.C! -> O(n) def canReach(self, arr: List[int], start: int) -> bool: #We can model this as a general directed graph problem! #If we are at node i(at position index i), we can either #jump to two descendants: arr[i] + i or arr[i] - i! #We will only add to queue index positions not already visited #and in-bounds! visited = set() q = collections.deque() q.append(start) visited.add(start) #as long as queue is not empty, keep bfs going! while q: cur_index = q.popleft() #check if at current index has value 0! If so, immediately #break and return True if(arr[cur_index] == 0): return True #otherwise, process the two descendants and only add to queue #if it's not already visited and is in-bounds! neighbor1 = arr[cur_index] + cur_index neighbor2 = cur_index - arr[cur_index] if(neighbor1 not in visited and 0<=neighbor1 < len(arr)): q.append(neighbor1) visited.add(neighbor1) if(neighbor2 not in visited and 0<=neighbor2 < len(arr)): q.append(neighbor2) visited.add(neighbor2) #once bfs is over, we tried every possible path from start! #could not reach index position with value of 0! return False
jump-game-iii
Python3 | Solved using BFS + Queue + Model Problem as Directed Graph
JOON1234
0
8
jump game iii
1,306
0.631
Medium
19,484
https://leetcode.com/problems/jump-game-iii/discuss/2118392/Breadth-First-Search-(BFS)-Algorithm-in-Python
class Solution: def canReach(self, arr: List[int], start: int) -> bool: max_size = len(arr) - 1 visited = set() queue = [] queue.append(start) visited.add(start) while not len(queue) == 0: curr = queue.pop(0) print("\n") step = arr[curr] if step == 0: return True lower = curr - step upper = curr + step next_nodes = [] if lower >= 0: next_nodes.append(lower) if upper <= max_size: next_nodes.append(upper) for next_node in next_nodes: if next_node not in visited: queue.append(next_node) visited.add(next_node) return False
jump-game-iii
Breadth First Search (BFS) Algorithm in Python
juliancarax
0
38
jump game iii
1,306
0.631
Medium
19,485
https://leetcode.com/problems/jump-game-iii/discuss/2090895/Python-Solution-in-BFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: # Time O(n) # Space O(n) n = len(arr) que = deque() visited = [False] * n que.append(start) while que: tmp = que.popleft() if arr[tmp] == 0: return True if visited[tmp]: continue if tmp + arr[tmp] < n: que.append(tmp + arr[tmp]) if tmp - arr[tmp] >= 0: que.append(tmp - arr[tmp]) visited[tmp] = True return False
jump-game-iii
Python Solution in BFS
blazers08
0
26
jump game iii
1,306
0.631
Medium
19,486
https://leetcode.com/problems/jump-game-iii/discuss/1946494/PYTHON3-SOLUTION-or-Beats-100-of-python3-solutions
class Solution: def canReach(self, arr: List[int], start: int) -> bool: queue = deque() visited = set() queue.append(start) m = len(arr) while queue: index = queue.popleft() left, right = index - arr[index], index + arr[index] if arr[index] == 0: return True if 0 <= left < m and left not in visited: queue.append(left) visited.add(left) if 0 <= right < m and right not in visited: queue.append(right) visited.add(right) return False
jump-game-iii
PYTHON3 SOLUTION | Beats 100% of python3 solutions
dericktolentino2002
0
18
jump game iii
1,306
0.631
Medium
19,487
https://leetcode.com/problems/jump-game-iii/discuss/1835216/Python-or-BFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: n = len(arr) q = deque([start]) visited = {start} while q: idx = q.popleft() if arr[idx] == 0: return True forward = idx + arr[idx] # jump forwards back = idx - arr[idx] # jump backwards if forward not in visited and forward < n: visited.add(forward) q.append(forward) if back not in visited and back >= 0: visited.add(back) q.append(back) return False
jump-game-iii
Python | BFS
jgroszew
0
40
jump game iii
1,306
0.631
Medium
19,488
https://leetcode.com/problems/jump-game-iii/discuss/1773944/Python-easy-to-read-and-understand-or-DFS
class Solution: def dfs(self, arr, i, visited): if i < 0 or i >= len(arr) or visited[i] == True: return if arr[i] == 0: self.ans = True return visited[i] = True self.dfs(arr, i+arr[i], visited) self.dfs(arr, i-arr[i], visited) visited[i] = False def canReach(self, arr: List[int], start: int) -> bool: self.ans = False n = len(arr) visited = [False for _ in range(n)] self.dfs(arr, start, visited) return self.ans
jump-game-iii
Python easy to read and understand | DFS
sanial2001
0
77
jump game iii
1,306
0.631
Medium
19,489
https://leetcode.com/problems/jump-game-iii/discuss/1681478/PYTHON-Recursive-solution-DFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: self.seen = set() def dfs(arr, index): if index > len(arr)-1 or index < 0 or (index in self.seen): return False if arr[index] == 0: return True self.seen.add(index) return dfs(arr, index + arr[index]) or dfs(arr, index - arr[index]) return dfs(arr, start)
jump-game-iii
[PYTHON] Recursive solution DFS
satyu
0
47
jump game iii
1,306
0.631
Medium
19,490
https://leetcode.com/problems/jump-game-iii/discuss/1619921/Python3-Dfs-recursion-solution
class Solution: def traversal(self, arr, idx): if idx < 0 or idx >= len(arr) or arr[idx] < 0: return False if arr[idx] == 0: return True arr[idx] *= -1 return self.traversal(arr, idx + arr[idx]) or self.traversal(arr, idx - arr[idx]) def canReach(self, arr: List[int], start: int) -> bool: return self.traversal(arr, start)
jump-game-iii
[Python3] Dfs recursion solution
maosipov11
0
28
jump game iii
1,306
0.631
Medium
19,491
https://leetcode.com/problems/jump-game-iii/discuss/1619524/Iterative-and-recursive-DFS-in-Python
class Solution: def canReach(self, arr: List[int], start: int) -> bool: def helper(i: int = start): if i >= len(arr) or i < 0 or arr[i] < 0: return False if arr[i] == 0: return True arr[i] = -arr[i] return helper(i + arr[i]) or helper(i - arr[i]) return helper()
jump-game-iii
Iterative and recursive DFS in Python
mousun224
0
29
jump game iii
1,306
0.631
Medium
19,492
https://leetcode.com/problems/jump-game-iii/discuss/1619524/Iterative-and-recursive-DFS-in-Python
class Solution: def canReach(self, arr: List[int], start: int) -> bool: stack = [start] while stack: v = stack.pop() if arr[v] < 0: continue if arr[v] == 0: return True arr[v] = -arr[v] for step in (arr[v], -arr[v]): if 0 <= (v + step) < len(arr): stack += (v + step), return False
jump-game-iii
Iterative and recursive DFS in Python
mousun224
0
29
jump game iii
1,306
0.631
Medium
19,493
https://leetcode.com/problems/jump-game-iii/discuss/1618956/Python-BFS-with-explanation-(96.12-faster-84.22less-memory)
class Solution: def canReach(self, arr: List[int], start: int) -> bool: jumps = [start] while jumps: current = jumps.pop(0) if arr[current] == -1: continue elif arr[current] == 0: return True next_steps = current+arr[current], current-arr[current] # Add next steps to the queue if next_steps[0]<len(arr): jumps.append(next_steps[0]) if next_steps[1]>=0: jumps.append(next_steps[1]) # Mark the current index as visited arr[current] = -1 return False
jump-game-iii
Python BFS with explanation (96.12% faster, 84.22%less memory)
kaushalya
0
63
jump game iii
1,306
0.631
Medium
19,494
https://leetcode.com/problems/jump-game-iii/discuss/1618941/Python3-ITERATIVE-BFS-Explained-(beats100!)
class Solution: def canReach(self, arr: List[int], start: int) -> bool: n = len(arr) vis = set() q = deque([start]) while q: i = q.popleft() if arr[i] == 0: return True vis.add(i) l, r = i - arr[i], i + arr[i] if not l in vis and l > -1: q.append(l) if not r in vis and r < n: q.append(r) return False
jump-game-iii
✔️ [Python3] ITERATIVE BFS, Explained (beats100%!)
artod
0
133
jump game iii
1,306
0.631
Medium
19,495
https://leetcode.com/problems/jump-game-iii/discuss/1547883/BFS-268ms-99.7-runtime-93-memory
class Solution: def canReach(self, arr: List[int], start: int) -> bool: # let's do BFS, it's a binary tree node_queue = [start] for i in node_queue: # Check if element is blind if arr[i] is None: continue node = arr[i] if node == 0: return True left = i - node right = i + node # Bound checks if left >= 0: node_queue.append(left) if right <= len(arr) - 1: node_queue.append(right) # Blind the element from further expansion arr[i] = None return False
jump-game-iii
BFS, 268ms - 99.7% runtime, 93% memory
HoundThe
0
51
jump game iii
1,306
0.631
Medium
19,496
https://leetcode.com/problems/jump-game-iii/discuss/1465220/PyPy3-Solution-using-DFS-w-comments
class Solution: def canReach(self, arr: List[int], start: int) -> bool: # init n = len(arr) # DFS def dfs(idx, isVisited=set()): # Check if idx is not yet visited and # it is within boundaries if idx not in isVisited and 0 <= idx < n: # Add to is visited set isVisited.add(idx) # Check if destination reached, if yes, return true if arr[idx] == 0: return True # else run dfs on i+arr[i] and i-arr[i] index else: return dfs(idx+arr[idx], isVisited) or dfs(idx-arr[idx], isVisited) return False # By default return false return dfs(start) # Start dfs at start index
jump-game-iii
[Py/Py3] Solution using DFS w/ comments
ssshukla26
0
60
jump game iii
1,306
0.631
Medium
19,497
https://leetcode.com/problems/jump-game-iii/discuss/1442807/Python-Recursive-DFS-Solution
class Solution: #gets the next valid moves def getNextInd(self, i, arr): ret = [] r, l = i + arr[i], i - arr[i] if r < len(arr): ret.append(r) if l >= 0: ret.append(l) return ret def canReach(self, arr: List[int], start: int) -> bool: vis = [False for _ in range(len(arr))] def recur(start): #if already visited, it doesn't need to be reexplored if vis[start]: return False vis[start] = True if arr[start] == 0: return True moves = self.getNextInd(start, arr) reached = False for i in moves: reached = reached or recur(i) return reached
jump-game-iii
Python Recursive DFS Solution
mguthrie45
0
109
jump game iii
1,306
0.631
Medium
19,498
https://leetcode.com/problems/jump-game-iii/discuss/1375836/Python3-solution-using-recursion
class Solution: def canReach(self, arr, start): def check(index, arr, explored): if arr[index] == 0: return True else: a = None b = None if index + arr[index] < len(arr) and explored[index + arr[index]] == 0: explored[index + arr[index]] = 1 a = check(index + arr[index], arr, explored) if index - arr[index] >= 0 and explored[index - arr[index]] == 0: explored[index - arr[index]] = 1 b = check(index - arr[index], arr, explored) return a or b explored = [0]*len(arr) x = check(start, arr, explored) return x
jump-game-iii
Python3 solution using recursion
EklavyaJoshi
0
40
jump game iii
1,306
0.631
Medium
19,499