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/complex-number-multiplication/discuss/1223014/Python-Explained-or-Easy-Solution
class Solution: def complexNumberMultiply(self, num1: str, num2: str) -> str: digit1 = num1.split("+") # ['1', '-1i'] digit2 = num2.split("+") # ['1', '-1i'] digit1[1] = digit1[1].replace('i','') # ['1', '-1'] digit2[1] = digit2[1].replace('i','') # ['1', '-1'] first = (int(...
complex-number-multiplication
[Python] Explained | Easy Solution
arkumari2000
0
61
complex number multiplication
537
0.714
Medium
9,400
https://leetcode.com/problems/complex-number-multiplication/discuss/872842/Python3-3-line
class Solution: def complexNumberMultiply(self, num1: str, num2: str) -> str: r1, i1 = map(int, num1[:-1].split('+')) r2, i2 = map(int, num2[:-1].split('+')) return f"{r1*r2-i1*i2}+{r1*i2+r2*i1}i"
complex-number-multiplication
[Python3] 3-line
ye15
0
39
complex number multiplication
537
0.714
Medium
9,401
https://leetcode.com/problems/complex-number-multiplication/discuss/853013/Python-3-or-Split-%2B-f-string-or-Explanation
class Solution: def complexNumberMultiply(self, a: str, b: str) -> str: a, b = a.split('+'), b.split('+') # split integer part and complex part a1, b1 = int(a[0]), int(a[-1][:-1]) # convert to integer a2, b2 = int(b[0]), int(b[-1][:-1]) x = a1*a2 # mult...
complex-number-multiplication
Python 3 | Split + f-string | Explanation
idontknoooo
0
61
complex number multiplication
537
0.714
Medium
9,402
https://leetcode.com/problems/complex-number-multiplication/discuss/317795/Python-solution-32ms
class Solution: def complexNumberMultiply(self, a: str, b: str) -> str: k1=a.split('+') k2=b.split('+') (xa,ya)=(k1[0],k1[1][:-1]) (xb,yb)=(k2[0],k2[1][:-1]) return(str(int(xa)*int(xb)+int(ya)*int(yb)*-1)+'+'+str(int(xa)*int(yb)+int(ya)*int(xb))+'i')
complex-number-multiplication
Python solution 32ms
ketan35
0
63
complex number multiplication
537
0.714
Medium
9,403
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1057429/Python.-faster-than-100.00.-Explained-clear-and-Easy-understanding-solution.-O(n).-Recursive
class Solution: def convertBST(self, root: TreeNode) -> TreeNode: sum = 0 def sol(root: TreeNode) -> TreeNode: nonlocal sum if root: sol(root.right) root.val += sum sum = root.val sol(root.left) return root return sol(root)
convert-bst-to-greater-tree
Python. faster than 100.00%. Explained, clear & Easy-understanding solution. O(n). Recursive
m-d-f
13
912
convert bst to greater tree
538
0.674
Medium
9,404
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1951325/Python3-IN-ORDER-DFS-(-*-.-*-)-Explained
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def dfs(node, init): if not node: return 0 r_sum = dfs(node.right, init) orig, node.val = node.val, node.val + init + r_sum l_sum = dfs(node.left, node.val) ...
convert-bst-to-greater-tree
✔️ [Python3] IN-ORDER DFS ( •́ .̫ •̀ ), Explained
artod
10
546
convert bst to greater tree
538
0.674
Medium
9,405
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1952102/Python-Simple-recursion-O(N)-%3A-Reverse-Inorder
class Solution: def newBST(self, root, sumSoFar): if root == None: return sumSoFar newSum = self.newBST(root.right, sumSoFar) root.val = root.val + newSum return self.newBST(root.left, root.val) def convertBST(self, root: Optional[TreeNode]) -> ...
convert-bst-to-greater-tree
Python, Simple recursion, O(N) : Reverse Inorder
souravrane_
1
16
convert bst to greater tree
538
0.674
Medium
9,406
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1951959/Python-or-Java-Very-Easy-Solution
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def traverse(root, sm): if root is None: return sm right = traverse(root.right, sm) root.val += right left = traverse(root.left, root.val) return left tra...
convert-bst-to-greater-tree
✅ Python | Java Very Easy Solution
dhananjay79
1
76
convert bst to greater tree
538
0.674
Medium
9,407
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/2442340/Python-3-or-2-pass-solution-or-Inorder-traversal
class Solution: def __init__(self): self.arr = [] def getNodesInorder(self, node: Optional[TreeNode]): if not node: return self.getNodesInorder(node.left) self.arr.append(node) self.getNodesInorder(node.right) def convertBST(self, root: Optional[Tr...
convert-bst-to-greater-tree
Python 3 | 2-pass solution | Inorder traversal
Ploypaphat
0
14
convert bst to greater tree
538
0.674
Medium
9,408
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1957074/Python-Intutive-solution
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: vals = [] def traverse_inorder(node): if not node: return traverse_inorder(node.left) vals.append(node.val) traverse_inorder(node.right) # ...
convert-bst-to-greater-tree
Python Intutive solution
pradeep288
0
20
convert bst to greater tree
538
0.674
Medium
9,409
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1954903/3-ways-of-traverse-and-sum-or-faster-than-98.49-or-Python3
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: nodes = [] def inorder(root: Optional[TreeNode]): if root != None: inorder(root.left) nodes.append(root) inorder(root.right) inorder(root) ...
convert-bst-to-greater-tree
3 ways of traverse and sum | faster than 98.49% | Python3
qihangtian
0
5
convert bst to greater tree
538
0.674
Medium
9,410
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1954903/3-ways-of-traverse-and-sum-or-faster-than-98.49-or-Python3
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: self.total = 0 def traverse(node: Optional[TreeNode]): if node != None: traverse(node.right) self.total += node.val node.val = self.total ...
convert-bst-to-greater-tree
3 ways of traverse and sum | faster than 98.49% | Python3
qihangtian
0
5
convert bst to greater tree
538
0.674
Medium
9,411
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1954903/3-ways-of-traverse-and-sum-or-faster-than-98.49-or-Python3
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: total = 0 stack = [] node = root while node or stack: if node: stack.append(node) node = node.right else: temp = stack.pop() ...
convert-bst-to-greater-tree
3 ways of traverse and sum | faster than 98.49% | Python3
qihangtian
0
5
convert bst to greater tree
538
0.674
Medium
9,412
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1953799/Simple-and-Intuitive-Python-Solution
class Solution: # added the param 'passdown' for easier recurrsion def convertBST(self, root: Optional[TreeNode], passdown=0) -> Optional[TreeNode]: if not root: return None root.val = self.getNewVal(root, passdown) root.left = self.convertBST(root.left, root.val) if...
convert-bst-to-greater-tree
Simple and Intuitive Python Solution
Mowei
0
7
convert bst to greater tree
538
0.674
Medium
9,413
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1953029/Python-Easy-Python-Solution-Using-Array-and-Recursion
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: array = [] def InOrder(node): if node != None: InOrder(node.left) array.append(node.val) InOrder(node.right) InOrder(root) newnode = root def ReplaceInOrder(newnode): if newnode != None: Repla...
convert-bst-to-greater-tree
[ Python ] ✅✅ Easy Python Solution Using Array and Recursion ✌🥳
ASHOK_KUMAR_MEGHVANSHI
0
12
convert bst to greater tree
538
0.674
Medium
9,414
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1953024/Python-Solution-or-Recursive-Inverted-Inorder-Traversal-or-Over-99-Faster-or-Simple-Logic
class Solution: def __init__(self): self.sum = 0 def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if root is None: return None self.convertBST(root.right) self.sum += root.val root.val = self.sum self.convertBST(root.left) ...
convert-bst-to-greater-tree
Python Solution | Recursive Inverted Inorder Traversal | Over 99% Faster | Simple Logic
Gautam_ProMax
0
11
convert bst to greater tree
538
0.674
Medium
9,415
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1952454/Python-or-Very-Easy-To-Understand-or-Reverse-Inorder
class Solution: def reverseInorder(self, root): if root is None: return None if root.right: root.right = self.reverseInorder(root.right) self.sm += root.val root.val = self.sm if root.left: root.left = self.reverseInorder(root.le...
convert-bst-to-greater-tree
Python | Very Easy To Understand | Reverse Inorder
sathwickreddy
0
36
convert bst to greater tree
538
0.674
Medium
9,416
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1952283/Python-or-Reverse-Inorder-Traversal-or-Beats-98
class Solution(object): def convertBST(self, root): def inorder(root, vals): if not root: return inorder(root.right, vals) if not vals: vals.append(root.val) else: root.val += vals[-1] vals.append(root.val) ...
convert-bst-to-greater-tree
Python | Reverse Inorder Traversal | Beats 98%
prajyotgurav
0
24
convert bst to greater tree
538
0.674
Medium
9,417
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1952235/~10-line-recursive-DFS-in-Python
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def dfs(n: TreeNode = root, prev: int = 0) -> Tuple[TreeNode, int]: if not n: return None, 0 r, rs = dfs(n.right, prev) val = n.val + rs + prev l, ls = dfs...
convert-bst-to-greater-tree
~10-line recursive DFS in Python
mousun224
0
22
convert bst to greater tree
538
0.674
Medium
9,418
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1951736/My-python3-solution
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if root is None: return root arr=[]#initializing array for prefix sum mp={}# required map to map all the prefix sum values to values in BST def fun(root):# doing an inorder travesal ...
convert-bst-to-greater-tree
My python3 solution
arpit92_8
0
6
convert bst to greater tree
538
0.674
Medium
9,419
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1951679/python-recursion
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def convert(root, val): if root is None: return 0 rval = convert(root.right, val) root.val += rval + val lval = convert(root.left, root.val) ...
convert-bst-to-greater-tree
python recursion
user2613C
0
13
convert bst to greater tree
538
0.674
Medium
9,420
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1951320/Simple-python-recursion-with-an-accumulator-Beats-~90
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def convertGreaterTree(node, acc): if not node: return acc right = convertGreaterTree(node.right, acc) node.val += right #...
convert-bst-to-greater-tree
✔️Simple python recursion with an accumulator - Beats ~90%
constantine786
0
20
convert bst to greater tree
538
0.674
Medium
9,421
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1881877/Python-easy-and-intuitive-solution-using-inorder-traversal
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root: return None stack = [root] values = [] def inorder(node): if node: inorder(node.left) values.append(node.val) ...
convert-bst-to-greater-tree
Python easy and intuitive solution using inorder traversal
byuns9334
0
24
convert bst to greater tree
538
0.674
Medium
9,422
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1837109/Simple-Python-Solution
class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def inorder(root): elements=[] if root is None: return [] elements+=inorder(root.left) elements.append(root.val) elements+=inorder(root.r...
convert-bst-to-greater-tree
Simple Python Solution
Siddharth_singh
0
23
convert bst to greater tree
538
0.674
Medium
9,423
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1581330/Pyhton3-Solution
class Solution: val = 0 def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if root: if root.right : self.convertBST(root.right) self.val = root.val = root.val + self.val if root.left : self.convertBST(root.left) return root
convert-bst-to-greater-tree
Pyhton3 Solution
satyam2001
0
41
convert bst to greater tree
538
0.674
Medium
9,424
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1434471/Python3-Recursive-dfs-solution
class Solution: def traversal(self, node, total): if not node: return None self.traversal(node.right, total) total[0] += node.val node.val = total[0] self.traversal(node.left, total) def convertBST(self, root: Optional[TreeN...
convert-bst-to-greater-tree
[Python3] Recursive dfs solution
maosipov11
0
45
convert bst to greater tree
538
0.674
Medium
9,425
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1380953/Simple-Python-Recursive-Solution
class Solution: def __init__(self): self.sum = 0 def helper(self,root,val): if not root: return val else: returnedVal = self.helper(root.right,val) root.val += returnedVal return self.helper(root.left,root.val) ...
convert-bst-to-greater-tree
Simple Python Recursive Solution
Akarshippili
0
48
convert bst to greater tree
538
0.674
Medium
9,426
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1058359/Python3-beats-99-speed-reversed-inorder-traversal
class Solution: def convertBST(self, root: TreeNode) -> TreeNode: if not root: return None def update_nodes(root: TreeNode, nodes_sum: int) -> int: # base case, don't add to the sum if not root: return nodes_sum # add to the value of current node the su...
convert-bst-to-greater-tree
[Python3] beats 99% speed, reversed inorder traversal
dbialon
0
16
convert bst to greater tree
538
0.674
Medium
9,427
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/1057417/Python-Easy-DFS-approach-95%2B
class Solution: def dfs(self, root: TreeNode, s) -> int: if root.right: s = self.dfs(root.right, s) root.val += s s = root.val if root.left: s = self.dfs(root.left, s) return s def convertBST(self, root: TreeNode) -> TreeNode: if root: self.dfs(root, 0) return root
convert-bst-to-greater-tree
Python Easy DFS approach 95%+
SlavaHerasymov
0
34
convert bst to greater tree
538
0.674
Medium
9,428
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/872828/Python3-inorder-traversal
class Solution: def convertBST(self, root: TreeNode) -> TreeNode: def fn(node, x): """Inorder traverse the tree and update node's value.""" if not node: return x x = fn(node.right, x) # sum of right subtree x += node.val node.val = x ...
convert-bst-to-greater-tree
[Python3] inorder traversal
ye15
0
45
convert bst to greater tree
538
0.674
Medium
9,429
https://leetcode.com/problems/convert-bst-to-greater-tree/discuss/872828/Python3-inorder-traversal
class Solution: def convertBST(self, root: TreeNode) -> TreeNode: val = 0 node, stack = root, [] while stack or node: if node: stack.append(node) node = node.right else: node = stack.pop() node.val = v...
convert-bst-to-greater-tree
[Python3] inorder traversal
ye15
0
45
convert bst to greater tree
538
0.674
Medium
9,430
https://leetcode.com/problems/minimum-time-difference/discuss/1829297/python-3-bucket-sort-O(n)-time-O(1)-space
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: M = 1440 times = [False] * M for time in timePoints: minute = self.minute(time) if times[minute]: return 0 times[minute] = True minutes = [i for i i...
minimum-time-difference
[python 3] bucket sort, O(n) time, O(1) space
dereky4
9
595
minimum time difference
539
0.563
Medium
9,431
https://leetcode.com/problems/minimum-time-difference/discuss/1995265/Clean-Python-O(n)-vs-O(n-log-n)-Discussion-Shortcut-for-long-inputs
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: def toMinFromZero(t): return 60*int(t[:2]) + int(t[3:]) maxMins = 60*24 # number of mins in a day if len(timePoints) > maxMins: return 0 # we sort the timestamps. Idea: Th...
minimum-time-difference
Clean Python, O(n) vs O(n log n) Discussion, Shortcut for long inputs
boris17
8
528
minimum time difference
539
0.563
Medium
9,432
https://leetcode.com/problems/minimum-time-difference/discuss/488280/Python-Simple-Solution-2-Liner
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: minutes = sorted(list(map(lambda x: int(x[:2]) * 60 + int(x[3:]), timePoints))) return min((y - x) % (24 * 60) for x, y in zip(minutes, minutes[1:] + minutes[:1]))
minimum-time-difference
Python - Simple Solution - 2 Liner
mmbhatk
4
951
minimum time difference
539
0.563
Medium
9,433
https://leetcode.com/problems/minimum-time-difference/discuss/1676798/WEEB-DOES-PYTHON
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: timePoints.sort() def getTimeDiff(timeString1, timeString2): time1 = int(timeString1[:2]) * 60 + int(timeString1[3:]) time2 = int(timeString2[:2]) * 60 + int(timeString2[3:]) minDiff = abs(time1 - time2) return min(minDiff, 1...
minimum-time-difference
WEEB DOES PYTHON
Skywalker5423
3
149
minimum time difference
539
0.563
Medium
9,434
https://leetcode.com/problems/minimum-time-difference/discuss/846858/Python-3-or-Sort-Math-or-Explanations
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: def minute(time_str): h, m = time_str.split(':') return int(h)*60+int(m) total, minutes = 24*60, sorted([minute(time_str) for time_str in timePoints]) n, diff = len(minutes), total - (minutes[-...
minimum-time-difference
Python 3 | Sort, Math | Explanations
idontknoooo
2
560
minimum time difference
539
0.563
Medium
9,435
https://leetcode.com/problems/minimum-time-difference/discuss/2438080/Python-Solution-with-Sorting-and-Two-pointers
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: minutesConvert = [] # convert time points to minutes expression for time in timePoints: t = time.split(":") minutes = int(t[0]) * 60 + int(t[1]) minutesConvert.append(minutes) # sort th...
minimum-time-difference
Python Solution with Sorting and Two-pointers
siyu_
1
152
minimum time difference
539
0.563
Medium
9,436
https://leetcode.com/problems/minimum-time-difference/discuss/872912/Python3-sort-the-time
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: def fn(t): """Return time point as minute.""" h, m = map(int, t.split(":")) return 60*h + m timePoints = sorted(map(fn, timePoints)) timePoints += [timePoints[0]...
minimum-time-difference
[Python3] sort the time
ye15
1
141
minimum time difference
539
0.563
Medium
9,437
https://leetcode.com/problems/minimum-time-difference/discuss/872912/Python3-sort-the-time
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: timePoints = sorted(60*int(x[:2]) + int(x[3:]) for x in timePoints) # sorting timePoints += [timePoints[0]] return min((timePoints[i] - timePoints[i-1])%1440 for i in range(1, len(timePoints)))
minimum-time-difference
[Python3] sort the time
ye15
1
141
minimum time difference
539
0.563
Medium
9,438
https://leetcode.com/problems/minimum-time-difference/discuss/710624/Python3-sort-based-solution-Minimum-Time-Difference
class Solution: def findMinDifference(self, time: List[str]) -> int: def minute(a): return int(a[:2]) * 60 + int(a[-2:]) time.sort() ans = 12 * 60 for i in range(len(time)): j = (i + 1) % len(time) ans = min(ans, (minute(time[j]) - minute(time[i]))...
minimum-time-difference
Python3 sort-based solution - Minimum Time Difference
r0bertz
1
327
minimum time difference
539
0.563
Medium
9,439
https://leetcode.com/problems/minimum-time-difference/discuss/2829819/SIMPLE-PYTHON-SOLUTION
class Solution: def toint(s): h=int(s[0:2]) m=int(s[3:5]) return int(h*60+m) def findMinDifference(self, t: List[str]) -> int: b=set(t) if(len(b)<len(t)): return 0 def toint(s): h=int(s[0:2]) m=int(s[3:5]) retu...
minimum-time-difference
SIMPLE PYTHON SOLUTION
_gajera__28_0
0
5
minimum time difference
539
0.563
Medium
9,440
https://leetcode.com/problems/minimum-time-difference/discuss/2597155/Python-Concise-but-readable-Solution
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: # get the minutes timePoints = list(map(self.get_minutes, timePoints)) # sort the time points timePoints.sort() # append the earlies value increased by one day (to get differ...
minimum-time-difference
[Python] - Concise but readable Solution
Lucew
0
32
minimum time difference
539
0.563
Medium
9,441
https://leetcode.com/problems/minimum-time-difference/discuss/2366803/Python-solution-(easy-to-understand)
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: timePoints.sort() minDiff = float("inf") for i in range(1, len(timePoints)): prev = timePoints[i - 1] curr = timePoints[i] if curr == prev: return 0 prev...
minimum-time-difference
Python solution (easy to understand)
lau125
0
43
minimum time difference
539
0.563
Medium
9,442
https://leetcode.com/problems/minimum-time-difference/discuss/2061993/Python-sorting
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: minutes = sorted(60*int(t[:2]) + int(t[3:]) for t in timePoints) diff = minutes[0] - minutes[-1] + 24 * 60 for i in range(len(minutes)-1): diff = min(diff, minutes[i+1] - minutes[i]) ...
minimum-time-difference
Python, sorting
blue_sky5
0
132
minimum time difference
539
0.563
Medium
9,443
https://leetcode.com/problems/minimum-time-difference/discuss/2039125/fast-python
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: times = [ (int(time.split(":")[0])*60 + int(time.split(":")[1])) for time in timePoints] times = sorted(times) for t in times.copy(): if t < 12*60: times.ap...
minimum-time-difference
fast python
hermesdt
0
108
minimum time difference
539
0.563
Medium
9,444
https://leetcode.com/problems/minimum-time-difference/discuss/1735026/python3-Solution-with-using-sorting
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: res = float('inf') timePoints.sort() timePoints[0] = int(timePoints[0][:2]) * 60 + int(timePoints[0][3:]) for idx in range(1, len(timePoints)): timePoints[idx] = int(time...
minimum-time-difference
[python3] Solution with using sorting
maosipov11
0
62
minimum time difference
539
0.563
Medium
9,445
https://leetcode.com/problems/minimum-time-difference/discuss/1321140/Beginner-Friendly
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: noDuplicates = set(timePoints) if len(noDuplicates) != len(timePoints): return 0 timePoints = [time.split(':') for time in timePoints] print(timePoints) timePoints.sort(key = lambda x:(int(...
minimum-time-difference
Beginner Friendly
WinstonWolfe07
0
163
minimum time difference
539
0.563
Medium
9,446
https://leetcode.com/problems/minimum-time-difference/discuss/908148/Python-easy-solution
class Solution: def findMinDifference(self, timePoints: List[str]) -> int: temp = [] for t in timePoints: hour, minute = t.split(":") temp.append(int(hour)*60+int(minute)) # additional check to count more than 24h ...
minimum-time-difference
Python easy solution
ermolushka2
0
167
minimum time difference
539
0.563
Medium
9,447
https://leetcode.com/problems/minimum-time-difference/discuss/413812/Python3-88-runtime-%2B-100-memory
class Solution: def time2min(self, t): return int(t[:2]) * 60 + int(t[3:]) def findMinDifference(self, timePoints: List[str]) -> int: new = [self.time2min(t) for t in timePoints] new.sort() offset = 60*24 new.extend([n+offset for n in new]) ...
minimum-time-difference
Python3 88% runtime + 100% memory
bear_sun
0
368
minimum time difference
539
0.563
Medium
9,448
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: counts = defaultdict(int) for num in nums: counts[num] += 1 for num, count in counts.items(): if count == 1: return num return -1 # this will never be reached # return Cou...
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,449
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: val, seen = -1, True for num in nums: if val == num: seen = True elif seen: val = num seen = False else: return val return...
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,450
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: for i in range(0, len(nums)-1, 2): # pairwise comparison if nums[i] != nums[i+1]: # found the single element return nums[i] return nums[-1] # the last element is the single element
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,451
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: result = 0 for i in range(len(nums)): if i%2: # alternate between +ve and -ve result -= nums[i] else: result += nums[i] return result # return sum((-1)**i*v for i,...
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,452
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: result = 0 for num in nums: result ^= num return result # return reduce(xor, nums) # one-liner
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,453
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: result = nums[0] for i in range(1, len(nums), 2): result += nums[i+1]-nums[i] return result # return nums[0] + sum(nums[i+1]-nums[i] for i in range(1, len(nums), 2)) # one-liner
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,454
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: result = nums[0] for i in range(1, len(nums), 2): result ^= nums[i]^nums[i+1] return result # return reduce(lambda x,i: x^nums[i]^nums[i+1], range(1, len(nums), 2), nums[0]) # one-liner
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,455
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo, hi = 0, len(nums)-1 while lo < hi: mid = lo+(hi-lo)//2 if nums[mid] == nums[mid-1]: # duplicate found if mid%2: # target > mid lo = mid+1 # exclude second ind...
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,456
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587293/Python-3-Simple-Approaches-with-Explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo, hi = 0, len(nums)-2 # hi starts from an even index so that hi^1 gives the next odd number while lo <= hi: mid = lo+(hi-lo)//2 if nums[mid] == nums[mid^1]: lo = mid+1 else: ...
single-element-in-a-sorted-array
[Python] 3 Simple Approaches with Explanation
zayne-siew
54
2,800
single element in a sorted array
540
0.585
Medium
9,457
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/627989/Python-O(lg-n)-by-binary-search-85%2B-w-Visualization
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: size = len(nums) left, right = 0, size // 2 while left < right: pair_index = left + ( right - left ) // 2 if nums[2*pair_index] != nums[2*pair_in...
single-element-in-a-sorted-array
Python O(lg n) by binary search 85%+ [w/ Visualization]
brianchiang_tw
7
714
single element in a sorted array
540
0.585
Medium
9,458
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1588059/Simple-One-Liner-or-Python
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: return Counter(nums).most_common()[-1][0]
single-element-in-a-sorted-array
Simple One Liner | Python
deep765
3
109
single element in a sorted array
540
0.585
Medium
9,459
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1588464/Simple-Python-Solution
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: c=0 for i in nums: if nums.count(i)==1: return i
single-element-in-a-sorted-array
Simple Python Solution
FaizanAhmed_
2
147
single element in a sorted array
540
0.585
Medium
9,460
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2833161/defaultdict-does-the-job
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: dc=defaultdict(lambda:0) for a in nums: dc[a]+=1 for a in dc: if(dc[a]==1): return a
single-element-in-a-sorted-array
defaultdict does the job
droj
0
1
single element in a sorted array
540
0.585
Medium
9,461
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2829267/Binary-search-on-two-conditions
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if mid > 0 and nums[mid] == nums[mid - 1]: if (mid - 1) % 2 == 0: left = mid + 1 ...
single-element-in-a-sorted-array
Binary search on two conditions
michaelniki
0
4
single element in a sorted array
540
0.585
Medium
9,462
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2810156/Simple-Python-Solution-%3A-Time-Complexity-%3A-O(LogN)-or-Space-Complexity-%3A-O(1)
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: start = 0 end = len(nums)-1 while start <= end: if start == end: return nums[start] mid = (start+end) // 2 if nums[mid] != nums[mid+1] and nums[mid]!= nums[mi...
single-element-in-a-sorted-array
Simple Python Solution : Time Complexity : O(LogN) | Space Complexity : O(1)
MaviOp
0
3
single element in a sorted array
540
0.585
Medium
9,463
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2804580/Simple-Python-SolutionororBinary-SearchororO(log2n)
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: n=len(nums) low=0 high=n-1 while(low <= high): mid = (low+high)//2 if(( mid==n-1 or nums[mid] != nums[mid+1] ) and ( mid==0 or nums[mid]!=nums[mid-1])): return(nums[mid]) ...
single-element-in-a-sorted-array
Simple Python Solution||Binary Search||O(log2n)
Ankit_Verma03
0
1
single element in a sorted array
540
0.585
Medium
9,464
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2802481/Python-Easy-to-understand-using-lambda
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: d = {} for word in nums: d[word] = d.get(word, 0) + 1 #print(d) res = sorted(d.keys(), key=lambda word: d[word]) #print(res) return res[0]
single-element-in-a-sorted-array
Python Easy to understand, using lambda
subbhashitmukherjee
0
2
single element in a sorted array
540
0.585
Medium
9,465
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2788513/Simple-python-binary-search-explanation
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo, hi = 0, len(nums) - 1 while lo < hi: mid = (lo + hi) // 2 if mid % 2 == 0: # make sure the number of elements between index 0 and index mid (inclusive) is always is even ...
single-element-in-a-sorted-array
Simple python binary search explanation
lister777
0
6
single element in a sorted array
540
0.585
Medium
9,466
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2753181/using-dictionary-python-3
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: dict={} for i in nums: if i not in dict: dict[i]=1 else: dict[i]+=1 for x,y in dict.items(): if y==1: return x
single-element-in-a-sorted-array
using dictionary python-3
Kevin7777777
0
2
single element in a sorted array
540
0.585
Medium
9,467
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2445714/Binary-search-problem-practise
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: low = 0 high = len(nums)-1 while(low<=high): mid = (low+high)//2 if(low == high): return nums[low] if(mid%2 == 0): if(nums[mid] == n...
single-element-in-a-sorted-array
Binary search problem practise
sandhya_vollala
0
36
single element in a sorted array
540
0.585
Medium
9,468
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2372119/Single-Element-in-a-Sorted-Array
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: count=collections.Counter(nums) for k,v in count.items(): if v==1:return k
single-element-in-a-sorted-array
Single Element in a Sorted Array
Faraz369
0
13
single element in a sorted array
540
0.585
Medium
9,469
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2346863/C%2B%2BPython-O(N)-to-O(logN)-Solution
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: res = 0 for i in range(len(nums)): res = res^nums[i] return res
single-element-in-a-sorted-array
C++/Python O(N) to O(logN) Solution
arpit3043
0
57
single element in a sorted array
540
0.585
Medium
9,470
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2346863/C%2B%2BPython-O(N)-to-O(logN)-Solution
class Solution(object): def singleNonDuplicate(self, nums): """ :type nums: List[int] :rtype: int """ left, right = 0, len(nums) - 1 while left + 1 < right: mid = (left + right) // 2 if mid % 2 == 1: if nums[mid] == nums[mid - 1...
single-element-in-a-sorted-array
C++/Python O(N) to O(logN) Solution
arpit3043
0
57
single element in a sorted array
540
0.585
Medium
9,471
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2284683/Python3-Binary-search
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 if right == 0: return nums[right] if nums[0]!=nums[1]: return nums[0] if nums[right] != nums[right-1]: return nums[right] while(left<=right...
single-element-in-a-sorted-array
Python3 Binary search
yashchandani98
0
19
single element in a sorted array
540
0.585
Medium
9,472
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2219612/Python-Solution-or-2-Approaches-or-TC%3A-O(n)-and-O(log(n))
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: # 1st Approach # TC: O(n) # xor=0 # for i in range(len(nums)): # xor^=nums[i] # return xor # 2nd Approach # using binary search # TC: O(log(n))...
single-element-in-a-sorted-array
Python Solution | 2 Approaches | TC:- O(n) and O(log(n))
Siddharth_singh
0
54
single element in a sorted array
540
0.585
Medium
9,473
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2192157/Easy-Approach
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: n = len(nums) i = 1 if n == 1: return nums[0] while i < n: if nums[i] != nums[i - 1]: return nums[i - 1] i += 2 return nums[i - 1]
single-element-in-a-sorted-array
Easy Approach
Vaibhav7860
0
50
single element in a sorted array
540
0.585
Medium
9,474
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2110027/Python-or-Two-Different-Solution-using-Binary-Search-and-hashmap
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: # ///////// Solution 2 TC: O(log N) ///////// l,r = 0, len(nums) - 1 while l < r: mid = (l+r)//2 mid = mid - 1 if mid % 2 == 1 else mid if nums[mid] == nums[mid+1]: ...
single-element-in-a-sorted-array
Python | Two Different Solution using Binary Search and hashmap
__Asrar
0
66
single element in a sorted array
540
0.585
Medium
9,475
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/2091061/Python-1-Liner-Solution
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: return reduce(operator.ixor, nums)
single-element-in-a-sorted-array
Python 1-Liner Solution
VictorSG
0
39
single element in a sorted array
540
0.585
Medium
9,476
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1922641/2-Python-Solutions
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: return Counter(nums).most_common()[-1][0]
single-element-in-a-sorted-array
2 Python Solutions
Taha-C
0
33
single element in a sorted array
540
0.585
Medium
9,477
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1922641/2-Python-Solutions
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: C=defaultdict(int) for num in nums: C[num]+=1 for num,c in C.items(): if c==1: return num
single-element-in-a-sorted-array
2 Python Solutions
Taha-C
0
33
single element in a sorted array
540
0.585
Medium
9,478
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1922641/2-Python-Solutions
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: nums.append(100001) for i in range(0,len(nums)-1,2): if nums[i]!=nums[i+1]: return nums[i]
single-element-in-a-sorted-array
2 Python Solutions
Taha-C
0
33
single element in a sorted array
540
0.585
Medium
9,479
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1922641/2-Python-Solutions
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: return sum((-1)**i*v for i,v in enumerate(nums))
single-element-in-a-sorted-array
2 Python Solutions
Taha-C
0
33
single element in a sorted array
540
0.585
Medium
9,480
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1922641/2-Python-Solutions
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo=0 ; hi=len(nums)-1 while lo<hi: mid=2*((lo+hi)//4) if nums[mid]==nums[mid+1]: lo=mid+2 else: hi=mid return nums[lo]
single-element-in-a-sorted-array
2 Python Solutions
Taha-C
0
33
single element in a sorted array
540
0.585
Medium
9,481
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1808800/Python-or-Xor-or-simple-or-basic
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: val=0 for i in range(len(nums)): val=val^nums[i] return val
single-element-in-a-sorted-array
Python | Xor | simple | basic
InvincibleTaki
0
30
single element in a sorted array
540
0.585
Medium
9,482
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1588736/100-fastest-log(n)-solution-with-clear-comments
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: # noting the low and high l, h = 0, len(nums) - 1 # boundary conditions if h == 0: return nums[0] elif nums[l] != nums[l+1]: return nums[l] elif nums[h] != nums[h-1]: ...
single-element-in-a-sorted-array
100% fastest - log(n) solution with clear comments
PSC_Crack
0
74
single element in a sorted array
540
0.585
Medium
9,483
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1588686/Python-3-binary-search-faster-than-99
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: n = len(nums) - 1 left, right = 0, n while left <= right: mid = (left + right) // 2 if (mid == 0 or nums[mid-1] != nums[mid]) and (mid == n or nums[mid+1] != nums[mid]): return nums...
single-element-in-a-sorted-array
Python 3 binary search faster than 99%
dereky4
0
116
single element in a sorted array
540
0.585
Medium
9,484
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587845/One-pass-with-step-2-99-speed
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: for i in range(0, len(nums) - 1, 2): if nums[i] != nums[i + 1]: return nums[i] return nums[-1]
single-element-in-a-sorted-array
One pass with step 2, 99% speed
EvgenySH
0
20
single element in a sorted array
540
0.585
Medium
9,485
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1587418/Python3-simple-soln.-0-sliding-window
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: dictN = {} # hashtable to store element reptition count if len(nums) == 1: #boundary condition . if length of nums is 1 then return 1 return 1 for j,i in enumerate(nums): if i not in dictN: dictN[i] = 0 #initiate count to 0 ...
single-element-in-a-sorted-array
Python3 simple soln. 0 sliding window
golden-eagle
0
16
single element in a sorted array
540
0.585
Medium
9,486
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1582712/Python-oror-Easy-Solution-oror-O(logn)-and-O(1)
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: if len(nums) == 1: return nums[0] l, r = 0, len(nums) - 1 while l < r: mid = l + (r - l) // 2 if nums[mid] == nums[mid + 1]: if (r - mid) % 2 == 0: l = mid + 2 else: r = mid - 1 elif nums[mid - 1] == nums[mid]...
single-element-in-a-sorted-array
Python || Easy Solution || O(logn) and O(1)
naveenrathore
0
99
single element in a sorted array
540
0.585
Medium
9,487
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1578323/Py3Py-Very-simple-solution-using-two-pointers-w-comments
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: # Init i = 0 j = len(nums) - 1 # Update till unique number not found while i < j: # Increment i if nums[i] == nums[i+1]: i = i+2 ...
single-element-in-a-sorted-array
[Py3/Py] Very simple solution using two pointers w/ comments
ssshukla26
0
44
single element in a sorted array
540
0.585
Medium
9,488
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1559678/Python3-Solution-with-using-binary-search
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 isOdd = (right - mid) % 2 != 0 # isOdd is flag that there is an odd number of elements in the left part relative to the ...
single-element-in-a-sorted-array
[Python3] Solution with using binary search
maosipov11
0
51
single element in a sorted array
540
0.585
Medium
9,489
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1456184/Simple-Python-O(logn)-binary-search-%2B-parity-check-solution
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: # monotonicity is a step function: # ___________ yes single # | # no single _______| # the problem boils down to determining whether # the single number exists to ...
single-element-in-a-sorted-array
Simple Python O(logn) binary search + parity check solution
Charlesl0129
0
96
single element in a sorted array
540
0.585
Medium
9,490
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1403185/Python-logn-solution-or-Easy-to-understand-or-SR
class Solution: def check(self, li, idx, n): if idx-1 >= 0 and li[idx-1] == li[idx]: return -1 elif idx+1 < n and li[idx+1] == li[idx]: return 1 else: return 0 #no left and no right same value def singleNonDuplicate(self, nums: List[int]) -> ...
single-element-in-a-sorted-array
Python logn solution | Easy to understand | SR
sathwickreddy
0
94
single element in a sorted array
540
0.585
Medium
9,491
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/1093564/Simple-Python3-Binary-search-solution
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: while 1 == 1: len_nums = len(nums) offset = 0 if len_nums == 1: return nums[0] mid = round(len_nums / 2) if nums[mid ...
single-element-in-a-sorted-array
Simple Python3 Binary search solution
olivierkessler
0
42
single element in a sorted array
540
0.585
Medium
9,492
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/873912/Python3-binary-search
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo, hi = 0, len(nums) while lo < hi: mid = lo + hi >> 1 if (mid == 0 or nums[mid - 1] != nums[mid]) and (mid+1 == len(nums) or nums[mid] != nums[mid+1]): return nums[mid] if nums[mid] == nums[m...
single-element-in-a-sorted-array
[Python3] binary search
ye15
0
69
single element in a sorted array
540
0.585
Medium
9,493
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/873912/Python3-binary-search
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: lo, hi = 0, len(nums) while lo < hi: mid = lo + hi >> 1 if mid^1 < len(nums) and nums[mid] == nums[mid^1]: lo = mid+1 else: hi = mid return nums[lo]
single-element-in-a-sorted-array
[Python3] binary search
ye15
0
69
single element in a sorted array
540
0.585
Medium
9,494
https://leetcode.com/problems/single-element-in-a-sorted-array/discuss/629392/Python-Recursive-solution-with-O(logn)-time-complexity-and-O(1)-space-complexity
class Solution: def singleNonDuplicate(self, nums: List[int]) -> int: return self.helper(nums) def helper(self, nums): if len(nums)==1: return nums[0] mid = len(nums)//2 if nums[mid]!=nums[mid-1] and nums[mid]!=nums[mid+1]: return nums[mid] el...
single-element-in-a-sorted-array
Python Recursive solution with O(logn) time complexity and O(1) space complexity
user8847C
0
28
single element in a sorted array
540
0.585
Medium
9,495
https://leetcode.com/problems/reverse-string-ii/discuss/343424/Python-3-solution-using-recursion-(efficient)-3-liner-with-explanation
class Solution: def reverseStr(self, s: str, k: int) -> str: if len(s)<(k):return s[::-1] if len(s)<(2*k):return (s[:k][::-1]+s[k:]) return s[:k][::-1]+s[k:2*k]+self.reverseStr(s[2*k:],k)
reverse-string-ii
Python 3 solution using recursion (efficient) 3-liner with explanation
ketan35
22
1,700
reverse string ii
541
0.505
Easy
9,496
https://leetcode.com/problems/reverse-string-ii/discuss/1017551/Simple-and-easy-faster-than-99.52
class Solution: def reverseStr(self, s: str, k: int) -> str: s=list(s) for i in range(0,len(s),2*k): if len(s[i:i+k])<k: s[i:i+k]=s[i:i+k][::-1] elif 2*k>len(s[i:i+k])>=k: s[i:i+k]=s[i:i+k][::-1] return "".join(s)
reverse-string-ii
Simple and easy - faster than 99.52%
thisisakshat
5
598
reverse string ii
541
0.505
Easy
9,497
https://leetcode.com/problems/reverse-string-ii/discuss/1977347/Python3-94-faster-with-explanation
class Solution: def reverseStr(self, s: str, k: int) -> str: news, ogLen = '', len(s) i = 0 while (i < ogLen) and s: if i % 2 == 0: news += s[:k][::-1] else: news += s[:k] s = s[k:] i += 1 return news
reverse-string-ii
Python3, 94% faster with explanation
cvelazquez322
2
302
reverse string ii
541
0.505
Easy
9,498
https://leetcode.com/problems/reverse-string-ii/discuss/1477226/Simple-or-Python-3-or-24-ms-faster-than-97.61
class Solution: def reverseStr(self, s: str, k: int) -> str: r = '' for i in range(0, len(s), k*2): r += s[i:i+k][::-1] + s[i+k:i+k+k] return r
reverse-string-ii
Simple | Python 3 | 24 ms, faster than 97.61%
deep765
2
205
reverse string ii
541
0.505
Easy
9,499