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/task-scheduler/discuss/1914699/Python3-Solution-with-using-heap-and-queue-with-comments | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
c = collections.Counter(tasks)
heap = []
# store pair (time, -cnt) - time - stamps on time interval, cnt - number of tasks (negative because we use python min heap)
q = collections.deque()
... | task-scheduler | [Python3] Solution with using heap and queue with comments | maosipov11 | 0 | 113 | task scheduler | 621 | 0.558 | Medium | 10,500 |
https://leetcode.com/problems/task-scheduler/discuss/1843609/Python-easy-to-read-and-understand-or-max-heap | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
d = {}
for task in tasks:
d[task] = d.get(task, 0) + 1
pq, q = [], deque()
for key in d:
heapq.heappush(pq, -d[key])
time = 0
while len(pq) > 0 or len(q... | task-scheduler | Python easy to read and understand | max-heap | sanial2001 | 0 | 266 | task scheduler | 621 | 0.558 | Medium | 10,501 |
https://leetcode.com/problems/task-scheduler/discuss/1625646/Python3-Solution-oror-O(N)-TIME-O(1)-SPACE-oror-HashTable | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
dictMap = {}
for task in tasks:
if dictMap.get(task) is None:
dictMap[task] = 1
else:
dictMap[task] += 1
dictMap = {k: v for k, v in sorted(dictMap.items(), rever... | task-scheduler | Python3 Solution || O(N) TIME, O(1) SPACE || HashTable | henriducard | 0 | 335 | task scheduler | 621 | 0.558 | Medium | 10,502 |
https://leetcode.com/problems/task-scheduler/discuss/1617087/Python-Bucket-sort-Timsort-and-Heap-Priority-queue | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
freq = [0] * 26
for task in tasks: freq[ord(task) - ord('A')] += 1
# highest freq, how many letters have it
highest_freq, no_high_tasks = self.highest_freq1(freq, len(tasks))
parts = h... | task-scheduler | [Python] Bucket sort, Timsort and Heap/ Priority queue | mostafaa | 0 | 157 | task scheduler | 621 | 0.558 | Medium | 10,503 |
https://leetcode.com/problems/task-scheduler/discuss/761331/Python3-frequency-table | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
#frequency table
freq = dict()
high = count = 0 #highest frequency & its count
for task in tasks:
freq[task] = 1 + freq.get(task, 0)
if freq[task] > high: high, count = fr... | task-scheduler | [Python3] frequency table | ye15 | 0 | 63 | task scheduler | 621 | 0.558 | Medium | 10,504 |
https://leetcode.com/problems/task-scheduler/discuss/761331/Python3-frequency-table | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
#frequency table
freq = dict()
high = count = 0 #highest frequency & its count
for task in tasks:
freq[task] = 1 + freq.get(task, 0)
if freq[task] > high: high, count = fr... | task-scheduler | [Python3] frequency table | ye15 | 0 | 63 | task scheduler | 621 | 0.558 | Medium | 10,505 |
https://leetcode.com/problems/task-scheduler/discuss/761331/Python3-frequency-table | class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
freq = {} # frequency table
for x in tasks: freq[x] = 1 + freq.get(x, 0)
mx = max(freq.values()) # highest frequency
cnt = sum(v == mx for v in freq.values()) # occurrences of most frequent tasks
retur... | task-scheduler | [Python3] frequency table | ye15 | 0 | 63 | task scheduler | 621 | 0.558 | Medium | 10,506 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/1101104/Python.-Recursive.-Easy-understanding-solution | class Solution:
def addOneRow(self, root: TreeNode, v: int, d: int, side = "left") -> TreeNode:
if d == 1:
res = TreeNode(v)
setattr(res, side, root)
return res
if root:
root.left = self.addOneRow(root.left, v, d - 1)
root.right = self.addO... | add-one-row-to-tree | Python. Recursive. Easy understanding solution | m-d-f | 13 | 772 | add one row to tree | 623 | 0.595 | Medium | 10,507 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/885328/Python3-recursive-solution | class Solution:
def addOneRow(self, root: TreeNode, v: int, d: int) -> TreeNode:
if d == 1: return TreeNode(v, left=root) # edge case
def fn(node, d):
if d == 1:
node.left = TreeNode(v, left=node.left)
node.right = TreeNode(v, right=node.right)... | add-one-row-to-tree | [Python3] recursive solution | ye15 | 3 | 134 | add one row to tree | 623 | 0.595 | Medium | 10,508 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/885328/Python3-recursive-solution | class Solution:
def addOneRow(self, root: TreeNode, v: int, d: int) -> TreeNode:
if d == 1: return TreeNode(v, left=root) # edge case
queue = [root]
while queue:
d -= 1
if d == 1:
for node in queue:
node.left = TreeNode(v, left=... | add-one-row-to-tree | [Python3] recursive solution | ye15 | 3 | 134 | add one row to tree | 623 | 0.595 | Medium | 10,509 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2664284/Python-or-Two-solutions-using-DFS-and-BFS | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1: return TreeNode(val, root)
def add_row(node, depth):
if node:
if depth == 1:
node.left = TreeNode(val, left = node.left... | add-one-row-to-tree | Python | Two solutions using DFS and BFS | ahmadheshamzaki | 2 | 95 | add one row to tree | 623 | 0.595 | Medium | 10,510 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2664284/Python-or-Two-solutions-using-DFS-and-BFS | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1: return TreeNode(val, root)
queue = deque([root])
while depth - 1 != 1:
for _ in range(len(queue)):
node = queue.popleft()
... | add-one-row-to-tree | Python | Two solutions using DFS and BFS | ahmadheshamzaki | 2 | 95 | add one row to tree | 623 | 0.595 | Medium | 10,511 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2661866/Clean-Iterative-Solution | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
node = TreeNode(val)
node.left = root
return node
queue = [root]
for _ in range(depth - 2):
temp = []
... | add-one-row-to-tree | Clean Iterative Solution | namanxk | 2 | 66 | add one row to tree | 623 | 0.595 | Medium | 10,512 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665599/Python-Simple-Solutions-Bfs-98 | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
return TreeNode(val=val, left=root)
def dfs(node, level):
if level == depth - 1:
node.left = TreeNode(val, left=node.left) if... | add-one-row-to-tree | ✅ [Python] Simple Solutions Bfs [ 98%] | Kofineka | 1 | 30 | add one row to tree | 623 | 0.595 | Medium | 10,513 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2664553/Python-DFS | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int, is_left=True) -> Optional[TreeNode]:
if depth == 1:
return TreeNode(val, root, None) if is_left else TreeNode(val, None, root)
if root:
root.left = self.addOneRow(roo... | add-one-row-to-tree | Python, DFS | blue_sky5 | 1 | 17 | add one row to tree | 623 | 0.595 | Medium | 10,514 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/1248372/level-order-traversal-simple-solution | class Solution:
def addOneRow(self, root: TreeNode, val: int, depth: int) -> TreeNode:
if depth==1:
temp=TreeNode()
temp.val=val
temp.left=root
temp.right=None
return temp
def solve(root,l,depth,val):
if not ro... | add-one-row-to-tree | [level order traversal] simple solution | _kumarmohit_ | 1 | 96 | add one row to tree | 623 | 0.595 | Medium | 10,515 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/1102438/PythonPython3-Add-One-Row-to-Tree | class Solution:
def addOneRow(self, root: TreeNode, v: int, d: int) -> TreeNode:
if d == 1:
newRoot = TreeNode(v)
newRoot.left = root
return newRoot
else:
level_prev = list()
# out = []
level = [root]
depth ... | add-one-row-to-tree | [Python/Python3] Add One Row to Tree | newborncoder | 1 | 88 | add one row to tree | 623 | 0.595 | Medium | 10,516 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2674962/Python-Simple-Python-Solution-Using-Recursion-or-DFS | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
newnode = TreeNode(val)
newnode.left = root
return newnode
def DFS(node, val, depth):
if node == None:
return None
if depth == 1:
node.left = TreeNode(val, node.left,... | add-one-row-to-tree | [ Python ] ✅✅ Simple Python Solution Using Recursion | DFS 🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 19 | add one row to tree | 623 | 0.595 | Medium | 10,517 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2666077/DFS-in-Python-faster-than-85 | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if root is None:
return None
elif depth == 1:
return TreeNode(val, root, None)
elif depth == 2:
root.left, root.right = TreeNode(val, root.left, None... | add-one-row-to-tree | DFS in Python, faster than 85% | metaphysicalist | 0 | 11 | add one row to tree | 623 | 0.595 | Medium | 10,518 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665816/Python3!-As-short-as-it-gets! | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth < 1 or root is None:
return None
if depth == 1:
return TreeNode(val, root, None)
if depth == 2:
root.left = TreeNode(val, root.left, None)
... | add-one-row-to-tree | 😎Python3! As short as it gets! | aminjun | 0 | 21 | add one row to tree | 623 | 0.595 | Medium | 10,519 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665569/Python-or-DFS-or-Simple-Solution | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
# deal with depth=1 first
if depth == 1:
return TreeNode(val=val, left=root) # simply make the root node a left child of the new node
def dfs(node, level):
... | add-one-row-to-tree | Python | DFS | Simple Solution | anon82214 | 0 | 61 | add one row to tree | 623 | 0.595 | Medium | 10,520 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665255/Python-easy-dfs-explained | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
def dfs(root, depth, lr):
if depth == 1:
if not root:
return TreeNode(val)
elif lr == 'l':
return TreeNode(val, root... | add-one-row-to-tree | [Python] easy dfs, explained | b10415048 | 0 | 13 | add one row to tree | 623 | 0.595 | Medium | 10,521 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665115/Python-Faster-than-97 | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
def traversal(node, currentDepth):
if currentDepth == depth - 1:
newNodeLeft = TreeNode(val=val, left=node.left)
newNodeRight = TreeNode(val=val, right=node.... | add-one-row-to-tree | Python Faster than 97% | AxelDovskog | 0 | 9 | add one row to tree | 623 | 0.595 | Medium | 10,522 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665106/python3-very-efficient-algorithm | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth==1:
n=TreeNode(val,root)
return n
qu=deque([root])
dep=2
while qu:
r=len(qu)
if dep==depth:
for node in ... | add-one-row-to-tree | python3 very efficient algorithm | benon | 0 | 33 | add one row to tree | 623 | 0.595 | Medium | 10,523 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2665072/Python-and-Go-very-short-and-simple-DFS | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int, left=True) -> Optional[TreeNode]:
if depth == 1:
return TreeNode(val, root if left else None, root if not left else None)
if root:
root.left = self.addOneRow(root.left, val, depth - 1, True)
... | add-one-row-to-tree | Python and Go - very short and simple DFS | alexkogulko | 0 | 3 | add one row to tree | 623 | 0.595 | Medium | 10,524 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2664245/Straight-forward-python-solution-with-dummy-node | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
dummy = TreeNode(0, root, None)
if depth == 0:
return root
def rec(node, depth):
if node is None:
return
if depth == 1:
... | add-one-row-to-tree | Straight forward python solution with dummy node | PAquaticus | 0 | 2 | add one row to tree | 623 | 0.595 | Medium | 10,525 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2664143/python3-recursive-solution | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
def dfs(node, depth):
if not node or depth < 0:
return
if node and depth == 0:
ln, rn = TreeNode(val), TreeNode(val)
ln.left = no... | add-one-row-to-tree | python3, recursive solution | pjy953 | 0 | 15 | add one row to tree | 623 | 0.595 | Medium | 10,526 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2663699/Easy-to-understand-Python-code-(Faster-than-93)-or-Iterative-BFS | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
root = TreeNode(val, root)
return root
q = deque()
q.append(root)
while q:
depth -= 1
for _ in range(len(q)):
... | add-one-row-to-tree | Easy to understand Python code (Faster than 93%) | Iterative BFS | KevinJM17 | 0 | 3 | add one row to tree | 623 | 0.595 | Medium | 10,527 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2663313/Python-Solution-or-90-Faster-or-Recursive-DFS-%2B-Levels-Based-or-Clean-Code | class Solution(object):
def addOneRow(self, root, v, d):
if not root or d == 0:
return None
# add a treenode with og tree as left child
if d == 1:
return TreeNode(v, root, None)
# give og left to new left and og right to new right
if d == 2:
... | add-one-row-to-tree | Python Solution | 90% Faster | Recursive DFS + Levels Based | Clean Code | Gautam_ProMax | 0 | 25 | add one row to tree | 623 | 0.595 | Medium | 10,528 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2663265/96-Faster-Solution-or-Easy-to-Understand-or-Beginner's-Friendly | class Solution(object):
def addOneRow(self, root, val, depth):
if depth == 1:
newNode = TreeNode(val)
newNode.left = root
root = newNode
return root
idx = 1
flag = False
q = deque()
q.append(root)
while len(q):
... | add-one-row-to-tree | 96% Faster Solution | Easy to Understand | Beginner's Friendly | its_krish_here | 0 | 17 | add one row to tree | 623 | 0.595 | Medium | 10,529 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2662945/Python3-solution-or-DFS-or-Easy-to-understand | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1:
return TreeNode(val, root, None)
def DFS(node, level):
if node:
if depth - 1 == level:
node.left = TreeNode(v... | add-one-row-to-tree | Python3 solution | DFS | Easy to understand | mediocre-coder | 0 | 22 | add one row to tree | 623 | 0.595 | Medium | 10,530 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2662645/Python-10-line-simple-recursive-solution | class Solution:
def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:
if depth == 1: return TreeNode(val, root)
def f(node: TreeNode, depth: int):
if depth == 1:
node.left = TreeNode(val, node.left)
node.right = TreeNo... | add-one-row-to-tree | Python 10-line simple recursive solution | OverflowCat | 0 | 2 | add one row to tree | 623 | 0.595 | Medium | 10,531 |
https://leetcode.com/problems/add-one-row-to-tree/discuss/2662581/Simple-Solution-Python-or-Easy-to-understand | class Solution:
def addOneRow(self, root: Optional[TreeNode], v: int, d: int) -> Optional[TreeNode]:
temp=root
def hii(temp,d):
if d==2 and temp:
x=TreeNode(v)
y=TreeNode(v)
x.left=temp.left
y.right=temp.right
... | add-one-row-to-tree | Simple Solution Python | Easy to understand | narendra_036 | 0 | 13 | add one row to tree | 623 | 0.595 | Medium | 10,532 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/356715/Python3-O(N)-and-O(NlogN)-solutions | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
max1 = max2 = max3 = float("-inf")
min1 = min2 = float("inf")
for num in nums:
if num > max1:
max1, max2, max3 = num, max1, max2
elif num > max2:
max2, max3 = n... | maximum-product-of-three-numbers | [Python3] O(N) and O(NlogN) solutions | ye15 | 7 | 677 | maximum product of three numbers | 628 | 0.463 | Easy | 10,533 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/356715/Python3-O(N)-and-O(NlogN)-solutions | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[0]*nums[1]*nums[-1], nums[-3]*nums[-2]*nums[-1]) | maximum-product-of-three-numbers | [Python3] O(N) and O(NlogN) solutions | ye15 | 7 | 677 | maximum product of three numbers | 628 | 0.463 | Easy | 10,534 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2119438/4-Lines-of-Python-code | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
l1 = nums[-1]*nums[-2]*nums[-3]
l2 = nums[0]*nums[1]*nums[-1]
return max(l1,l2) | maximum-product-of-three-numbers | 4 Lines of Python code | prernaarora221 | 4 | 348 | maximum product of three numbers | 628 | 0.463 | Easy | 10,535 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1867647/Python-easy-to-read-and-understand-or-sorting | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3]) | maximum-product-of-three-numbers | Python easy to read and understand | sorting | sanial2001 | 2 | 302 | maximum product of three numbers | 628 | 0.463 | Easy | 10,536 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/353426/Three-Solutions-in-Python-3-(beats-~100)-(-O(n)-) | class Solution:
def maximumProduct(self, n: List[int]) -> int:
n.sort()
return max(n[-1]*n[0]*n[1], n[-1]*n[-2]*n[-3]) | maximum-product-of-three-numbers | Three Solutions in Python 3 (beats ~100%) ( O(n) ) | junaidmansuri | 2 | 1,200 | maximum product of three numbers | 628 | 0.463 | Easy | 10,537 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/353426/Three-Solutions-in-Python-3-(beats-~100)-(-O(n)-) | class Solution:
def maximumProduct(self, n: List[int]) -> int:
return (lambda x: x[-1]*max(x[-3]*x[-2],x[0]*x[1]))(sorted(n))
- Junaid Mansuri
(LeetCode ID)@hotmail.com | maximum-product-of-three-numbers | Three Solutions in Python 3 (beats ~100%) ( O(n) ) | junaidmansuri | 2 | 1,200 | maximum product of three numbers | 628 | 0.463 | Easy | 10,538 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1576485/Fast-and-simple-solution-beats-98-submissions | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1]*nums[0]*nums[1], nums[-1]*nums[-2]*nums[-3]) | maximum-product-of-three-numbers | Fast and simple solution beats 98% submissions | cyrille-k | 1 | 265 | maximum product of three numbers | 628 | 0.463 | Easy | 10,539 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1573006/Python-very-easy-sorting-solution | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1] * nums[-2] * nums[-3], nums[-1] * nums[0] * nums[1]) | maximum-product-of-three-numbers | Python very easy sorting solution | dereky4 | 1 | 212 | maximum product of three numbers | 628 | 0.463 | Easy | 10,540 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1235363/Python3-simple-solution-using-sorting | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3]) | maximum-product-of-three-numbers | Python3 simple solution using sorting | EklavyaJoshi | 1 | 123 | maximum product of three numbers | 628 | 0.463 | Easy | 10,541 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1191364/Python32-Line-Solution | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-3] * nums[-2] * nums[-1], nums[0] * nums[1] * nums[-1]) | maximum-product-of-three-numbers | 【Python3】2 Line Solution | qiaochow | 1 | 85 | maximum product of three numbers | 628 | 0.463 | Easy | 10,542 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1114896/Easy-to-remember-Python-solution-(O(n)-time-O(1)-space) | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
max1, min1 = max(nums[0], nums[1]), min(nums[0], nums[1])
max2 = min2 = nums[0] * nums[1]
max3 = nums[0] * nums[1] * nums[2]
for i in range(2, len(nums)):
currNum = nums[i]
... | maximum-product-of-three-numbers | Easy to remember Python solution (O(n) time, O(1) space) | genefever | 1 | 343 | maximum product of three numbers | 628 | 0.463 | Easy | 10,543 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/850848/Time-O(n)-Space-O(1)-Easy-and-concise-Python-solution-(Faster-than-90) | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
max1=max2=max3= float("-inf")
min1=min2= float("inf")
for num in nums:
# 由大往小 (先覆盖比最大的大的, 再到比第二个大的, 再到比第三个大的)
if num > max3:
max1 = max2
max2 = max3
max3 ... | maximum-product-of-three-numbers | Time O(n), Space O(1) Easy and concise Python solution (Faster than 90%) | noSuchThing_ | 1 | 122 | maximum product of three numbers | 628 | 0.463 | Easy | 10,544 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/385507/Easy-python-solution-using-maths-logic | class Solution(object):
def maximumProduct(self, nums):
x=sorted(nums)
a = x[0]*x[1]*x[-1]
b=x[-1]*x[-2]*x[-3]
return max(a,b) | maximum-product-of-three-numbers | Easy python solution using maths logic | rachitsxn292 | 1 | 3,200 | maximum product of three numbers | 628 | 0.463 | Easy | 10,545 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/325127/Python3-or-fast-and-easy-understanding-or-O(n) | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
max1, max2, max3, min1, min2 = float('-inf'), float('-inf'), float('-inf'), float('inf'), float('inf')
for n in nums:
if n > max1:
max1, max2, max3 = n, max1, max2
elif n > max2:
... | maximum-product-of-three-numbers | Python3 | fast and easy understanding | O(n) | zhuzhengyuan824 | 1 | 247 | maximum product of three numbers | 628 | 0.463 | Easy | 10,546 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2779386/Max-Product-of-three-numbers | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1]*nums[-2]*nums[-3],nums[0]*nums[1]*nums[-1]) | maximum-product-of-three-numbers | Max Product of three numbers | RaviShanker__Thadishetti | 0 | 6 | maximum product of three numbers | 628 | 0.463 | Easy | 10,547 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2771041/One-line-easy-code | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max (nums[-1]*nums[-2]*nums[-3],nums[-1]*nums[0]*nums[1]) | maximum-product-of-three-numbers | One line easy code | nishithakonuganti | 0 | 3 | maximum product of three numbers | 628 | 0.463 | Easy | 10,548 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2736466/Fast-and-Easy-Solution | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
product1 = nums[0] * nums[1] * nums[-1]
product2 = nums[-1] * nums[-2] * nums[-3]
return max(product1, product2) | maximum-product-of-three-numbers | Fast and Easy Solution | user6770yv | 0 | 5 | maximum product of three numbers | 628 | 0.463 | Easy | 10,549 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2620224/Python3-easy-and-shortest-solution-using-math-concept. | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums = sorted(nums)
return max(nums[0]*nums[1]*nums[-1],nums[-1]*nums[-2]*nums[-3]) | maximum-product-of-three-numbers | Python3 easy and shortest solution using math concept. | sanzid | 0 | 23 | maximum product of three numbers | 628 | 0.463 | Easy | 10,550 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2421146/python3-easy-solution-for-beginners | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
ans=[]
#keeping in mind all the 6 possibilities the following six combos are formed:
ans.append(nums[-1]*nums[-2]*nums[-3])
ans.append(nums[-1]*nums[-2]*nums[0])
ans.append(nums[-1]*nums[0]*nums[1])
ans.append(nums... | maximum-product-of-three-numbers | python3 easy solution for beginners | keertika27 | 0 | 126 | maximum product of three numbers | 628 | 0.463 | Easy | 10,551 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2419184/Python-optimized-solution | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[0]*nums[1]*nums[-1],nums[-3]*nums[-2]*nums[-1]) | maximum-product-of-three-numbers | Python optimized solution | shacid | 0 | 39 | maximum product of three numbers | 628 | 0.463 | Easy | 10,552 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2356806/python3%3A-Easy-and-intuitive-explanation | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
# TC = O(NlogN) because sorting the array
# SC = O(1); no extra space needed; sorting was done in place.
# sorting the array in descending order
nums.sort(reverse = True)
# maximum product ca... | maximum-product-of-three-numbers | python3: Easy & intuitive explanation | jinwooo | 0 | 103 | maximum product of three numbers | 628 | 0.463 | Easy | 10,553 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/2161304/faster-than-95.12-of-Python3-online-submissions | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
a,b = heapq.nlargest(3,nums), heapq.nsmallest(2,nums)
return max(a[0]*a[1]*a[2], b[0]*b[1]*a[0]) | maximum-product-of-three-numbers | faster than 95.12% of Python3 online submissions | writemeom | 0 | 174 | maximum product of three numbers | 628 | 0.463 | Easy | 10,554 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1831837/2-Lines-Python-Solution-oror-70-Faster-oror-Memory-less-than-85 | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1]*nums[-2]*nums[-3], nums[0]*nums[1]*nums[2], nums[0]*nums[1]*nums[-1]) | maximum-product-of-three-numbers | 2-Lines Python Solution || 70% Faster || Memory less than 85% | Taha-C | 0 | 181 | maximum product of three numbers | 628 | 0.463 | Easy | 10,555 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1782153/Simple-python3-solution-or-simple-trick | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums = sorted(nums)
a = nums[0]*nums[1]*nums[-1]
b = nums[-1]*nums[-2]*nums[-3]
if a>b:
return a
return b | maximum-product-of-three-numbers | ✔Simple python3 solution | simple trick | Coding_Tan3 | 0 | 182 | maximum product of three numbers | 628 | 0.463 | Easy | 10,556 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1762463/Python3-Two-kind-of-solutions | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[0] * nums[1] * nums[-1], nums[-1] * nums[-2] * nums[-3])
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
min1, min2 = float('inf'), float('inf')
max1, max2, m... | maximum-product-of-three-numbers | [Python3] Two kind of solutions | maosipov11 | 0 | 116 | maximum product of three numbers | 628 | 0.463 | Easy | 10,557 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1569466/Python-Easy-Solution | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
val1 = nums[0]*nums[1]*nums[-1]
val2 = nums[-3]*nums[-2]*nums[-1]
if val1>=val2:
return val1
else:
return val2 | maximum-product-of-three-numbers | Python, Easy-Solution | AshwinBalaji52 | 0 | 107 | maximum product of three numbers | 628 | 0.463 | Easy | 10,558 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1317826/Python-3-%3A-simple-using-math.prod() | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums = sorted(nums)
return max(math.prod(nums[:2]+nums[-1:]),math.prod(nums[-3:])) | maximum-product-of-three-numbers | Python 3 : simple , using math.prod() | rohitkhairnar | 0 | 202 | maximum product of three numbers | 628 | 0.463 | Easy | 10,559 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1304023/Python3-dollarolution | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
if nums[0] > -1:
s = nums[-1]*nums[-2]*nums[-3]
else:
s = 0
if nums[1] < 0:
s = nums[0]*nums[1]*nums[-1]
x = nums[-1]*nums[-2]*nums[-3]
if... | maximum-product-of-three-numbers | Python3 $olution | AakRay | 0 | 205 | maximum product of three numbers | 628 | 0.463 | Easy | 10,560 |
https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/1434053/Python3-One-Line-Faster-Than-98.27 | class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1]) | maximum-product-of-three-numbers | Python3 One-Line, Faster Than 98.27% | Hejita | -1 | 138 | maximum product of three numbers | 628 | 0.463 | Easy | 10,561 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2293304/Python3-oror-dp1D-array-10-lines-w-explanation-oror-TM%3A-9586 | class Solution:
# A very good description of the dp solution is at
# https://leetcode.com/problems/k-inverse-pairs-array/solution/
# The code below uses two 1D arrays--dp and tmp--instead if a
# 2D array. tmp replaces dp a... | k-inverse-pairs-array | Python3 || dp,1D array, 10 lines, w/ explanation || T/M: 95%/86% | warrenruud | 8 | 657 | k inverse pairs array | 629 | 0.429 | Hard | 10,562 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2293484/PYTHON-95-FASTER-EASY-BEGINER-FRIENDLY-Multiple-Solutions | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
m = 10 ** 9 + 7
dp0 = [0] * (k + 1)
dp0[0] = 1
for i in range(n):
dp1 = []
s = 0
for j in range(k + 1):
s += dp0[j]
if j >= i + 1:
s -= dp0[j - i - 1]
s %= m
dp1.append(s)
dp0 = dp1
return dp0[-... | k-inverse-pairs-array | PYTHON 95% FASTER EASY BEGINER FRIENDLY Multiple Solutions | anuvabtest | 4 | 495 | k inverse pairs array | 629 | 0.429 | Hard | 10,563 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/1284528/Python3-top-down-dp | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
@cache
def fn(n, k):
"""Return number of ways for n numbers with k inverse pairs."""
if k == 0: return 1
if n <= 0 or k < 0: return 0
return fn(n-1, k) + fn(n, k-1) - fn(n-1, k-... | k-inverse-pairs-array | [Python3] top-down dp | ye15 | 1 | 149 | k inverse pairs array | 629 | 0.429 | Hard | 10,564 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2814863/Python3-Solution-7-implementation-with-explanation | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
# set dynamically programmed values array original
# this is where we will store the final nth run of the algorithm
# we will also store the prior nth valuation here up until the end
dynamically_programmed_values = [0... | k-inverse-pairs-array | Python3 Solution 7 implementation with explanation | laichbr | 0 | 2 | k inverse pairs array | 629 | 0.429 | Hard | 10,565 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2318365/Python-Fastest-solution | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
# Complexity:
# - Time: O(N*K)
# - Space: O(K)
# Special cases that can be short-circuited right away
# - For k=0, there's only one solution, which is having the numbers in sorted order
# DP(n, 0) = ... | k-inverse-pairs-array | [Python] Fastest solution | RegInt | 0 | 79 | k inverse pairs array | 629 | 0.429 | Hard | 10,566 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2296065/Python3-Solution | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
dp, mod = [1]+[0] * k, 1000000007
for i in range(n):
tmp, sm = [], 0
for j in range(k + 1):
sm+= dp[j]
if j-i >= 1: sm-= dp[j-i-1]
sm%= mod
... | k-inverse-pairs-array | Python3 Solution | creativerahuly | 0 | 15 | k inverse pairs array | 629 | 0.429 | Hard | 10,567 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2294693/python3-1-d-dynamic-pytonic-solution | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
memo = [0 for i in range(k+1)]
memo[0] = 1
for i in range(2,n+1):
tmp = [0 for i in range(k+1)]
tmp[0] = 1
for j in range(1,k+1):
tmp[j] = tmp[j-1] + memo[j] - (memo[j-i] if j-... | k-inverse-pairs-array | python3, 1-d dynamic, pytonic solution | pjy953 | 0 | 26 | k inverse pairs array | 629 | 0.429 | Hard | 10,568 |
https://leetcode.com/problems/k-inverse-pairs-array/discuss/2294430/GoPython-O(n*k)time-or-O(k)-space | class Solution:
def kInversePairs(self, n: int, k: int) -> int:
curr = [0 for _ in range(k+1)]
prev = [0 for _ in range(k+1)]
for i in range(1,n+1):
value = 0
for j in range(k+1):
if j == 0:
curr[j] = 1
value += ... | k-inverse-pairs-array | Go/Python O(n*k)time | O(k) space | vtalantsev | 0 | 56 | k inverse pairs array | 629 | 0.429 | Hard | 10,569 |
https://leetcode.com/problems/course-schedule-iii/discuss/2185553/Python3-oror-Heapq-oror-Faster-Solution-with-explanation | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
courses.sort(key=lambda c: c[1])
A, curr = [], 0
for dur, ld in courses:
heapq.heappush(A,-dur)
curr += dur
if curr > ld: curr += heapq.heappop(A)
return len(A) | course-schedule-iii | Python3 || Heapq || Faster Solution with explanation | bvian | 28 | 1,000 | course schedule iii | 630 | 0.402 | Hard | 10,570 |
https://leetcode.com/problems/course-schedule-iii/discuss/1187517/PythonPython3-solution-using-heap | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
#sorts the course according to the 2nd index of all elements in the list
courses.sort(key = lambda x:x[1])
#print(courses) #to visualize the code after sorting it
maxHeap = [] # to store the negative values of the c... | course-schedule-iii | Python/Python3 solution using heap | prasanthksp1009 | 9 | 552 | course schedule iii | 630 | 0.402 | Hard | 10,571 |
https://leetcode.com/problems/course-schedule-iii/discuss/2380782/Python-Max-Heap-O(NlogN)-very-straighforward-and-explained-well | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
time, max_heap = 0, []
for duration, last_day in sorted(courses, key=lambda c:c[1]):
# we can take the course
if time + duration <= last_day:
time += duration
heapq.heap... | course-schedule-iii | Python Max Heap O(NlogN) very straighforward and explained well | ya332 | 4 | 104 | course schedule iii | 630 | 0.402 | Hard | 10,572 |
https://leetcode.com/problems/course-schedule-iii/discuss/2186524/Python-Recursive-%2B-Memoization-%2B-Bottom-UP-Greedy-%2B-Heap | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
# First, sorting according to the lastday.
courses.sort(key=lambda x: x[1])
return self.scheduleCourseHelper(courses, 0, 0)
# Hypothesis, will return maximum number of courses taken
def scheduleCourseHelper(se... | course-schedule-iii | Python Recursive + Memoization + Bottom-UP - Greedy + Heap | zippysphinx | 3 | 263 | course schedule iii | 630 | 0.402 | Hard | 10,573 |
https://leetcode.com/problems/course-schedule-iii/discuss/2186524/Python-Recursive-%2B-Memoization-%2B-Bottom-UP-Greedy-%2B-Heap | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
# First, sorting according to the lastday.
courses.sort(key=lambda x: x[1])
memo = [[-1 for _ in range(courses[-1][1] + 1)] for _ in range(len(courses) + 1)]
return self.scheduleCourseHelper(courses, 0, 0, mem... | course-schedule-iii | Python Recursive + Memoization + Bottom-UP - Greedy + Heap | zippysphinx | 3 | 263 | course schedule iii | 630 | 0.402 | Hard | 10,574 |
https://leetcode.com/problems/course-schedule-iii/discuss/2186524/Python-Recursive-%2B-Memoization-%2B-Bottom-UP-Greedy-%2B-Heap | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
# First, sorting according to the lastday.
courses.sort(key=lambda x: x[1])
dp = [[0 for _ in range(courses[-1][1] + 1)] for _ in range(len(courses) + 1)]
for i in range(1, len(courses) + 1):
for j... | course-schedule-iii | Python Recursive + Memoization + Bottom-UP - Greedy + Heap | zippysphinx | 3 | 263 | course schedule iii | 630 | 0.402 | Hard | 10,575 |
https://leetcode.com/problems/course-schedule-iii/discuss/2186524/Python-Recursive-%2B-Memoization-%2B-Bottom-UP-Greedy-%2B-Heap | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
# First, sorting according to the lastday.
courses.sort(key=lambda x: x[1])
totalDays = 0
count = 0
for i in range(len(courses)):
# If course can be taken, keep adding course
if... | course-schedule-iii | Python Recursive + Memoization + Bottom-UP - Greedy + Heap | zippysphinx | 3 | 263 | course schedule iii | 630 | 0.402 | Hard | 10,576 |
https://leetcode.com/problems/course-schedule-iii/discuss/1084646/Python3-simple-solution-in-O(nlogn) | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
"""
Very tricky question.
We sort the courses by their expected deadline.
Now one by one we start doing the courses.
If by chance, we are exceeding the deadline. We tend to eliminate the courses, which... | course-schedule-iii | Python3 simple solution in O(nlogn) | amol1729 | 2 | 203 | course schedule iii | 630 | 0.402 | Hard | 10,577 |
https://leetcode.com/problems/course-schedule-iii/discuss/2185450/Python3-or-explained-or-easy-to-understand-or-heap-or-sort | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
heap = [] # max heap
courses.sort(key = lambda x: x[1]) # sort w.r.t end time
stime = 0
for d, e in courses:
heappush(heap, -d) # push the duration in hea... | course-schedule-iii | Python3 | explained | easy to understand | heap | sort | H-R-S | 1 | 135 | course schedule iii | 630 | 0.402 | Hard | 10,578 |
https://leetcode.com/problems/course-schedule-iii/discuss/1208076/python3-maxheap-O(nlgn)-concise-solution | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
courses.sort(key= lambda c:c[1])
currentTotalTime = count = 0
maxheap = []
for duration, lastday in courses:
if currentTotalTime + duration <= lastday:
currentTotalTime += ... | course-schedule-iii | python3 maxheap O(nlgn) concise solution | savikx | 1 | 84 | course schedule iii | 630 | 0.402 | Hard | 10,579 |
https://leetcode.com/problems/course-schedule-iii/discuss/1042493/python-heap-solution | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
courses.sort(key=lambda x: x[1])
taken = []
start_time = 0
for duration, end_time in courses:
if start_time + duration <= end_time:
start_time += duration
heapq.heap... | course-schedule-iii | python heap solution | ChiCeline | 1 | 126 | course schedule iii | 630 | 0.402 | Hard | 10,580 |
https://leetcode.com/problems/course-schedule-iii/discuss/2753223/Python-Heap-solution | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
courses.sort(key=lambda x: x[1])
cur_time = 0
res = []
for duration,lastday in courses:
heappush(res,-duration)
cur_time += duration
if cur_time > lastday:
... | course-schedule-iii | Python Heap solution | shriyansnaik | 0 | 2 | course schedule iii | 630 | 0.402 | Hard | 10,581 |
https://leetcode.com/problems/course-schedule-iii/discuss/2185724/Python3-Solution | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
if courses == None or len(courses) == 0:
return 0
courses.sort(key = lambda x: x[1])
curr_time = count = 0
max_heap = []
heapify(max_heap)
for i in range(len(courses)):
... | course-schedule-iii | Python3 Solution | creativerahuly | 0 | 67 | course schedule iii | 630 | 0.402 | Hard | 10,582 |
https://leetcode.com/problems/course-schedule-iii/discuss/2185619/Short-python-code-trick-is-in-sorting-after-that-it-is-easy | class Solution:
def scheduleCourse(self, courses: List[List[int]], indent="") -> int:
# Whatever the final selected list is, the course with earlier end date
# should be taken earlier. That will make sure that the all the courses finish
# at the earliest. Few other combinations will work but... | course-schedule-iii | Short python code - trick is in sorting - after that it is easy | pmajumdar1976 | 0 | 14 | course schedule iii | 630 | 0.402 | Hard | 10,583 |
https://leetcode.com/problems/course-schedule-iii/discuss/1220245/Python3-greedy | class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
prefix = 0
pq = [] # max-heap
for x, y in sorted(courses, key=lambda x: x[1]):
prefix += x
heappush(pq, -x)
while prefix > y: prefix += heappop(pq)
return len(pq) | course-schedule-iii | [Python3] greedy | ye15 | 0 | 145 | course schedule iii | 630 | 0.402 | Hard | 10,584 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/1495460/PYTHON-SOLUTION-FASTER-THAN-88.58-OF-PYTHON-SUBMISSIONS | class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
k=len(nums)
maxx=-float('inf')
ans=[0,float('inf')]
heap=[]
for i in range(k):
heap.append((nums[i][0],i,0))
if nums[i][0]>maxx:maxx=nums[i][0]
heapq.heapify(heap)
... | smallest-range-covering-elements-from-k-lists | PYTHON SOLUTION FASTER THAN 88.58% OF PYTHON SUBMISSIONS | reaper_27 | 1 | 113 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,585 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/2727570/Python3-Straightforward-Min-Heap-(With-Comments) | class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
h = [] # min heap, keeps track of current range that fits 1+ numbers from all lists
lower, upper = float('inf'), float('-inf') # min and max of the current range
# initialize current range
for i, l in enume... | smallest-range-covering-elements-from-k-lists | Python3 Straightforward Min Heap (With Comments) | jonathanbrophy47 | 0 | 26 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,586 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/2671083/Python3-heap-solution | class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
curr_v = [(l[0], i, 0) for i, l in enumerate(nums)]
heapq.heapify(curr_v)
currmax = max(curr_v)[0]
minrange = currmax - curr_v[0][0]
minrange_value = [curr_v[0][0], currmax]
while ... | smallest-range-covering-elements-from-k-lists | Python3 heap solution | ellayu | 0 | 17 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,587 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/2585391/Python-Easy-to-understand-heap-solution | class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
k = len(nums)
# reverse each list so we can pop lowest item from them
for stack in nums: stack.reverse()
low,high = float('-inf'),float('inf')
counts = defaultdict... | smallest-range-covering-elements-from-k-lists | [Python] Easy to understand heap solution | fomiee | 0 | 48 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,588 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/2544300/Python3-Solution-or-Heap | class Solution:
def smallestRange(self, nums):
heap = [(k[0], 0, i) for i, k in enumerate(nums)]
heapq.heapify(heap)
cmax = max(k[0] for k in nums)
ans = (heap[0][0], cmax)
while len(nums[heap[0][2]]) != heap[0][1] + 1:
val, index, row = heapq.heappop(heap)
... | smallest-range-covering-elements-from-k-lists | ✔ Python3 Solution | Heap | satyam2001 | 0 | 35 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,589 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/1512454/Python3-Solution-with-using-heap | class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
smallest_range = 10 ** 6
_range = []
heap = []
cur_min, cur_max = 10 ** 6, - 10 ** 6
for idx, elems in enumerate(nums):
cur_min = min(cur_min, elems[-1])
cur... | smallest-range-covering-elements-from-k-lists | [Python3] Solution with using heap | maosipov11 | 0 | 60 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,590 |
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/1264007/Python3-priority-queue | class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
hi = -inf
pq = []
for i, num in enumerate(nums):
heappush(pq, (num[0], i, 0))
hi = max(hi, num[0])
ans = [-inf, inf]
while pq:
ans = min(ans, [pq[0][0],... | smallest-range-covering-elements-from-k-lists | [Python3] priority queue | ye15 | 0 | 45 | smallest range covering elements from k lists | 632 | 0.606 | Hard | 10,591 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/2203194/Python3-solution-using-two-pointers | class Solution:
def judgeSquareSum(self, c: int) -> bool:
low = 0
high = int(sqrt(c))
if high**2 == c:
return True
while low<=high:
x = low **2 + high **2
if x == c:
return True
if x > c:
... | sum-of-square-numbers | 📌 Python3 solution using two pointers | Dark_wolf_jss | 6 | 89 | sum of square numbers | 633 | 0.346 | Medium | 10,592 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/1711795/Two-pointer-solution-or-Python3 | class Solution:
def judgeSquareSum(self, c: int) -> bool:
first=0
last=int(sqrt(c))
if c<=2:
return True
while first<=last:
k=(first*first) + (last*last)
if k==c:
return True
elif k<c:
first=fir... | sum-of-square-numbers | Two pointer solution | Python3 | _SID_ | 4 | 217 | sum of square numbers | 633 | 0.346 | Medium | 10,593 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/2688387/Python3-Solution-or-Two-Pointer-or-O(c) | class Solution:
def judgeSquareSum(self, c):
l, r = 0, int(c ** 0.5)
while l <= r:
lhs = l*l + r*r
if lhs == c: return True
if lhs < c: l += 1
else: r -= 1
return False | sum-of-square-numbers | ✔ Python3 Solution | Two Pointer | O(√c) | satyam2001 | 1 | 194 | sum of square numbers | 633 | 0.346 | Medium | 10,594 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/2188648/Python-2-Simple-Solutions-Explained-oror-Two-Pointer-and-Math | class Solution(object):
def judgeSquareSum(self, c):
l,r = 0,int(sqrt(c))
while l<=r:
b = l+(r-1)//2
if b*b == c:
return True
elif b*b < c:
l=b+1
else:
r=b-1
return False | sum-of-square-numbers | Python 2 Simple Solutions Explained || Two-Pointer & Math | NathanPaceydev | 1 | 67 | sum of square numbers | 633 | 0.346 | Medium | 10,595 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/2188648/Python-2-Simple-Solutions-Explained-oror-Two-Pointer-and-Math | class Solution(object):
def judgeSquareSum(self, c):
root = int(sqrt(c))
for i in range(root+1):
if int(sqrt(c-i*i))**2+i**2 == c:
return True
return False | sum-of-square-numbers | Python 2 Simple Solutions Explained || Two-Pointer & Math | NathanPaceydev | 1 | 67 | sum of square numbers | 633 | 0.346 | Medium | 10,596 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/1954219/Python-Two-approaches-or-Two-Pointer-HashMap-Clean-and-Easy-Understand | class Solution:
def judgeSquareSum(self, c: int) -> bool:
low = 0
high = c
hashMap = {}
if c < 3:
return True
while low <= high:
mid = (low + high)//2
mul = mid * mid
if mul > c:
hi... | sum-of-square-numbers | [Python] Two approaches | Two Pointer, HashMap, Clean & Easy Understand | jamil117 | 1 | 87 | sum of square numbers | 633 | 0.346 | Medium | 10,597 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/1954219/Python-Two-approaches-or-Two-Pointer-HashMap-Clean-and-Easy-Understand | class Solution:
def judgeSquareSum(self, c: int) -> bool:
if c < 3:
return True
low = 0
high = int(sqrt(c))
while low <= high:
val = low * low + high * high
if val == c:
return True
elif val < c:
... | sum-of-square-numbers | [Python] Two approaches | Two Pointer, HashMap, Clean & Easy Understand | jamil117 | 1 | 87 | sum of square numbers | 633 | 0.346 | Medium | 10,598 |
https://leetcode.com/problems/sum-of-square-numbers/discuss/1425386/Python-Beats-100-runtime-91.33-memory | class Solution:
def judgeSquareSum(self, c: int) -> bool:
def prime_factors(n):
i = 2
factors = Counter()
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors[i] += 1
... | sum-of-square-numbers | [Python] Beats 100% runtime, 91.33% memory | mjgallag | 1 | 248 | sum of square numbers | 633 | 0.346 | Medium | 10,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.