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/min-stack/discuss/2092529/Python3-Runtime%3A-55ms-98.00-O(1)
class MinStack: def __init__(self): self.stack = [] self.minStack = [] def push(self, val: int) -> None: self.stack.append(val) if len(self.minStack) == 0: self.minStack.append(val) elif val <= self.minStack[-1]: self.minStack.append(val) def pop(self) -> None: if self.stack[-1] == self.minStack[-1]: self.minStack.pop() self.stack.pop() def top(self) -> int: return self.stack[-1] def getMin(self) -> int: return self.minStack[-1]
min-stack
Python3 Runtime: 55ms 98.00% O(1)
mingwheel
0
123
min stack
155
0.519
Medium
2,500
https://leetcode.com/problems/min-stack/discuss/1987514/Python-or-99.96-faster-or-2-methods-with-O(1)-time-complexity-or-Complexity-Analysis
class MinStack: def __init__(self): self.stack = [] self.stackHash = {} def push(self, val: int) -> None: self.stack.append(val) self.stackHash[len(self.stack)-1] = min(self.stackHash.get(len(self.stack)-2, float('inf')), val) def pop(self) -> None: self.stack.pop() def top(self) -> int: return self.stack[-1] def getMin(self) -> int: return self.stackHash[len(self.stack)-1]
min-stack
Python | 99.96% faster | 2 methods with O(1) time complexity | Complexity Analysis
CSociety
0
166
min stack
155
0.519
Medium
2,501
https://leetcode.com/problems/min-stack/discuss/1987514/Python-or-99.96-faster-or-2-methods-with-O(1)-time-complexity-or-Complexity-Analysis
class MinStack: def __init__(self): self.stack = [] def push(self, val: int) -> None: # for first element if not self.stack: self.stack.append((val, val)) else: self.stack.append((val, min(self.getMin(), val))) def pop(self) -> None: self.stack.pop() def top(self) -> int: return self.stack[-1][0] def getMin(self) -> int: return self.stack[-1][1]
min-stack
Python | 99.96% faster | 2 methods with O(1) time complexity | Complexity Analysis
CSociety
0
166
min stack
155
0.519
Medium
2,502
https://leetcode.com/problems/min-stack/discuss/1970247/Simple-Python-Solution-oror-70-Faster-oror-Memory-less-than-60
class MinStack: def __init__(self): self.N=[] ; self.M=[] def push(self, val: int) -> None: self.N.append(val) self.M.append(val if not self.M else min(val,self.M[-1]) ) def pop(self) -> None: self.N.pop() ; self.M.pop() def top(self) -> int: return self.N[-1] def getMin(self) -> int: return self.M[-1]
min-stack
Simple Python Solution || 70% Faster || Memory less than 60%
Taha-C
0
132
min stack
155
0.519
Medium
2,503
https://leetcode.com/problems/min-stack/discuss/1912563/Python-solution-with-one-line-for-every-function
class MinStack: def __init__(self): self.stack = [] def push(self, val: int) -> None: self.stack.append(val) def pop(self) -> None: self.stack.pop(-1) def top(self) -> int: return self.stack[-1] def getMin(self) -> int: return min(self.stack) # Your MinStack object will be instantiated and called as such: # obj = MinStack() # obj.push(val) # obj.pop() # param_3 = obj.top() # param_4 = obj.getMin()
min-stack
Python solution with one line for every function
alishak1999
0
162
min stack
155
0.519
Medium
2,504
https://leetcode.com/problems/min-stack/discuss/1882731/Python-Simple-and-Elegant!-Multiple-Solutions!
class MinStack: def __init__(self): self.stack, self.minStack = [], [] def push(self, val): self.stack.append(val) self.minStack.append(val if not self.minStack else min(val, self.minStack[-1])) def pop(self): self.stack.pop() self.minStack.pop() def top(self): return self.stack[-1] def getMin(self): return self.minStack[-1]
min-stack
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
0
118
min stack
155
0.519
Medium
2,505
https://leetcode.com/problems/min-stack/discuss/1882731/Python-Simple-and-Elegant!-Multiple-Solutions!
class MinStack: def __init__(self): self.stack, self.minStack = [], [] def push(self, val): self.stack.append(val) if not self.minStack or val <= self.minStack[-1]: self.minStack.append(val) def pop(self): val = self.stack.pop() if val == self.minStack[-1]: self.minStack.pop() def top(self): return self.stack[-1] def getMin(self): return self.minStack[-1]
min-stack
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
0
118
min stack
155
0.519
Medium
2,506
https://leetcode.com/problems/min-stack/discuss/1882731/Python-Simple-and-Elegant!-Multiple-Solutions!
class MinStack: def __init__(self): self.stack = [] def push(self, val): self.stack.append((val,val) if not self.stack else (val,min(val,self.stack[-1][1]))) def pop(self): self.stack.pop() def top(self): return self.stack[-1][0] def getMin(self): return self.stack[-1][1]
min-stack
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
0
118
min stack
155
0.519
Medium
2,507
https://leetcode.com/problems/min-stack/discuss/1871125/Python-or-Approach-800%2B-ms-greater-76ms-TCO(1)-Optimized-solutionor-TCO(1)SCO(1)-or-Easy-to-understand
class MinStack(object): def __init__(self): self.stack = [] def push(self, val): return self.stack.append(val) def pop(self): return self.stack.pop() def top(self): return self.stack[-1] def getMin(self): return min(self.stack)
min-stack
Python | Approach = 800+ ms -> 76ms TC=O(1) Optimized solution| TC=O(1)/SC=O(1) | Easy to understand
Patil_Pratik
0
160
min stack
155
0.519
Medium
2,508
https://leetcode.com/problems/min-stack/discuss/1871125/Python-or-Approach-800%2B-ms-greater-76ms-TCO(1)-Optimized-solutionor-TCO(1)SCO(1)-or-Easy-to-understand
class MinStack(object): def __init__(self): self.stack = [] self.minStack = [] def push(self, val): newMin = val if self.minStack: lastMin = self.minStack[-1] newMin = min(lastMin, newMin) self.minStack.append(newMin) return self.stack.append(val) def pop(self): self.minStack.pop() return self.stack.pop() def top(self): return self.stack[-1] def getMin(self): return self.minStack[-1]
min-stack
Python | Approach = 800+ ms -> 76ms TC=O(1) Optimized solution| TC=O(1)/SC=O(1) | Easy to understand
Patil_Pratik
0
160
min stack
155
0.519
Medium
2,509
https://leetcode.com/problems/min-stack/discuss/1863037/Python-Straight-And-Easy-solution
class MinStack: def __init__(self): self.items = [] def push(self, val: int) -> None: self.items.append(val) def pop(self) -> None: self.items.pop(-1) def top(self) -> int: return self.items[-1] def getMin(self) -> int: return min(self.items)
min-stack
Python Straight And Easy solution
hardik097
0
124
min stack
155
0.519
Medium
2,510
https://leetcode.com/problems/min-stack/discuss/1840510/PYTHON3-EFFICIENT-SOLUTION-oror-BEATS-90
class MinStack: def __init__(self): self.stack = list() self.minStack = list() def push(self, val: int) -> None: #Normal Push into Stack self.stack.append(val) #check if minStack.top()>val, then push it. if not len(self.minStack) or self.minStack[-1]>=val: self.minStack.append(val) def pop(self) -> None: #Normal Pop from the Stack if len(self.stack): removed = self.stack.pop() #If removed element is minimum/ Top in minStack, then we'll pop it from minStack if len(self.minStack) and removed == self.minStack[-1]: self.minStack.pop() return def top(self) -> int: if len(self.stack): return self.stack[-1] return -1 def getMin(self) -> int: if len(self.minStack): return self.minStack[-1] return -1
min-stack
PYTHON3 EFFICIENT SOLUTION || BEATS 90%
yolo_man
0
93
min stack
155
0.519
Medium
2,511
https://leetcode.com/problems/min-stack/discuss/1687376/Python3-Simple-using-List
class MinStack: def __init__(self): self.myStack = list() self.min = math.inf def push(self, val: int) -> None: self.myStack.append(val) if val < self.min: self.min = val def pop(self) -> None: if len(self.myStack) == 0: return self.myStack.pop() if len(self.myStack) == 0: self.min = math.inf return self.min = min(self.myStack) def top(self) -> int: if len(self.myStack) == 0: return return self.myStack[-1] def getMin(self) -> int: if len(self.myStack) == 0: return return self.min
min-stack
Python3, Simple, using List
kukamble
0
73
min stack
155
0.519
Medium
2,512
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2116127/Python-oror-Easy-2-approaches-oror-O(1)-space
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: first_set=set() curr=headA while curr: first_set.add(curr) curr=curr.next curr = headB while curr: if curr in first_set: return curr curr=curr.next return None
intersection-of-two-linked-lists
Python || Easy 2 approaches || O(1) space
constantine786
75
5,200
intersection of two linked lists
160
0.534
Easy
2,513
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2116127/Python-oror-Easy-2-approaches-oror-O(1)-space
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: one = headA two = headB while one != two: one = headB if one is None else one.next two = headA if two is None else two.next return one
intersection-of-two-linked-lists
Python || Easy 2 approaches || O(1) space
constantine786
75
5,200
intersection of two linked lists
160
0.534
Easy
2,514
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1093186/Python.-Magic-solution.-O(n)-time.-O(1)-solution.
class Solution: def changeSign(self, head: ListNode): while ( head ): head.val *= -1 head = head.next def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: self.changeSign(headA) while ( headB ): if headB.val < 0:break headB = headB.next self.changeSign(headA) return headB
intersection-of-two-linked-lists
Python. Magic solution. O(n) time. O(1) solution.
m-d-f
20
1,600
intersection of two linked lists
160
0.534
Easy
2,515
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1056026/Python.-Super-simple-easy-and-clear-solution.-O(n)-time-O(1)-memory.
class Solution: def changeSign(self, head: ListNode): while ( head ): head.val *= -1 head = head.next def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: self.changeSign(headA) while ( headB ): if headB.val < 0:break headB = headB.next self.changeSign(headA) return headB
intersection-of-two-linked-lists
Python. Super simple, easy & clear solution. O(n) time, O(1) memory.
m-d-f
5
443
intersection of two linked lists
160
0.534
Easy
2,516
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/467993/Python-3-(five-lines)-(beats-~99)
class Solution: def getIntersectionNode(self, A: ListNode, B: ListNode) -> ListNode: S = set() while A != None: A, _ = A.next, S.add(A) while B != None: if B in S: return B B = B.next - Junaid Mansuri - Chicago, IL
intersection-of-two-linked-lists
Python 3 (five lines) (beats ~99%)
junaidmansuri
5
1,200
intersection of two linked lists
160
0.534
Easy
2,517
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1196770/Python-solution-O-(m%2Bn)-time-O(1)-memory
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: lenA = 0 lenB = 0 # calculate length for both linked lists currA = headA while currA: lenA = lenA+1 currA = currA.next currB = headB while currB: lenB = lenB + 1 currB = currB.next # reset pointer back to start of the linked lists currA = headA currB = headB # the possibility of the intersection (if any) will be within the length of the shorter linked list # so skip through the longer list until the lengths match if lenA > lenB: while lenA != lenB: lenA = lenA-1 currA = currA.next else: while lenA != lenB: lenB = lenB-1 currB = currB.next # once the remaining lengths for both linked lists are same, compare each of the nodes while lenA > 0: if currA == currB: return currA currA = currA.next currB = currB.next lenA = lenA-1 return
intersection-of-two-linked-lists
Python solution, O (m+n) time, O(1) memory
_choksi
4
477
intersection of two linked lists
160
0.534
Easy
2,518
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/366774/Python-multiple-solutions
class Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ node1 = headA node2 = headB while node1 != node2: node1 = node1.next if node1 else headB node2 = node2.next if node2 else headA
intersection-of-two-linked-lists
Python multiple solutions
amchoukir
4
1,000
intersection of two linked lists
160
0.534
Easy
2,519
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/366774/Python-multiple-solutions
class Solution(object): def _length(self, head): count = 0 while head: count += 1 head = head.next return count def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ lenA = self._length(headA) lenB = self._length(headB) (headS, lenS), (headL, lenL) = sorted([(headA, lenA), (headB, lenB)], key=lambda x: x[1]) diff = lenL - lenS while diff: headL = headL.next diff -= 1 while headS != headL: headS = headS.next headL = headL.next return headS
intersection-of-two-linked-lists
Python multiple solutions
amchoukir
4
1,000
intersection of two linked lists
160
0.534
Easy
2,520
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/366774/Python-multiple-solutions
class Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ cache = set() while headA: cache.add(headA) headA = headA.next while headB: if headB in cache: return headB headB = headB.next return None
intersection-of-two-linked-lists
Python multiple solutions
amchoukir
4
1,000
intersection of two linked lists
160
0.534
Easy
2,521
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2278151/Simple-Python-Solution.-Beats-96.-with-comments
class Solution(object): def getIntersectionNode(self, headA, headB): seen = set() #Create a set for seen nodes while headA or headB: if headA: if headA in seen: # If B has passed the node previously, then this is the intersect return headA else: seen.add(headA) # record the node if not previously seen headA = headA.next if headB: if headB in seen: return headB else: seen.add(headB) headB = headB.next return # if no intersect, return None #If you find this solution helpful, please upvote :)
intersection-of-two-linked-lists
Simple Python Solution. Beats 96%. with comments
cz2105
3
203
intersection of two linked lists
160
0.534
Easy
2,522
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2118895/Python-oror-O(N)
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: # calculate length of lists a = headA alen = 0 while a: alen += 1 a = a.next b = headB blen = 0 while b: blen += 1 b = b.next # check which list is longer s = None g = None if alen > blen: g = headA s = headB else: g = headB s = headA # equalize lengths for i in range(max(alen, blen) - min(alen, blen)): g = g.next # check intersection node wise while s and g: if s == g: return s s = s.next g = g.next return None
intersection-of-two-linked-lists
Python || O(N)
Pootato
3
159
intersection of two linked lists
160
0.534
Easy
2,523
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2025817/Python-Easy-2-Pointers-Solution
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: a = headA b = headB loopCount = 0 # if no intersection while True: if a == b: return a a = a.next b = b.next if not a: a = headB; loopCount += 1 if not b: b = headA if loopCount > 1: return None # Time: O(n + m) # Space: O(1)
intersection-of-two-linked-lists
Python Easy 2 Pointers Solution
samirpaul1
3
164
intersection of two linked lists
160
0.534
Easy
2,524
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1554184/faster-than-82.67%3A-Python-O(1)-and-O(n)-space
class Solution: # O(n) Space def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: hashset = set() curr = headA while curr: hashset.add(curr) curr = curr.next curr = headB while curr: if curr in hashset: return curr curr = curr.next return None # O(1) Space def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: sizeA = self.count(headA) sizeB = self.count(headB) if sizeA > sizeB: diff = sizeA - sizeB return self.get_result(diff, headA, headB) else: diff = sizeB - sizeA return self.get_result(diff, headB, headA) def get_result(self, diff, long_node, short_node): while diff: diff -= 1 long_node = long_node.next while long_node != short_node: if long_node is None or short_node is None: return None long_node = long_node.next short_node = short_node.next return long_node def count(self, node): count = 0 while node: count += 1 node = node.next return count
intersection-of-two-linked-lists
faster than 82.67%: Python O(1) and O(n) space
SleeplessChallenger
3
468
intersection of two linked lists
160
0.534
Easy
2,525
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2151429/Python-Solution-100
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: a=headA b=headB while a!=b: if a==None: a=headB else: a=a.next if b==None: b=headA else: b=b.next return a
intersection-of-two-linked-lists
Python Solution 100%
Siddharth_singh
2
243
intersection of two linked lists
160
0.534
Easy
2,526
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1401392/Python-or-Constant-Memory-or-O(N)-time-or-O(1)-space
class Solution: def getLength(self,head): cnt = 0 while head!=None: cnt+=1 head = head.next return cnt def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: n1, n2 = self.getLength(headA), self.getLength(headB) h1,h2 = headA, headB ans = None if n2 > n1: h1, h2 = h2, h1 n1, n2 = n2, n1 #now n1 >= n2 skip_h1 = n1-n2 for i in range(skip_h1): h1 = h1.next while h1 != None: if h1==h2: return h1 h1 = h1.next h2 = h2.next return None
intersection-of-two-linked-lists
Python | Constant Memory | O(N) time | O(1) space
sathwickreddy
2
259
intersection of two linked lists
160
0.534
Easy
2,527
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1401392/Python-or-Constant-Memory-or-O(N)-time-or-O(1)-space
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: h1, h2 = headA, headB if h1 == None or h2 == None: return None while h1!=h2: h1 = headB if h1==None else h1.next h2 = headA if h2==None else h2.next #even if no intersection they will equal at some point of time i.e., h1=None, h2=None #at max 2 passess will be happened return h1
intersection-of-two-linked-lists
Python | Constant Memory | O(N) time | O(1) space
sathwickreddy
2
259
intersection of two linked lists
160
0.534
Easy
2,528
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1092876/Python3-Easy-soln-for-Intersection-of-Two-Linked-Lists-O(n)-time
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: pointer1 = headA pointer2 = headB while pointer1 != pointer2: #if pointers are not the same if pointer1: #if pointer1 is not at the end, it advances by one step pointer1 = pointer1.next else: #if pointer1 is at the end, it becomes headB pointer1 = headB #same should be done for pointer2 if pointer2: #if pointer1 is not at the end, it advances by one step pointer2 = pointer2.next else: #if pointer1 is at the end, it becomes headB pointer2 = headA return pointer1 #if the pointers are te same
intersection-of-two-linked-lists
[Python3] Easy soln for Intersection of Two Linked Lists, O(n) time
avEraGeC0der
2
142
intersection of two linked lists
160
0.534
Easy
2,529
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2366113/Python-oror-Easy-oror-O(m%2Bn)-time-oror-O(1)-space
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: m = 0 n = 0 temp = headA while temp != None: m+=1 temp = temp.next temp = headB while temp != None: n+=1 temp = temp.next diff = 0 if m>=n : diff = m-n else: diff = n-m p1 = headA p2 = headB if max(m,n) == m: while diff > 0: p1 = p1.next diff-=1 else: while diff > 0: p2 = p2.next diff-=1 while p1 != None and p2!=None: if p1 == p2: return p1 p1 = p1.next p2 = p2.next return None
intersection-of-two-linked-lists
Python || Easy || O(m+n) time || O(1) space
nitisanand
1
220
intersection of two linked lists
160
0.534
Easy
2,530
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2126395/Python-Simple-Python-Solution-Using-Difference-of-Node-Counts
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: total_node_A = 0 total_node_B = 0 new_headA = headA new_headB = headB while new_headA: total_node_A = total_node_A + 1 new_headA = new_headA.next while new_headB: total_node_B = total_node_B + 1 new_headB = new_headB.next diffrence = total_node_A - total_node_B if diffrence < 0: count = 0 while count < abs(diffrence): count = count + 1 headB = headB.next else: count = 0 while count < diffrence: count = count + 1 headA = headA.next while headA and headB: if headA is headB: return headA headA = headA.next headB = headB.next return None
intersection-of-two-linked-lists
[ Python ] ✅✅ Simple Python Solution Using Difference of Node Counts 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
1
154
intersection of two linked lists
160
0.534
Easy
2,531
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2117785/java-python-easy-linear-solution-(space-O1)
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: if headA == None or headB == None : return None lena, lenb = 0, 0 w = headA while w.next != None : w, lena = w.next, lena+1 w = headB while w.next != None : w, lenb = w.next, lenb+1 i, shift = 0, lena-lenb while i < shift : headA, i = headA.next, i+1 i, shift = 0, lenb-lena while i < shift : headB, i = headB.next, i+1 while headA != headB : headA, headB = headA.next, headB.next return headA
intersection-of-two-linked-lists
java, python - easy linear solution (space O1)
ZX007java
1
84
intersection of two linked lists
160
0.534
Easy
2,532
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2117192/Python-easy-solution-for-beginners-using-seen-set
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: seen = set() while headA: seen.add(headA) headA = headA.next while headB: if headB in seen: return headB seen.add(headB) headB = headB.next
intersection-of-two-linked-lists
Python easy solution for beginners using seen set
alishak1999
1
110
intersection of two linked lists
160
0.534
Easy
2,533
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1740195/only-3-lines-of-operation-or-easy-to-understand-or-O(1)-space
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: if headA == None or headB == None: return None n1 = headA n2 = headB while n1 != n2: n1 = headB if n1 == None else n1.next n2 = headA if n2 == None else n2.next return n1
intersection-of-two-linked-lists
only 3 lines of operation | easy to understand | O(1) space
prankurgupta18
1
92
intersection of two linked lists
160
0.534
Easy
2,534
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1188963/Runtime%3A-faster-than-74-.Memory-Usage%3A-less-than-95-of-Python3
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: listA = headA listB = headB semaphore = 2 while(listA or listB): if semaphore == 0 or listA == listB: break if listA: listA = listA.next else: listA = headB semaphore -= 1 if listB: listB = listB.next else: listB = headA semaphore -= 1 while(listA or listB): if listA is listB: return listA listA = listA.next listB = listB.next return listA
intersection-of-two-linked-lists
Runtime: faster than 74% .Memory Usage: less than 95% of Python3
BanarasiVaibhav
1
178
intersection of two linked lists
160
0.534
Easy
2,535
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1188963/Runtime%3A-faster-than-74-.Memory-Usage%3A-less-than-95-of-Python3
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: len_headA = self.count_length(headA) len_headB = self.count_length(headB) for i in range(len_headA - len_headB): headA = headA.next for i in range(len_headB - len_headA): headB = headB.next while(headA): if headA is headB: return headA headA=headA.next headB=headB.next return headA def count_length(self,some_head): count = 0 while(some_head): count += 1 some_head=some_head.next return count
intersection-of-two-linked-lists
Runtime: faster than 74% .Memory Usage: less than 95% of Python3
BanarasiVaibhav
1
178
intersection of two linked lists
160
0.534
Easy
2,536
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1092856/Python-Using-Dictionaries-or-Easy-Solution
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: d={} temp = headA tempB = headB # Storing all links of A while temp!=None: d[temp]=1 temp = temp.next # Checking if any of the links of B are present in the Dict. while tempB!= None: if tempB in d: return tempB tempB = tempB.next return
intersection-of-two-linked-lists
Python - Using Dictionaries | Easy Solution
adz0612
1
37
intersection of two linked lists
160
0.534
Easy
2,537
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1080397/Python-O(n)-easy-to-understand
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: alen = 0 blen = 0 ptra = headA ptrb = headB #2 while loops to find the length of the linked list while ptra: alen+=1 ptra= ptra.next while ptrb: blen +=1 ptrb = ptrb.next if alen==0 or blen==0: return None dif = abs(alen-blen) #difference between their lengths #below 2 if statements make the length of both the linked list equal by deleting the starting node of the larger linked list if alen>blen: while dif: headA = headA.next dif-=1 elif blen>alen: while dif: headB= headB.next dif-=1 #iterate through both the linked list together and once they point to the same node return that node while headA and headB: if headA == headB: return headA headA = headA.next headB = headB.next #if there is no common node return None return None
intersection-of-two-linked-lists
Python O(n) easy to understand
abhishekm2106
1
298
intersection of two linked lists
160
0.534
Easy
2,538
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/642385/JavaPython3-traverse-A-greaterB-and-B-greaterA-at-the-same-time
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: nodeA, nodeB = headA, headB while nodeA != nodeB: nodeA = nodeA.next if nodeA else headB nodeB = nodeB.next if nodeB else headA return nodeA
intersection-of-two-linked-lists
[Java/Python3] traverse A->B and B->A at the same time
ye15
1
124
intersection of two linked lists
160
0.534
Easy
2,539
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/250387/two-pointer-python-solution
class Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """ curA = headA curB = headB while curA != curB: curA = curA.next if curA else headB curB = curB.next if curB else headA return curA
intersection-of-two-linked-lists
two pointer python solution
mazheng
1
278
intersection of two linked lists
160
0.534
Easy
2,540
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2831326/python-oror-simple-solution-oror-o(1)-memory
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: itrA, itrB = headA, headB # iterators for A and B # iterate through A+B and B+A simultaneously, first shared node is the intersection while itrA is not itrB: # if end of either reached, loop back to beginning of other linkedlist itrA = itrA.next if itrA else headB itrB = itrB.next if itrB else headA # return either intersection node or null if end of headB reached return itrA
intersection-of-two-linked-lists
python || simple solution || o(1) memory
wduf
0
6
intersection of two linked lists
160
0.534
Easy
2,541
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2784624/Bruteforce-Method
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: # get the lenght of the two heads d1, d2 = ListNode(0, headA), ListNode(0, headB) l1, l2 = 0,0 while d1: d1 = d1.next l1 += 1 while d2: d2 = d2.next l2 += 1 if l1 > l2: while l1 > l2: headA = headA.next l1 -= 1 else: while l2 > l1: headB = headB.next l2 -= 1 while headA and headB: if headA == headB: return headA headA, headB = headA.next, headB.next return None
intersection-of-two-linked-lists
Bruteforce Method
keshan-spec
0
2
intersection of two linked lists
160
0.534
Easy
2,542
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2773191/Using-a-dictionary-Python3
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: node_dict = {} node = headA while node: node_dict[node] = True node = node.next node = headB while node: if node in node_dict: return node node = node.next return None
intersection-of-two-linked-lists
Using a dictionary - Python3
divesh-panwar
0
4
intersection of two linked lists
160
0.534
Easy
2,543
https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/2702220/Python3-Easy-and-fast-Sol-90
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: Set=set() curr= headA while curr: Set.add(curr) curr=curr.next curr=headB while curr: if curr in Set: return curr curr=curr.next return None
intersection-of-two-linked-lists
Python3 Easy and fast Sol 90%
pranjalmishra334
0
7
intersection of two linked lists
160
0.534
Easy
2,544
https://leetcode.com/problems/find-peak-element/discuss/1440424/Python-oror-Easy-Solution
class Solution: def findPeakElement(self, lst: List[int]) -> int: start, end = 0, len(lst) - 1 while start < end: mid = start + (end - start) // 2 if lst[mid] > lst[mid + 1]: end = mid else: start = mid + 1 return start
find-peak-element
Python || Easy Solution
naveenrathore
8
619
find peak element
162
0.462
Medium
2,545
https://leetcode.com/problems/find-peak-element/discuss/2120747/PYTHON-oror-O-(-LOG-N-)-oror-EXPANATION
class Solution: def findPeakElement(self, nums: List[int]) -> int: if len(nums)==1: return 0 if nums[0]>nums[1]: return 0 if nums[-1]>nums[-2]: return len(nums)-1 l=1 h=len(nums)-1 while l<h: m=(l+h)//2 if nums[m-1]<nums[m]>nums[m+1]: return m elif nums[m-1]<nums[m]<nums[m+1]: l=m+1 else: h=m return l
find-peak-element
✔️ PYTHON || O ( LOG N ) || EXPANATION
karan_8082
4
346
find peak element
162
0.462
Medium
2,546
https://leetcode.com/problems/find-peak-element/discuss/1335946/Linear-time-Solution(python)
class Solution: def findPeakElement(self, nums: List[int]) -> int: for i in range(len(nums)-1): if nums[i]>nums[i+1]: return i return len(nums)-1
find-peak-element
Linear time Solution(python)
_jorjis
3
139
find peak element
162
0.462
Medium
2,547
https://leetcode.com/problems/find-peak-element/discuss/2670585/Python-solution-or-Binary-search
class Solution: def findPeakElement(self, nums: List[int]) -> int: left = 0 right = len(nums) - 1 if len(nums) == 1: return 0 while right > left: mid = (left + right)//2 if nums[mid] > nums[mid-1] and nums[mid] > nums[mid+1]: return mid if nums[mid] < nums[mid+1]: left = mid + 1 else: right = mid - 1 return right if nums[right] > nums[mid] else mid
find-peak-element
Python solution | Binary search
maomao1010
1
575
find peak element
162
0.462
Medium
2,548
https://leetcode.com/problems/find-peak-element/discuss/2185102/Super-simple-easy-to-understand-python-one-liner
class Solution: def findPeakElement(self, nums: List[int]) -> int: return nums.index(max(nums))
find-peak-element
Super simple, easy to understand, python one-liner
pro6igy
1
113
find peak element
162
0.462
Medium
2,549
https://leetcode.com/problems/find-peak-element/discuss/2000054/Easy-one-liner-Python-faster-than-88.55-of-Python3-online-submissions
class Solution: def findPeakElement(self, nums: List[int]) -> int: return nums.index(max(nums))
find-peak-element
Easy one liner Python faster than 88.55% of Python3 online submissions
James-Kosinar
1
110
find peak element
162
0.462
Medium
2,550
https://leetcode.com/problems/find-peak-element/discuss/1772943/Python-Code-or-99-Faster-or-O(log-N)
class Solution: def findPeakElement(self, arr: List[int]) -> int: n=len(arr) l,e=0,n-1 while l<e: mid=l+(e-l)//2 if arr[mid]>=arr[mid+1] and arr[mid]>=arr[mid-1]: return mid else: if arr[mid]<arr[mid+1]: l=mid+1 elif arr[mid]<arr[mid-1]: e=mid-1 return l
find-peak-element
Python Code | 99% Faster | O(log N)
rackle28
1
96
find peak element
162
0.462
Medium
2,551
https://leetcode.com/problems/find-peak-element/discuss/1431737/WEEB-DOES-PYTHON
class Solution: def findPeakElement(self, nums: List[int]) -> int: low, high = 0, len(nums)-1 while low<high: mid = low + (high-low)//2 if nums[mid] < nums[mid+1]: low = mid + 1 else: high = mid return low
find-peak-element
WEEB DOES PYTHON
Skywalker5423
1
134
find peak element
162
0.462
Medium
2,552
https://leetcode.com/problems/find-peak-element/discuss/724744/Python3-binary-search
class Solution: def findPeakElement(self, nums: List[int]) -> int: lo, hi = 0, len(nums)-1 while lo < hi: mid = lo + hi >> 1 if nums[mid] < nums[mid+1]: lo = mid+1 else: hi = mid return lo
find-peak-element
[Python3] binary search
ye15
1
100
find peak element
162
0.462
Medium
2,553
https://leetcode.com/problems/find-peak-element/discuss/397476/Solution-in-Python-3-(beats-~100)
class Solution: def findPeakElement(self, N: List[int]) -> int: L, _ = len(N), N.append(-math.inf) for i in range(L): if N[i] > N[i+1]: return i - Junaid Mansuri (LeetCode ID)@hotmail.com
find-peak-element
Solution in Python 3 (beats ~100%)
junaidmansuri
1
331
find peak element
162
0.462
Medium
2,554
https://leetcode.com/problems/find-peak-element/discuss/2847773/Python-binary-search-beat-90
class Solution: def findPeakElement(self, nums: List[int]) -> int: start=0 end=len(nums)-1 while(start<end): mid=(start+end)//2 if(nums[mid]<nums[mid+1]): start=mid+1 else: end=mid return start
find-peak-element
Python binary search beat 90%
Akash2907
0
1
find peak element
162
0.462
Medium
2,555
https://leetcode.com/problems/find-peak-element/discuss/2829136/O(n)-solution-in-Python
class Solution: def findPeakElement(self, nums: List[int]) -> int: m = max(nums) for i in range(len(nums) + 1): if nums[i] == m: return i
find-peak-element
O(n) solution in Python
123liamspillane
0
1
find peak element
162
0.462
Medium
2,556
https://leetcode.com/problems/find-peak-element/discuss/2821965/Python-O(log(n))-solution-Accepted
class Solution: def findPeakElement(self, nums: List[int]) -> int: if len(nums) == 1: return 0 left: int = 0 right: int = len(nums) - 1 while left <= right: if right == len(nums) - 1: if nums[right - 1] < nums[right]: return right else: if nums[right - 1] < nums[right] and nums[right + 1] < nums[right]: return right if left == 0: if nums[left + 1] < nums[left]: return left else: if nums[left - 1] < nums[left] and nums[left + 1] < nums[left]: return left left += 1 right -= 1
find-peak-element
Python O(log(n)) solution [Accepted]
lllchak
0
3
find peak element
162
0.462
Medium
2,557
https://leetcode.com/problems/find-peak-element/discuss/2810485/Easy-Python-solution-EXPLAINED
class Solution: def findPeakElement(self, nums: List[int]) -> int: n = len(nums) left = 0 right = n - 1 while left <= right: mid = (right + left) // 2 l = nums[mid - 1] if mid > 0 else -float('inf') r = nums[mid + 1] if mid < n - 1 else -float('inf') if l < nums[mid] and nums[mid] > r: return mid elif nums[mid] < r: left = mid + 1 else: right = mid - 1
find-peak-element
Easy Python solution [EXPLAINED]
tleuliyev
0
5
find peak element
162
0.462
Medium
2,558
https://leetcode.com/problems/find-peak-element/discuss/2799432/Simple-python-codeororBinary-SearchororO(log2n)
class Solution: def findPeakElement(self, nums: List[int]) -> int: n=len(nums) low=0 high=n-1 if(low==high): return low while(low<=high): mid=(low+high)//2 if(mid==0): if nums[mid]>nums[mid+1]: return mid else: low=mid+1 elif (mid==len(nums)-1): if nums[mid]>nums[mid-1]: return mid else: high=mid-1 elif(mid>0 and mid <n-1 and nums[mid+1]<nums[mid] and nums[mid-1]<nums[mid]): return mid elif(mid>0 and nums[mid]<nums[mid-1]): high=mid-1 elif(mid<n-1 and nums[mid]<nums[mid+1]): low=mid+1 return -1
find-peak-element
Simple python code||Binary Search||O(log2n)
Ankit_Verma03
0
2
find peak element
162
0.462
Medium
2,559
https://leetcode.com/problems/find-peak-element/discuss/2796813/Python-or-Simple-or-Binary-Search-or-O(log-n)
class Solution: def findPeakElement(self, nums: List[int]) -> int: start, end = 0, len(nums) - 1 while start < end: mid = start + (end - start) // 2 if nums[mid] > nums[mid+1]: end = mid else: start = mid + 1 return start
find-peak-element
Python | Simple | Binary Search | O(log n)
david-cobbina
0
4
find peak element
162
0.462
Medium
2,560
https://leetcode.com/problems/find-peak-element/discuss/2793534/Python3-Straightforward-Binary-Search
class Solution: def findPeakElement(self, nums: List[int]) -> int: l, r = 0, len(nums)-1 while l <= r: mid = (l+r)//2 # midpoint is peak if ((mid-1 < 0 or nums[mid] > nums[mid-1]) and (mid+1 == len(nums) or nums[mid] > nums[mid+1])): return mid # peak is in the left partition elif (mid-1 >=0 and (mid+1 == len(nums) or (nums[mid-1] > nums[mid+1]))): r = mid-1 # peak is in the right partition else: l = mid+1
find-peak-element
[Python3] Straightforward Binary Search
jonathanbrophy47
0
2
find peak element
162
0.462
Medium
2,561
https://leetcode.com/problems/find-peak-element/discuss/2750381/Funny-python
class Solution: def findPeakElement(self, nums: List[int]) -> int: return nums.index(max(nums))
find-peak-element
Funny python
rajubuet24
0
2
find peak element
162
0.462
Medium
2,562
https://leetcode.com/problems/find-peak-element/discuss/2745441/python-binary-search-O(log(n))
class Solution: def findPeakElement(self, nums: List[int]) -> int: # O(log n), O(1) l, r = 0, len(nums) - 1 while l < r: mid = l + (r-l)//2 if nums[mid] < nums[mid + 1]: l = mid + 1 else: r = mid return l
find-peak-element
python binary search O(log(n))
sahilkumar158
0
1
find peak element
162
0.462
Medium
2,563
https://leetcode.com/problems/find-peak-element/discuss/2740091/Simple-Beginner-Friendly-Approach-or-One-Pass-or-no-built-in-function-or-O(LogN)-or-O(1)
class Solution: def findPeakElement(self, nums: List[int]) -> int: left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if left == mid: if nums[left] >= nums[right]: return left else: return right elif nums[mid] < nums[mid + 1]: left = mid + 1 elif nums[mid] < nums[mid - 1]: right = mid - 1 elif nums[mid] > nums[mid - 1] and nums[mid] > nums[mid + 1]: return mid
find-peak-element
Simple Beginner Friendly Approach | One Pass | no built-in function | O(LogN) | O(1)
sonnylaskar
0
2
find peak element
162
0.462
Medium
2,564
https://leetcode.com/problems/find-peak-element/discuss/2737245/Iterative-python3-solution-with-documentation-TC%3A-O(log2n)-SC%3A-O(1)
class Solution: def findPeakElement(self, nums: List[int]) -> int: ## SC O(1) ## TC O(log2n) # Extreme cases, array with one and two elements if len(nums) == 1: return 0 if len(nums) == 2: return 0 if nums[0] > nums[1] else 1 # binary search alogorithm left = 0 right = len(nums) - 1 while left < right: # find mid value M = left + (right-left)//2 # peak is found nums[M-1] < nums[M] > nums[M+1] if nums[M] > nums[M-1] and nums[M] > nums[M+1]: return M # divide remaining array in 2 and # move right to M-1 if nums[M-1] > nums[M] > nums[M+1] if nums[M] < nums[M-1] and nums[M] > nums[M+1]: right = M -1 # move left to M+1 if nums[M-1] < nums[M] < nums[M+1] # and if nums[M-1] > nums[M] < nums[M+1] - > arbitrary decision, you could also # add a new case where nums[M-1] > nums[M] < nums[M+1] and decide if go left or right else: left = M + 1 # end of loop left == right and num[right] is at a peak return right
find-peak-element
Iterative python3 solution with documentation TC: O(log2n) SC: O(1)
MPoinelli
0
2
find peak element
162
0.462
Medium
2,565
https://leetcode.com/problems/find-peak-element/discuss/2678548/Python-Solution-beats-runtime-by-93.44-using-Binary-Search
class Solution: def findPeakElement(self, nums: List[int]) -> int: start = 0 end = len(nums) - 1 while start <= end: mid = (start + end) // 2 if ((mid == 0) or (nums[mid] > nums[mid-1])) and ((mid == len(nums) - 1) or (nums[mid] > nums[mid+1])) : return mid elif (mid==0 or nums[mid]>nums[mid-1]): start = mid + 1 else: end = mid - 1 return -1
find-peak-element
Python Solution beats runtime by 93.44% using Binary Search
user2385PN
0
7
find peak element
162
0.462
Medium
2,566
https://leetcode.com/problems/find-peak-element/discuss/2674449/Python-Binary-Search-or-With-some-explanation
class Solution: def findPeakElement(self, nums: List[int]) -> int: # Handle edge cases for 1 or 2 element arrays if len(nums) == 1: return 0 n = len(nums) if nums[0] > nums[1]: return 0 if nums[-1] > nums[-2]: return n - 1 start = 0 end = len(nums) - 1 while start <= end: mid = (start + end) // 2 if nums[mid] > nums[mid - 1] and nums[mid] > nums[mid + 1]: return mid elif nums[mid] < nums[mid + 1]: start = mid + 1 else: end = mid - 1
find-peak-element
Python - Binary Search | With some explanation
saadbash
0
4
find peak element
162
0.462
Medium
2,567
https://leetcode.com/problems/find-peak-element/discuss/2674035/Easy-and-clean-Python-solution
class Solution: def findPeakElement(self, nums: List[int]) -> int: TOWARD_RIGHT = -1 # look like this: / PEAK = 0 # look like this: ^ TOWARD_LEFT = 1 # look like this: \ def explore_neighbors(pos: int): # three status: toward left, right and peak cur = nums[pos] left = float('-inf') if pos == 0 else nums[pos-1] right = float('-inf') if pos == len(nums) else nums[pos+1] if cur > left and cur > right: return PEAK elif cur < left: return TOWARD_LEFT else: return TOWARD_RIGHT l, r = 0, len(nums) - 1 while l < r: mid = (l + r) // 2 mid_status = explore_neighbors(mid) if mid_status == PEAK: return mid elif mid_status == TOWARD_RIGHT: l = mid + 1 else: r = mid return l
find-peak-element
Easy and clean Python solution
Starboy69
0
1
find peak element
162
0.462
Medium
2,568
https://leetcode.com/problems/find-peak-element/discuss/2645257/Python-explanation-with-graph-explanation
class Solution: # Append the nums with -infinity and prepend the nums with -infinity as per the question # This ensures atleast one peak will always exist # Think of 3 graphs before approaching the problem # For the first and the last graph we can just keep on increasing or decresing the l as per the binary search algo # The same applies to multiple peaks on right and left # For the middle graph if such a case or multiple peak case happens by default the first peak can be found easily # just by decreasing l as per the binary search algo def findPeakElement(self, nums: List[int]) -> int: l, r = 0, len(nums) - 1 while l < r: m = l + ((r - l) >> 1) if nums[m] > nums[m + 1]: r = m else: l = m + 1 return
find-peak-element
Python explanation with graph explanation
shiv-codes
0
47
find peak element
162
0.462
Medium
2,569
https://leetcode.com/problems/find-peak-element/discuss/2498096/Easy-Python-Solution-or-Binary-Search
class Solution: def findPeakElement(self, nums: List[int]) -> int: if len(nums)==1: return 0 if nums[0]>nums[1]: return 0 if nums[-1]>nums[-2]: return len(nums)-1 low=0 high=len(nums)-1 while low<=high: mid=(low+high)//2 if mid>0 and mid<len(nums)-1 and nums[mid]>nums[mid+1] and nums[mid]>nums[mid-1]: return mid if mid>0 and nums[mid-1]>nums[mid]: high=mid-1 else: low=mid+1 return -1
find-peak-element
Easy Python Solution | Binary Search
Siddharth_singh
0
119
find peak element
162
0.462
Medium
2,570
https://leetcode.com/problems/find-peak-element/discuss/2485302/Python3-or-Solved-Using-Binary-Search-With-Simple-Intuition
class Solution: #Time-Complexity: O(logn) #Space-Complexity: O(1) def findPeakElement(self, nums: List[int]) -> int: #Approach: Start with left boundary being index 0 position and right boundary #being first out of bounds position: index len(nums)! #At every iteration of binary search, we will get the middle index, see if middle #index element is a peak, and if it's not a peak, I will reduce search space towards #the greater of the two neighboring elements and reduce search by approximately half each #time! L, R = 0, len(nums) while L < R: mid = (L + R) // 2 left_n, right_n = None, None if(mid == 0): left_n = float(-inf) else: left_n = nums[mid - 1] if(mid == len(nums) - 1): right_n = float(-inf) else: right_n = nums[mid + 1] #now check if middle element is peak! if(nums[mid] > left_n and nums[mid] > right_n): return mid #only greater than left neighbor: reduce search space to right portion! elif(nums[mid] > left_n): L = mid + 1 continue else: R = mid continue #Post-Processing: Could there potentially be peak element with only single element #remaining in search space? #Yes, if input array is already sorted in strictly decreasing order! #But, we see we would eventually return peak index of 0 by returning from while loop! #Also, I dry runned my code where peak element was only last element where input array #is sorted in increasing order, and it also returned correct answer by returning #from while loop! #No post processing needed!
find-peak-element
Python3 | Solved Using Binary Search With Simple Intuition
JOON1234
0
34
find peak element
162
0.462
Medium
2,571
https://leetcode.com/problems/find-peak-element/discuss/2455098/Python-easy-binary-search
class Solution: def findPeakElement(self, nums: list[int]) -> int: left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[mid + 1]: right = mid else: left = mid + 1 return left
find-peak-element
Python easy binary search
Mark_computer
0
86
find peak element
162
0.462
Medium
2,572
https://leetcode.com/problems/find-peak-element/discuss/2432044/Python-O(log(N))
class Solution: def findPeakElement(self, nums: List[int]) -> int: l = 0 r = len(nums) while l < r: mid = (l+r) // 2 # greater than left gl = (mid == 0 or nums[mid] > nums[mid-1]) # greater than right gr = (mid == len(nums)-1 or nums[mid] > nums[mid+1]) # find the peak if gl and gr: return mid # left > mid and mid < right # left side has at least one peak, right has at least one peak # left > mid > right # left side has at least one peak elif (not gl and not gr) or (not gl and gr): r = mid # left < mid < right # right side has at least one peak else: l = mid + 1 return l
find-peak-element
Python O(log(N))
TerryHung
0
61
find peak element
162
0.462
Medium
2,573
https://leetcode.com/problems/find-peak-element/discuss/2403154/98-python3-solution-easily-understandable
class Solution: def findPeakElement(self, nums: List[int]) -> int: n = len(nums) if n < 3: max_val = max(nums) return nums.index(max_val) def binarySearchPeek(lo: int, hi: int) -> int: result = -1 while lo <= hi: mid = (lo+hi)//2 prev = (mid-1) nxt = (mid+1) if (prev < 0 or nums[mid] > nums[prev]) and (nxt >= n or nums[mid] > nums[nxt]): return mid elif lo == hi-1: lo = hi elif nums[prev] > nums[nxt]: hi = mid else: lo = mid return lo return binarySearchPeek(0, n-1)
find-peak-element
98% python3 solution, easily understandable
destifo
0
29
find peak element
162
0.462
Medium
2,574
https://leetcode.com/problems/find-peak-element/discuss/2214914/Python-pure-binary-search
class Solution: def findPeakElement(self, nums: List[int]) -> int: nums = [-math.inf] + nums + [-math.inf] left = 0 right = len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid-1] < nums[mid] > nums[mid+1]: return mid - 1 elif nums[mid-1] > nums[mid]: right = mid - 1 else: left = mid + 1
find-peak-element
Python, pure binary search
blue_sky5
0
51
find peak element
162
0.462
Medium
2,575
https://leetcode.com/problems/find-peak-element/discuss/2108398/Find-Peak-Element
class Solution: def findPeakElement(self, nums: List[int]) -> int: return nums.index(max(nums))
find-peak-element
Find Peak Element
anil5829354
0
14
find peak element
162
0.462
Medium
2,576
https://leetcode.com/problems/find-peak-element/discuss/2078912/O(LogN)-Approach-for-the-solution
class Solution: def findPeakElement(self, nums: List[int]) -> int: s = 0 e = len(nums)-1 mid = int(s+(e-s)/2) while(s<e): if(nums[mid]<nums[mid+1]): s=mid+1 else: e = mid mid = int(s+(e-s)/2) return mid
find-peak-element
O(LogN) Approach for the solution
arpit3043
0
113
find peak element
162
0.462
Medium
2,577
https://leetcode.com/problems/find-peak-element/discuss/2040977/Simple-Python-Binary-Search-sol
class Solution: def findPeakElement(self, nums: List[int]) -> int: start = 0 end = len(nums) - 1 while start < end: mid = start + (end - start) // 2 if nums[mid] > nums[mid + 1]: #If currently in decreasing order of the list end = mid else: #If in increasing order of the list, start after mid element start = mid + 1 return start
find-peak-element
Simple Python Binary Search sol
abhay147
0
47
find peak element
162
0.462
Medium
2,578
https://leetcode.com/problems/find-peak-element/discuss/1950470/Python-oror-Clean-Binary-Search
class Solution: def findPeakElement(self, nums: List[int]) -> int: start, end = 0 , len(nums)-1 nums.append(-inf) while start <= end: mid = (start+end)//2 if mid == 0: if nums[mid] > nums[mid+1]: return mid else: start = mid+1 elif nums[mid-1] < nums[mid] and nums[mid] > nums[mid+1]: return mid elif nums[mid] < nums[mid-1] or nums[mid] < nums[mid+1]: if nums[mid-1] < nums[mid+1]: start = mid+1 else: end = mid-1
find-peak-element
Python || Clean Binary Search
morpheusdurden
0
98
find peak element
162
0.462
Medium
2,579
https://leetcode.com/problems/find-peak-element/discuss/1855998/Python-easy-to-read-and-understand-or-binary-search
class Solution: def findPeakElement(self, nums: List[int]) -> int: if len(nums) == 1: return 0 if len(nums) == 2: return 0 if nums[0] > nums[1] else 1 start, end = 0, len(nums)-1 while start <= end: mid = (start+end) // 2 #print(mid) if mid > 0 and mid < len(nums)-1 and nums[mid-1] < nums[mid] > nums[mid+1]: return mid if mid < len(nums)-1 and nums[mid] < nums[mid+1]: start = mid+1 else: end = mid-1 return start
find-peak-element
Python easy to read and understand | binary search
sanial2001
0
59
find peak element
162
0.462
Medium
2,580
https://leetcode.com/problems/find-peak-element/discuss/1593230/Python-Binary-Search
class Solution: def findPeakElement(self, nums: List[int]) -> int: l, r = 0, len(nums)-1 while l <= r: mid = (l+r)//2 midVal = nums[mid] if mid+1 == len(nums): return mid if midVal > nums[mid+1] and midVal> nums[mid-1]: return mid elif midVal > nums[mid+1] and midVal < nums[mid-1]: r = mid -1 else: l = mid + 1 return -1
find-peak-element
Python Binary Search
flora_1888
0
168
find peak element
162
0.462
Medium
2,581
https://leetcode.com/problems/find-peak-element/discuss/929554/Python3-Faster-than-90-Solutions-and-100-Memory-less
class Solution: def findPeakElement(self, nums: List[int]) -> int: if len(nums) == 1: return 0 if len(nums) == 2: if nums[0] > nums[1]: return 0 else: return 1 for i in range(0,len(nums)): if i == 0 and nums[i] > nums[i+1]: return 0 if i == len(nums)-1 and nums[i] > nums[i-1]: return len(nums)-1 if nums[i-1] < nums[i] and nums[i] > nums[i+1]: return i return 0
find-peak-element
Python3 Faster than 90% Solutions and 100% Memory less
tgoel219
0
163
find peak element
162
0.462
Medium
2,582
https://leetcode.com/problems/find-peak-element/discuss/524156/Python-3-Binary-Search.-O(logn)
class Solution: def findPeakElement(self, nums: List[int]) -> int: left, right = 0, len(nums)-1 while left<=right: mid = left+(right-left)//2 if mid == len(nums)-1: return mid elif nums[mid]>nums[mid+1]: right = mid-1 else: left = mid+1 return left
find-peak-element
Python 3, Binary Search. O(logn)
timzeng
0
79
find peak element
162
0.462
Medium
2,583
https://leetcode.com/problems/find-peak-element/discuss/505362/Python3-both-linear-and-logarithmic-solutions
class Solution: def findPeakElement(self, nums: List[int]) -> int: def binary_search(left,right): if left==right: return left pivot=(left+right)//2 if nums[pivot]>nums[pivot+1]: return binary_search(left,pivot) return binary_search(pivot+1,right) return binary_search(0,len(nums)-1) def findPeakElement1(self, nums: List[int]) -> int: for i in range(len(nums)-1): if nums[i]>nums[i+1]: return i return len(nums)-1
find-peak-element
Python3 both linear and logarithmic solutions
jb07
0
75
find peak element
162
0.462
Medium
2,584
https://leetcode.com/problems/find-peak-element/discuss/405681/One-liner-using-Python-STL
class Solution: def findPeakElement(self, nums: List[int]) -> int: return nums.index(max(nums))
find-peak-element
One liner using Python STL
saffi
0
138
find peak element
162
0.462
Medium
2,585
https://leetcode.com/problems/find-peak-element/discuss/1727578/Python-99.9-Less-Memory-45-Faster
class Solution: def findPeakElement(self, nums: List[int]) -> int: #sort the list, take the last value, find that via index in the original nums list return(nums.index((sorted(nums)[-1::])[0]))
find-peak-element
Python 99.9% Less Memory, 45% Faster
ovidaure
-1
149
find peak element
162
0.462
Medium
2,586
https://leetcode.com/problems/find-peak-element/discuss/1610689/Short-and-readable-binary-search-for-Py3
class Solution: def findPeakElement(self, nums: List[int]) -> int: l,r = 0, len(nums)-1 while l<r: mid = l +(r-l)//2 if nums[mid]< nums[mid+1]: l = mid+1 else: r = mid return l
find-peak-element
Short and readable binary search for Py3
ys258
-2
124
find peak element
162
0.462
Medium
2,587
https://leetcode.com/problems/find-peak-element/discuss/1334815/One-line-solution-or-python
class Solution: def findPeakElement(self, nums: List[int]) -> int: return nums.index(max(nums))
find-peak-element
One line solution | python
maitysourab
-2
61
find peak element
162
0.462
Medium
2,588
https://leetcode.com/problems/maximum-gap/discuss/727709/Python3-group-data-into-buckets
class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums) == 0: return 0 #edge case mn, mx = min(nums), max(nums) step = max(1, (mx - mn)//(len(nums)-1)) #n-1 holes size = (mx - mn)//step + 1 buckets = [[inf, -inf] for _ in range(size)] for num in nums: i = (num - mn)//step x, xx = buckets[i] buckets[i] = min(x, num), max(xx, num) ans = 0 prev = mn for i in range(size): x, xx = buckets[i] if x < inf: ans = max(ans, x - prev) prev = xx return ans
maximum-gap
[Python3] group data into buckets
ye15
6
291
maximum gap
164
0.428
Hard
2,589
https://leetcode.com/problems/maximum-gap/discuss/499481/Python3-super-simple-nlog(n)-solution
class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums)==1: return 0 nums.sort() max_diff=0 for i in range(len(nums)-1): max_diff=max(max_diff,nums[i+1]-nums[i]) return max_diff
maximum-gap
Python3 super simple nlog(n) solution
jb07
3
131
maximum gap
164
0.428
Hard
2,590
https://leetcode.com/problems/maximum-gap/discuss/2735237/PythonororO(NlogN)ororRuntime-1123-ms-Beats-91.69-Memory-28.2-MB-Beats-42.79
class Solution: def maximumGap(self, nums: List[int]) -> int: nums.sort() n=len(nums) m=0 for i in range(n-1): m=max(m,abs(nums[i]-nums[i+1])) return m
maximum-gap
Python||O(NlogN)||Runtime 1123 ms Beats 91.69% Memory 28.2 MB Beats 42.79%
Sneh713
1
143
maximum gap
164
0.428
Hard
2,591
https://leetcode.com/problems/maximum-gap/discuss/1967826/python-3-oror-bucket-sort
class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums) < 2: return 0 maxNum = max(nums) digit = 1 base = 16 while maxNum >= digit: buckets = [[] for _ in range(base)] for num in nums: buckets[num // digit % base].append(num) nums = [] for bucket in buckets: nums.extend(bucket) digit *= base return max(nums[i] - nums[i - 1] for i in range(1, len(nums)))
maximum-gap
python 3 || bucket sort
dereky4
1
173
maximum gap
164
0.428
Hard
2,592
https://leetcode.com/problems/maximum-gap/discuss/1352305/Python-Bucket-Sort-Solution-(-Not-comparison-based-soritng)
class Solution: def BucketSort(self,arr,n): max_ele=max(arr) size=max_ele/n bucketList=[[] for i in range(n)] for i in range(n): j=int(arr[i]/size) if j==n:bucketList[j-1].append(arr[i]) else:bucketList[j].append(arr[i]) for i in bucketList: if i:i.sort() k=0 for lst in bucketList: if lst: for i in lst: arr[k]=i k+=1 def maximumGap(self, nums: List[int]) -> int: n=len(nums) if n<2:return 0 self.BucketSort(nums,n) ans=0 for i in range(n-1): x=nums[i+1]-nums[i] if x>ans:ans=x return ans
maximum-gap
Python Bucket Sort Solution ( Not comparison based soritng)
reaper_27
1
201
maximum gap
164
0.428
Hard
2,593
https://leetcode.com/problems/maximum-gap/discuss/1240367/Maximum-Gap-oror-Python-oror-Easy-to-understand-code
class Solution: def maximumGap(self, nums: List[int]) -> int: n=len(nums) if n<2: return 0 diff=0 nums.sort() for i in range(1,n): diff=max(diff,abs(nums[i]-nums[i-1])) return diff
maximum-gap
Maximum Gap || Python || Easy to understand code
jaipoo
1
224
maximum gap
164
0.428
Hard
2,594
https://leetcode.com/problems/maximum-gap/discuss/2823056/Maximum-Gap
class Solution: def maximumGap(self, nums: List[int]) -> int: n = len(nums) if (n < 2): return 0 maxm = float('-inf') minm = float('inf') i = 0 j = n-1 while (i <= j): maxm = max(maxm, nums[i], nums[j]) minm = min(minm, nums[i], nums[j]) i += 1 j -= 1 if (maxm == minm): return 0 gap = (maxm - minm)//(n-1) if ((maxm - minm)%(n-1) != 0): gap += 1 maxArr = [float('-inf')]*n minArr = [float('inf')]*n for i in range(n): bkt = (nums[i] - minm)//gap maxArr[bkt] = max(maxArr[bkt], nums[i]) minArr[bkt] = min(minArr[bkt], nums[i]) ans = float('-inf') prev = float('-inf') for i in range(n): if (maxArr[i] == float('-inf')): continue if (prev == float('-inf')): prev = maxArr[i] else: ans = max(ans, minArr[i]-prev) prev = maxArr[i] return ans
maximum-gap
Maximum Gap
CodeReactor8bits
0
2
maximum gap
164
0.428
Hard
2,595
https://leetcode.com/problems/maximum-gap/discuss/2685584/Python-3-Brute-Force
class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums) < 2: return 0 nums.sort() res = 0 for i in range(len(nums)-1): res = max(nums[i+1]-nums[i], res) return res
maximum-gap
Python 3 [Brute Force]
faraaz_usmani
0
6
maximum gap
164
0.428
Hard
2,596
https://leetcode.com/problems/maximum-gap/discuss/2661906/Simple-python-code-with-explanation
class Solution: def maximumGap(self, nums): #if the length of the list(nums) is 1 if len(nums) == 1: #then return 0 return 0 #create a empty list(a) a = [] #sort the elements in list(nums) nums = sorted(nums) #iterate over the elements in the list(nums) upto last but one for i in range(len(nums)-1): #add the difference of (next index and curr index) values to list(a) a.append(nums[i+1] - nums[i]) #return the maximum of list(a) return max(a)
maximum-gap
Simple python code with explanation
thomanani
0
17
maximum gap
164
0.428
Hard
2,597
https://leetcode.com/problems/maximum-gap/discuss/2661906/Simple-python-code-with-explanation
class Solution: def maximumGap(self, nums): #if length of list(nums) is 1 if len(nums) == 1: #then return 0 return 0 #sort the elements in the list(nums) upto last but one nums = sorted(nums) #iterate over the elements in list(nums) for i in range(len(nums)-1): #change the curr element as difference of (next element and curr element) nums[i] = nums[i+1]-nums[i] #change the last element to 0 nums[-1] = 0 #return the maximum of list(nums) return max(nums)
maximum-gap
Simple python code with explanation
thomanani
0
17
maximum gap
164
0.428
Hard
2,598
https://leetcode.com/problems/maximum-gap/discuss/2592606/Simple-solution-using-python
class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums)==1: return 0 mx = 0 nums = sorted(nums) for i in range(1,len(nums)): if(nums[i]-nums[i-1]>mx): mx = nums[i] - nums[i-1] return mx
maximum-gap
Simple solution using python
abdulrazak01
0
50
maximum gap
164
0.428
Hard
2,599