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/duplicate-zeros/discuss/1604969/Clean-Python-Solution | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
adjust = 0
for index, number in enumerate(arr[:]):
if not number:
arr.insert(index + adjust, 0)
arr.pop()
adjust += 1 | duplicate-zeros | Clean Python Solution | migash | 0 | 93 | duplicate zeros | 1,089 | 0.515 | Easy | 17,300 |
https://leetcode.com/problems/duplicate-zeros/discuss/1488295/Python-3-Simple | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
i = 0
while (i < len(arr)):
if (arr[i] == 0):
arr.pop() #Delete last element when you counter a 0
arr.insert(i+1, 0) #insert a 0 in the next index
i += 1 # skip the 0 that you just adde... | duplicate-zeros | Python 3 Simple | huangzi234 | 0 | 219 | duplicate zeros | 1,089 | 0.515 | Easy | 17,301 |
https://leetcode.com/problems/duplicate-zeros/discuss/1476349/Simple-Python-Solution | class Solution:
def duplicateZeros(self, arr: list) -> None:
temp=-1
for idx,num in enumerate(arr):
if num==0 and temp!=idx:
temp=idx+1
arr.pop()
arr.insert(idx,0) | duplicate-zeros | Simple Python Solution | 1_d99 | 0 | 139 | duplicate zeros | 1,089 | 0.515 | Easy | 17,302 |
https://leetcode.com/problems/duplicate-zeros/discuss/1229353/Python-Solution-with-extra-array-and-without-extra-Array | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
extra_arr = []
i = 0
j=0
while j<len(arr):
extra_arr.append(arr[i])
if arr[i]==0:
j+=1
extra_arr.append(0)
i+=1
j+=1
for i in range(len(arr)):
arr[i] = extra_arr[i] | duplicate-zeros | [Python] Solution with extra array and without extra Array | arkumari2000 | 0 | 225 | duplicate zeros | 1,089 | 0.515 | Easy | 17,303 |
https://leetcode.com/problems/duplicate-zeros/discuss/1229353/Python-Solution-with-extra-array-and-without-extra-Array | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
n=len(arr)
i=0
while i<n:
if arr[i]==0:
arr.insert(i+1,0)
i+=1
i+=1
diff = len(arr)-n
while diff!=0:
arr.pop()
diff -=1 | duplicate-zeros | [Python] Solution with extra array and without extra Array | arkumari2000 | 0 | 225 | duplicate zeros | 1,089 | 0.515 | Easy | 17,304 |
https://leetcode.com/problems/duplicate-zeros/discuss/1157080/Python3-why-is-my-arr-is-returned-incorrectly-when-print()-shows-it-correct | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
idx, length = 0, len(arr)
while idx < length:
print(arr)
if arr[idx] == 0:
arr = arr[:idx]+[0]+arr[idx:-1]
idx+=2
else:
idx+=1 | duplicate-zeros | Python3 why is my arr is returned incorrectly when print() shows it correct? | CerBerUs9 | 0 | 46 | duplicate zeros | 1,089 | 0.515 | Easy | 17,305 |
https://leetcode.com/problems/duplicate-zeros/discuss/1124429/Python-OneLiner | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
arr = [int(x) for x in
"".join([str(s) for s in arr]).replace('0', '00')][0:len(arr)]
# Compiler doesn't pick it up correctly but you can run a pri... | duplicate-zeros | Python - OneLiner | user0264Wr | 0 | 123 | duplicate zeros | 1,089 | 0.515 | Easy | 17,306 |
https://leetcode.com/problems/duplicate-zeros/discuss/1115980/very-easy-way-to-finish-the-problem | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
items = len(arr)
item = 0
flag = False
while item <= items-1:
if arr[item] == 0:
arr.insert(item + 1, 0)
... | duplicate-zeros | very easy way to finish the problem | shreeyansh | 0 | 180 | duplicate zeros | 1,089 | 0.515 | Easy | 17,307 |
https://leetcode.com/problems/duplicate-zeros/discuss/1087138/Slow-(45)-but-simple-solution | class Solution:
def duplicateZeros(self, arr: list[int]) -> None:
i = 0
starting_len = len(arr) # this is used to remove all elements that exceed the starting len of arr
while i < len(arr):
if arr[i] == 0:
arr.insert(i + 1, 0) # inserts a 0 one place after the first 0
i += 2 # this is to stop infin... | duplicate-zeros | Slow (45%) but simple solution | Jamie_2345 | 0 | 93 | duplicate zeros | 1,089 | 0.515 | Easy | 17,308 |
https://leetcode.com/problems/duplicate-zeros/discuss/632734/JavaPython3-two-pointers | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
i, k = len(arr), len(arr) + arr.count(0)
while (i:=i-1) < (k:=k-1):
if k < len(arr): arr[k] = arr[i]
if arr[i] == 0:
... | duplicate-zeros | [Java/Python3] two pointers | ye15 | 0 | 148 | duplicate zeros | 1,089 | 0.515 | Easy | 17,309 |
https://leetcode.com/problems/duplicate-zeros/discuss/632734/JavaPython3-two-pointers | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
zeros = arr.count(0)
for i in reversed(range(len(arr))):
if i + zeros < len(arr):
arr[i+zeros] = arr[i]
if ... | duplicate-zeros | [Java/Python3] two pointers | ye15 | 0 | 148 | duplicate zeros | 1,089 | 0.515 | Easy | 17,310 |
https://leetcode.com/problems/duplicate-zeros/discuss/618603/56-ms-faster-than-99.64-and-14.4-MB-less-than-100.00-of-Python3 | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
i = 0
while i < len(arr):
if arr[i] == 0:
arr.insert(i,0)
arr.pop()
i+=2
else:
i += 1 | duplicate-zeros | 56 ms, faster than 99.64% and 14.4 MB, less than 100.00% of Python3 | zastavropoulos | 0 | 98 | duplicate zeros | 1,089 | 0.515 | Easy | 17,311 |
https://leetcode.com/problems/duplicate-zeros/discuss/507301/Python-Simple-One-Pass-Deque-Solution-O(n)-time-and-space | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
from collections import deque
i, queue = 0, deque()
array_length = len(arr)
while i < array_length:
if queue:
... | duplicate-zeros | Python Simple One Pass Deque Solution - O(n) time and space | zachtheclimber | 0 | 156 | duplicate zeros | 1,089 | 0.515 | Easy | 17,312 |
https://leetcode.com/problems/duplicate-zeros/discuss/456355/Simple-Python3-solution-with-list-comprehension-(68-ms) | class Solution:
def duplicateZeros(self, arr):
"""
Do not return anything, modify arr in-place instead.
"""
N = len(arr)
indices = [ idx for idx, val in enumerate(arr) if val==0 ]
for idx in reversed(indices):
arr.insert(idx,0)
arr[:] = arr[0:N] | duplicate-zeros | Simple Python3 solution with list comprehension (68 ms) | vietspaceanh | 0 | 64 | duplicate zeros | 1,089 | 0.515 | Easy | 17,313 |
https://leetcode.com/problems/duplicate-zeros/discuss/382626/Solution-in-Python-3-(beats-~100)-(five-lines) | class Solution:
def duplicateZeros(self, a: List[int]) -> None:
L, i = len(a), 0
while i < L:
if not a[i]:
i, _, _= i + 1, a.pop(), a.insert(i,0)
i += 1
- Junaid Mansuri
(LeetCode ID)@hotmail.com | duplicate-zeros | Solution in Python 3 (beats ~100%) (five lines) | junaidmansuri | 0 | 436 | duplicate zeros | 1,089 | 0.515 | Easy | 17,314 |
https://leetcode.com/problems/duplicate-zeros/discuss/377356/100-space-O(n)-based-on-shift | class Solution(object):
def duplicateZeros(self, arr):
"""
:type arr: List[int]
:rtype: None Do not return anything, modify arr in-place instead.
"""
if len(arr) == 1:
return arr
shift = 0
new_elements = [0] * len(arr)
for idx, ele in enume... | duplicate-zeros | 100% space, O(n), based on shift | xavloc | 0 | 133 | duplicate zeros | 1,089 | 0.515 | Easy | 17,315 |
https://leetcode.com/problems/duplicate-zeros/discuss/366029/Python3-solutions-O(n)-time-and-O(n2)-with-no-extra-space | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
length = len(arr)
input_idx = 0
result_idx = 0
result = [0] * length
while result_idx < length: # result_idx will either reac... | duplicate-zeros | Python3 solutions O(n) time and O(n^2) with no extra space | llanowarelves | 0 | 73 | duplicate zeros | 1,089 | 0.515 | Easy | 17,316 |
https://leetcode.com/problems/duplicate-zeros/discuss/366029/Python3-solutions-O(n)-time-and-O(n2)-with-no-extra-space | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
length = len(arr)
i = 0
while i < length:
if arr[i] == 0:
self.shift_and_add_zero(arr, i)
i += 1
... | duplicate-zeros | Python3 solutions O(n) time and O(n^2) with no extra space | llanowarelves | 0 | 73 | duplicate zeros | 1,089 | 0.515 | Easy | 17,317 |
https://leetcode.com/problems/duplicate-zeros/discuss/314489/In-Python3-what's-the-difference-between-arr-and-arr%3A | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
left=0
size=len(arr)
while left<=size-1:
if arr[left]!=0:
left+=1
elif arr[left]==0:
arr.insert(left,0)
left+=2
arr[:]=arr[:size] | duplicate-zeros | In Python3 what's the difference between arr and arr[:]? | jasperjoe | 0 | 104 | duplicate zeros | 1,089 | 0.515 | Easy | 17,318 |
https://leetcode.com/problems/duplicate-zeros/discuss/1443178/PYTHON3-oror-%22CHEATING%22-5-LINE | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
res, length = [], len(arr)
for num in arr:
if num: res.append(num)
else: res += [0, 0]
arr... | duplicate-zeros | PYTHON3 || "CHEATING" 5 LINE | shadowcatlegion | -1 | 104 | duplicate zeros | 1,089 | 0.515 | Easy | 17,319 |
https://leetcode.com/problems/duplicate-zeros/discuss/1352144/Python-solution-straight-forward | class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
temp = []
for i in arr:
if i == 0:
temp.append(0)
temp.append(0)
else:
temp.a... | duplicate-zeros | Python solution straight forward | tianshuhuang6 | -2 | 142 | duplicate zeros | 1,089 | 0.515 | Easy | 17,320 |
https://leetcode.com/problems/largest-values-from-labels/discuss/1025001/Python3-greedy-O(NlogN) | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], num_wanted: int, use_limit: int) -> int:
ans = 0
freq = {}
for value, label in sorted(zip(values, labels), reverse=True):
if freq.get(label, 0) < use_limit:
ans += value
... | largest-values-from-labels | [Python3] greedy O(NlogN) | ye15 | 2 | 161 | largest values from labels | 1,090 | 0.609 | Medium | 17,321 |
https://leetcode.com/problems/largest-values-from-labels/discuss/1025001/Python3-greedy-O(NlogN) | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], num_wanted: int, use_limit: int) -> int:
ans = 0
freq = defaultdict(int)
for value, label in sorted(zip(values, labels), reverse=True):
if 0 < num_wanted and freq[label] < use_limit:
... | largest-values-from-labels | [Python3] greedy O(NlogN) | ye15 | 2 | 161 | largest values from labels | 1,090 | 0.609 | Medium | 17,322 |
https://leetcode.com/problems/largest-values-from-labels/discuss/2739466/Python3-Using-Heaps-Commented | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], numWanted: int, useLimit: int) -> int:
# collect the highest values for each label
highest = collections.defaultdict(list)
# find the highest values
for label, value in zip(labels, values):
... | largest-values-from-labels | [Python3] - Using Heaps - Commented | Lucew | 0 | 2 | largest values from labels | 1,090 | 0.609 | Medium | 17,323 |
https://leetcode.com/problems/largest-values-from-labels/discuss/2158255/PYTHON-or-EXPLAINED-or-GREEDY-%2B-SORTING-or-EASY-or-O(n*logn)or | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], numWanted: int, useLimit: int) -> int:
n = len(values)
used = defaultdict(lambda:0)
ans = 0
combined = sorted([(values[i],labels[i]) for i in range(n)],reverse = True)
for value,label in com... | largest-values-from-labels | PYTHON | EXPLAINED | GREEDY + SORTING | EASY | O(n*logn)| | reaper_27 | 0 | 55 | largest values from labels | 1,090 | 0.609 | Medium | 17,324 |
https://leetcode.com/problems/largest-values-from-labels/discuss/2111756/python-3-oror-simple-greedy-sorting-solution | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], numWanted: int, useLimit: int) -> int:
items = sorted(((value, label) for value, label in zip(values, labels)), reverse=True)
score = 0
uses = collections.Counter()
for value, label in item... | largest-values-from-labels | python 3 || simple greedy sorting solution | dereky4 | 0 | 19 | largest values from labels | 1,090 | 0.609 | Medium | 17,325 |
https://leetcode.com/problems/largest-values-from-labels/discuss/1422486/Python3-solution-using-dictionary | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], num_wanted: int, use_limit: int) -> int:
n = []
d = {}
for i,j in enumerate(values):
n.append([j,labels[i]])
d[labels[i]] = use_limit
n.sort(key = lambda x:x[0],reverse = Tru... | largest-values-from-labels | Python3 solution using dictionary | EklavyaJoshi | 0 | 39 | largest values from labels | 1,090 | 0.609 | Medium | 17,326 |
https://leetcode.com/problems/largest-values-from-labels/discuss/868135/python3-items-to-list-sort-count-frequency-in-hashmap | class Solution:
# values: List[int]
# labels: List[int]
# num_wanted: int
# use_limit: int
def largestValsFromLabels(self, values, labels, num_wanted, use_limit) -> int:
# create item list of (value, label) tuples; sort
# iterate through items, count frequency in hashmap
# if... | largest-values-from-labels | python3 - items to list, sort, count frequency in hashmap | dachwadachwa | 0 | 65 | largest values from labels | 1,090 | 0.609 | Medium | 17,327 |
https://leetcode.com/problems/largest-values-from-labels/discuss/312749/Python3-straightforward-solution-with-explanation | class Solution:
def largestValsFromLabels(self, values: List[int], labels: List[int], num_wanted: int, use_limit: int) -> int:
import collections
d=collections.defaultdict(int)
tem=[]
res=[]
a=zip(values,labels)
for i,j in a:
tem.append([i,j])
tem=sorted(tem, key=lambda x:-x[0])
count=0
for i in ... | largest-values-from-labels | Python3 straightforward solution with explanation | jasperjoe | 0 | 56 | largest values from labels | 1,090 | 0.609 | Medium | 17,328 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2043228/Python-Simple-BFS-with-Explanation | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
# check if source and target are not clear cells
if grid[0][0] != 0 or grid[-1][-1] != 0:
return -1
N = len(grid)
# offsets required for all 8 directions
offsets... | shortest-path-in-binary-matrix | โ
Python Simple BFS with Explanation | constantine786 | 15 | 1,800 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,329 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2046009/This-is-why-Memoization-also-fails-(illustrated-example) | class Solution:
NO_CLEAR_PATH = -1
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
dirs = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
dp = [[None] * n for _ in range(n)]
def countCellsToTarget(i, j):
if i < ... | shortest-path-in-binary-matrix | This is why Memoization also fails (illustrated example) | rcomesan | 12 | 304 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,330 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1025014/Python3-BFS-O(N) | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
if grid[0][0] == 0:
ans = 0
grid[0][0] = 1
queue = deque([(0, 0)])
while queue:
ans += 1
for _ in range(len(queue)):
... | shortest-path-in-binary-matrix | [Python3] BFS O(N) | ye15 | 3 | 118 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,331 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2111074/python-3-oror-simple-bfs | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
if m == 1 and n == 1:
return 1 if not grid[0][0] else -1
if grid[0][0] or grid[m - 1][n - 1]:
return -1
directions = ((1, 0), (1,... | shortest-path-in-binary-matrix | python 3 || simple bfs | dereky4 | 2 | 195 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,332 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2006248/Python-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] or grid[-1][-1]:
return -1
n = len(grid)
q = deque([(0, 0, 1)])
grid[0][0] = 1
while q:
r, c, d = q.popleft()
if r == n - 1 and c == n ... | shortest-path-in-binary-matrix | Python, BFS | blue_sky5 | 2 | 119 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,333 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1906805/Template-for-these-kind-of-questions..oror-Python-oror-BFS-oror-without-Visited-set | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
r=len(grid)
c=len(grid[0])
# base case
if grid[0][0] or grid[r-1][c-1]:
return -1
# it's better to define the directioln first instead to define in the bfs ... | shortest-path-in-binary-matrix | Template for these kind of questions..|| Python || BFS || without Visited set | Shivam_007 | 2 | 123 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,334 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2158310/PYTHON-or-FAST-or-BFS-or-EASY-or-WELL-EXPLAINED-or | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
if grid[0][0] == 1 or grid[-1][-1] == 1: return -1
queue = [(0,0,1)]
grid[0][0] = 1
while queue:
row,col,dist = queue.pop(0)
if row == col == n-1 : return d... | shortest-path-in-binary-matrix | PYTHON | FAST | BFS | EASY | WELL EXPLAINED | | reaper_27 | 1 | 124 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,335 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1151264/WEEB-DOES-PYTHON-BFS-EASILY | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
row, col, queue = len(grid), len(grid[0]), deque([(0,0,1)])
if grid[0][0] == 1: return -1 # bruh, if it aint 0 then it aint gonna work
while queue:
x, y, steps = queue.popleft()
if x == row-1 and y == col-1:
return step... | shortest-path-in-binary-matrix | WEEB DOES PYTHON BFS EASILY | Skywalker5423 | 1 | 186 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,336 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2849492/FASTEST-oror-BEATS-95-SUBMISSIONS-oror-EASIEST-oror-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
m,n=len(grid),len(grid[0])
q=deque()
dirs=[(0,1),(0,-1),(1,0),(-1,0),(-1,-1),(1,1),(-1,1),(1,-1)]
if grid[0][0]==0:
q.append((1,(0,0)))
grid[0][0]=1
while q:
... | shortest-path-in-binary-matrix | FASTEST || BEATS 95% SUBMISSIONS || EASIEST || BFS | Pritz10 | 0 | 1 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,337 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2804843/BFS.-In-place-modificationyu | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
layer = [(0,0)]
n = len(grid)
if grid[0][0] == 1:
return -1
grid[0][0] = -1
while layer:
n_layer = []
for (i,j) in layer:
for x in [-1,0,1]:
... | shortest-path-in-binary-matrix | BFS. In-place modificationั | Pavel_Kos | 0 | 5 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,338 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2788623/Python-optimised-bfs-solution-with-time-complexity | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
# bfs approach
# O(n), O(n)
# where n = rows * cols or size of grid
rows, cols = len(grid), len(grid[0])
if grid[0][0] or grid[-1][-1]:
return -1
queue = collectio... | shortest-path-in-binary-matrix | Python optimised bfs solution with time complexity | sahilkumar158 | 0 | 4 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,339 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2782476/BFS-for-fun-and-profit | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid) - 1
src, dest = (0,0), (n, n)
if grid[src[0]][src[1]] != 0 or grid[dest[0]][dest[1]] != 0:
return -1
frontier = deque([(src, 1)])
visited = set()
directions = ... | shortest-path-in-binary-matrix | BFS for fun and profit | godelbach | 0 | 5 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,340 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2707065/Simple-BFS-algorithm | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] == 1: return -1
n = len(grid)
distance = [[float("inf")]*len(grid) for _ in range(len(grid))]
directions = [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)]
distance... | shortest-path-in-binary-matrix | Simple BFS algorithm | shriyansnaik | 0 | 4 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,341 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2700240/Python-or-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
if grid[0][0] != 0 or grid[n-1][n-1] != 0:
return -1
stack = [[[0,0], 1]]
final_steps = 10000
visited = [0]*(n*n)
visited[0] = 1
while stack:
... | shortest-path-in-binary-matrix | Python | BFS | Arana | 0 | 9 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,342 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2660608/Python3-oror-easy-oror-BFS-oror-Dijkstra-algorithm-using-simple-queue | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if not grid:
return
if len(grid)==1 and grid[0][0]==0:
return 1
if (grid[0][0]==1 or grid[-1][-1]==1):
return -1
rowSize=len(grid)
colSize=len(grid)
... | shortest-path-in-binary-matrix | Python3 || easy || BFS || Dijkstra algorithm using simple queue | _soninirav | 0 | 6 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,343 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2363297/jw-1091.-Shortest-Path-in-Binary-Matrix | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] == 1:
return -1
len_grid = len(grid)-1
if len_grid == 0:
if grid[0][0] == 1:
return -1
else:
return 1
d = ((-1, 0), (1, ... | shortest-path-in-binary-matrix | jw - 1091. Shortest Path in Binary Matrix | gozj32 | 0 | 8 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,344 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2287341/Python-BFS-beginner-firendly-solution | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if 1 in [grid[0][0], grid[-1][-1]]:
return -1
visited = set((0, 0))
q = collections.deque([(0, 0, 1)])
n = len(grid)
directions = [(0, 1), (1, 0), (-1, 0), (0, -1), (1, 1), ... | shortest-path-in-binary-matrix | Python BFS, beginner firendly solution | scr112 | 0 | 96 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,345 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2171104/why-my-solution-is-giving-TLE-passing-the-test-cases | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0]==1:
return -1
li = []
li.append([0,0])
r = len(grid)
c = len(grid[0])
if r==1 and c==1:
return 1
ans = 1... | shortest-path-in-binary-matrix | why my solution is giving TLE, passing the test cases | abhineetsingh192 | 0 | 37 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,346 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2044266/Literal-BFS-in-Python-3 | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
from itertools import product
from collections import deque
# 8-directional ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
dirs = tuple(((x, y) for x, y in product(range... | shortest-path-in-binary-matrix | Literal BFS in Python 3 | mousun224 | 0 | 40 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,347 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2043806/Python-BFS-Dijkstra-Easy-understand | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] == 1: return -1
m, n = len(grid), len(grid[0])
heap = [(1, 0, 0)] # length, starting_x, starting_y
seen = set()
seen.add((0, 0))
while heap:
length, x, y = heap... | shortest-path-in-binary-matrix | Python BFS Dijkstra Easy-understand | Kennyyhhu | 0 | 59 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,348 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2043432/Python-solution-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid):
N = len(grid)
dirs = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
queue = deque([(1, 0, 0)]) if grid[0][0] == 0 else deque()
visit = set()
while queue:
dist, x, y = queue.popleft()
... | shortest-path-in-binary-matrix | Python solution BFS | saladino | 0 | 20 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,349 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/2043326/Python3-or-Simple-or-Easy-to-Understand-or-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
n = len(grid)
if grid[0][0] == 1 or grid[n-1][n-1] == 1:
return -1
return self.bfs(grid, n)
def bfs(self, grid, n):
queue = [(0, 0)]
minD = [[10**9 for _ in range(n)] fo... | shortest-path-in-binary-matrix | Python3 | Simple | Easy to Understand | BFS | H-R-S | 0 | 27 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,350 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1929842/BFS-Python-Time-Limit-Exceeding | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
row_length = len(grid)
column_length = len(grid[0])
if grid[0][0] == 1:
return -1
adj = {}
for row in range(row_length):
for column in range(co... | shortest-path-in-binary-matrix | BFS Python Time Limit Exceeding | authier | 0 | 96 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,351 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1878617/85-faster-or-45-less-mem-or-BFS-or-easy | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
if grid[0][0] == 1 or grid[ROWS-1][COLS-1] == 1:
return -1
moves = [[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1]]
visited = set... | shortest-path-in-binary-matrix | 85% faster | 45% less mem | BFS | easy | aamir1412 | 0 | 39 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,352 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1787241/Python-easy-to-read-and-understand-or-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
if grid[0][0] == 1 or grid[m-1][n-1] == 1:
return -1
q = [(0, 0)]
grid[0][0] = 1
ans = 1
while q:
for i in range(len(q)):
... | shortest-path-in-binary-matrix | Python easy to read and understand | BFS | sanial2001 | 0 | 154 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,353 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1679083/Easy-to-understand-BFS-using-python3 | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
neighbors = {(i, j) for i in range(-1, 2) for j in range(-1, 2)}
n = len(grid)
if grid[0][0] == 1:
return -1
q = [(0, 0, 1)] #position i, position j, number of hop... | shortest-path-in-binary-matrix | Easy to understand BFS using python3 | dylwu | 0 | 187 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,354 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1485163/Python-BFS-using-value-function | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[-1][-1] == 1 or grid[0][0] == 1:
return -1
current_positions = set([(0, 0)])
value_function = [[-1] * len(grid) for i in range(len(grid))]
value_function[0][0] = 1
... | shortest-path-in-binary-matrix | Python BFS using value function | ac_h_illes | 0 | 149 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,355 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1480653/Python-BFS-with-getAdjacent()-fully-detached | class Solution:
# outer loop: O(n); inner loop: O(1) as max 8 directions
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
if grid[0][0] == 1:
return -1
queue = [[0, 0]]
grid[0][0] = 1
while len(queue) != 0:
path = queue.p... | shortest-path-in-binary-matrix | Python BFS with getAdjacent() fully detached | SleeplessChallenger | 0 | 53 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,356 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1445153/Python3Python-Solution-using-BFS-w-comments | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
# Init
n = len(grid)
min_distance = float("inf")
start = (0,0)
end = (n-1,n-1)
# Function to get all 8 directions of the current cell
def directions(r: int, c: i... | shortest-path-in-binary-matrix | [Python3/Python] Solution using BFS w/ comments | ssshukla26 | 0 | 80 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,357 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1078060/Python-Djkstra-Simple | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
dist = [[sys.maxsize for x in range(len(grid[0]))] for x in range(len(grid))]
minHeap = []
dist[0][0] = 1
directions = [(-1,-1), (1,1), (-1,0), (0,-1), (1,0), (0,1), (-1,1), (1,-1)]
if grid[0][0... | shortest-path-in-binary-matrix | Python Djkstra Simple | eastwoodsamuel4 | 0 | 207 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,358 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1069339/Python-easy-solution-using-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
N,M = len(grid), len(grid[0])
if grid[0][0] == 1:
return -1
q = deque()
directions = [(-1,1),(0,1),(1,0),(1,1),(0,-1),(-1,0),(-1,-1),(1,-1)]
if grid[0][0] == 0:
... | shortest-path-in-binary-matrix | Python easy solution using BFS | Namangarg98 | 0 | 178 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,359 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/796308/Python-3-or-BFS-or-Explanation | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
ans, m, n = 0, len(grid), len(grid[0])
if grid[0][0] == 1 or grid[m-1][n-1] == 1: return -1
q = [(0, 0)]
while q:
tmp_q = []
ans += 1
while q:
x, y = ... | shortest-path-in-binary-matrix | Python 3 | BFS | Explanation | idontknoooo | 0 | 265 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,360 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/751691/Python-Easy-to-Read-and-Understand-BFS-beats-85-with-Comments! | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
# rows will == cols here as we're told its n*n
rows = len(grid)
cols = len(grid[0])
# Check validity of grid, make sure we can start and finish.
if not grid or grid[0][0] != 0 or grid[-1][-1] !=... | shortest-path-in-binary-matrix | Python Easy to Read and Understand BFS beats 85% with Comments! | Pythagoras_the_3rd | 0 | 152 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,361 |
https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/1811165/Python-simple-multisource-BFS | class Solution:
def shortestPathBinaryMatrix(self, grid: List[List[int]]) -> int:
N, M = len(grid), len(grid[0])
directions = ((1,1),(1,0),(0,1),(1,-1),(-1,1),(0,-1),(-1,0),(-1,-1))
Coordinate = namedtuple('Coordinate', ['x', 'y'])
start = Coordinate(0,0)
goal = Coor... | shortest-path-in-binary-matrix | Python simple multisource BFS | Rush_P | -1 | 61 | shortest path in binary matrix | 1,091 | 0.445 | Medium | 17,362 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/786544/Simple-Python-Accepted-Solution-using-LCS-implementation-faster-than-83-python-users | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
n,m = len(str1),len(str2)
dp = [[0 for j in range(m+1)]for i in range(n+1)]
for i in range(1,n+1):
for j in range(1,m+1):
if str1[i-1] == str2[j-1]:
dp[i][j] = ... | shortest-common-supersequence | Simple Python Accepted Solution using LCS implementation faster than 83% python users | theflash007 | 7 | 323 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,363 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1479075/DPIntuitive-Solution-with-slight-variation-of-LCS | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
m = len(str1)
n = len(str2)
# construct the dp table
t = [[0 for j in range(n + 1)] for i in range(m + 1)]
for i in range(1, m+1):
for j in range(1, n+1):
... | shortest-common-supersequence | [DP][Intuitive] Solution with slight variation of LCS | nandanabhishek | 3 | 215 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,364 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2464477/Hard-made-Easy-Python-solution-LCS-Easy-to-Understand | class Solution:
def LCS(self,s,t):
m=len(s)
n=len(t)
dp=[[0 for i in range(n+1)]for j in range(m+1)]
for i in range(m+1):
dp[i][0]=0
for j in range(n+1):
dp[0][j]=0
for i in range(1,m+1):
for j in range(1,n+1):
if s[... | shortest-common-supersequence | Hard made Easy Python solution LCS Easy to Understand | adarshg04 | 2 | 100 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,365 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2086630/Python-Solution | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
a=str1
b=str2
m=len(a)
n=len(b)
dp =([[0 for i in range(n + 1)] for i in range(m + 1)])
for i in range(1,m+1):
for j in range(1,n+1):
if a[i-1]==b[j-1]:
... | shortest-common-supersequence | Python Solution | a_dityamishra | 1 | 53 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,366 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1893922/Python-Beginner-Easy-Fast-solution-using-LCS | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
m=len(str1)
n=len(str2)
ans=""
#initialising DP
dp=[[0]*(n+1) for i in range(m+1)]
#filling DP table by finding LCS
for i in range(1,m+1):
for j in range(1,n+1):
if str1[i-1]==str2[j-1]:
dp[i][j]=1+dp[i-1]... | shortest-common-supersequence | [Python] Beginner Easy , Fast solution using LCS | RaghavGupta22 | 1 | 139 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,367 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1463754/python-3-oror-dp-solution-oror-3-parts | class Solution:
def shortestCommonSupersequence(self, x: str, y: str) -> str:
n=len(x)
m=len(y)
dp=[[-1]*(m+1)for i in range (n+1)]
#1.length of longest common subsequence of x and y
for i in range(n+1):
for j in range(m+1):
#base conditi... | shortest-common-supersequence | python 3 || dp solution || 3 parts | minato_namikaze | 1 | 110 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,368 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2767919/Dynamic-Programming-Python-Solution-using-LCS | class Solution:
def LCS(self,str1,str2):
m = len(str1)
n = len(str2)
t = [[-1 for i in range(n + 1)] for j in range(m + 1)]
for i in range(m+1):
t[i][0] = 0
for j in range(n+1):
t[0][j] = 0
for i in range(1,m+1):
for j in range(1,n+1):
if str1[i-1] == str2[j-1]:
t[i][j]=1 + t[i-1][j-1]
... | shortest-common-supersequence | [Dynamic Programming] Python Solution using LCS | nikhitamore | 0 | 2 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,369 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2704903/Python3-or-LCS-Variation | class Solution:
def shortestCommonSupersequence(self, s1: str, s2: str) -> str:
n,m=len(s1),len(s2)
dp=[[0 for i in range(m+1)] for j in range(n+1)]
for i in range(1,n+1):
for j in range(1,m+1):
if s1[i-1]==s2[j-1]:
dp[i][j]=dp[i-1][j-1]+1
... | shortest-common-supersequence | [Python3] | LCS Variation | swapnilsingh421 | 0 | 1 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,370 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2666802/python-easy-LCS-methodp | class Solution:
def shortestCommonSupersequence(self, A: str, B: str) -> str:
dp={}
def solve(A,B,i,j):
if i==0 or j==0:
return ""
if (i,j) in dp:
return dp[(i,j)]
if A[i-1]==B[j-1]:
dp[(i,j)]= A[i-1]+solve(A,B,i-1,j... | shortest-common-supersequence | python easy LCS methodp | Akash_chavan | 0 | 5 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,371 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2584944/Top-Down-Approach-oror-DP-oror-Solution-with-Print-Length-of-SCS | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
def lcs(x,y,n,m):
#creating martix of n+1 and m+1
dp = [[-1 for _ in range(len(y)+1)] for _ in range(len(x)+1)]
for i in range(n+1):
for j in range(m+1):
... | shortest-common-supersequence | Top-Down Approach || DP || Solution with Print Length of SCS | ajinkyabhalerao11 | 0 | 23 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,372 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2249420/Shortest-Common-Supersequence | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
#found the LCS
subseq = self.LCS(str1,str2,len(str1),len(str2))
#created bool arrays to store the common letters and make markers
bool1 = [False for i in range(len(str1))]
bool2 = [Fa... | shortest-common-supersequence | Shortest Common Supersequence | ishitab_15 | 0 | 35 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,373 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/2163034/PYTHON-or-EASIEST-POSSIBLE-SOLUTION-or-USING-LCS-or-WELL-EXPLAINED-or | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
n1,n2 = len(str1), len(str2)
dp = [[0 for i in range(n2+1)] for j in range(n1+1)]
for i in range(1,n1+1):
for j in range(1,n2+1):
if str1[i-1] == str2[j-1] :dp[i][j] = dp[i-1][j-1]... | shortest-common-supersequence | PYTHON | EASIEST POSSIBLE SOLUTION | USING LCS | WELL EXPLAINED | | reaper_27 | 0 | 39 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,374 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1511128/Python3-solution | class Solution:
def shortestCommonSupersequence_0(self, str1: str, str2: str) -> str:
n1 = len(str1)
n2 = len(str2)
# boundary conditions
dp = [ [""]*(n1+1) for _ in range(n2+1) ]
for i in range(n1):
dp[0][i+1] = str1[:i+1]
for j in range(n2):
... | shortest-common-supersequence | Python3 solution | dalechoi | 0 | 107 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,375 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1511128/Python3-solution | class Solution:
# Runtime: 508 ms, faster than 56.63% of Python3
# Memory Usage: 17.4 MB, less than 94.06% of Python3
# two-level scheme
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
n1 = len(str1)
n2 = len(str2)
# base level set up
dp = [ ... | shortest-common-supersequence | Python3 solution | dalechoi | 0 | 107 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,376 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1439137/1092.-Shortest-Common-Supersequence-Python-DP-Solution | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
storage = self.getStorage(str1, str2)
s1 = len(str1)
s2 = len(str2)
shortestCommonSupersequence = ""
while s1 > 0 and s2 > 0:
if str1[s1 - 1] == str2[s2 - 1]:
shor... | shortest-common-supersequence | 1092. Shortest Common Supersequence - Python DP Solution | rohanpednekar_ | 0 | 108 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,377 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1227844/LCS-with-DP-oror-93-faster-oror-Well-explained-oror | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
def LCS(A,B):
m,n=len(A),len(B)
dp = [["" for _ in range(n+1)] for _ in range(m+1)]
for i in range(m):
for j in range(n):
if A[i]==B[j]:
dp[i+1][j+1]=dp[i][j]+A... | shortest-common-supersequence | ๐ {LCS with DP} || 93% faster || Well-explained || | abhi9Rai | 0 | 78 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,378 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1227378/Python-LCS-based-Simplest-Code | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
#Length of LCS
m=len(str1)
n=len(str2)
t=[[0 for i in range(n+1)] for j in range(m+1)]
for i in range(1,m+1):
for j in range(1,n+1):
if str1[i-1]==str2[j-1... | shortest-common-supersequence | Python LCS based Simplest Code | coder1311 | 0 | 30 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,379 |
https://leetcode.com/problems/shortest-common-supersequence/discuss/1179633/Python3-dp-and-greedy | class Solution:
def shortestCommonSupersequence(self, str1: str, str2: str) -> str:
@lru_cache(None)
def fn(i, j):
"""Return min length of common supersequence of str1[i:] and str2[j:]."""
if i == len(str1): return len(str2)-j
if j == len(str2): return l... | shortest-common-supersequence | [Python3] dp & greedy | ye15 | 0 | 62 | shortest common supersequence | 1,092 | 0.578 | Hard | 17,380 |
https://leetcode.com/problems/statistics-from-a-large-sample/discuss/1653119/Python3-one-liner | class Solution:
def sampleStats(self, count: List[int]) -> List[float]:
return [
#Minimum
min(i for i,c in enumerate(count) if c != 0),
#Maximum
max(i for i,c in enumerate(count) if c != 0),
#Mean
sum(i*c for i,c in enumerate(count)) / ... | statistics-from-a-large-sample | Python3 one-liner | pknoe3lh | 0 | 179 | statistics from a large sample | 1,093 | 0.444 | Medium | 17,381 |
https://leetcode.com/problems/statistics-from-a-large-sample/discuss/1485170/Python3-solution | class Solution:
def sampleStats(self, count: List[int]) -> List[float]:
running_sum = 0
min_elem = 300
max_elem = -1
most_frequent_value, max_freq = None, 0
current_idx = 0
all_counter = sum(count)
medium_idx = all_counter // 2
medium_sum = 0
... | statistics-from-a-large-sample | Python3 solution | ac_h_illes | 0 | 148 | statistics from a large sample | 1,093 | 0.444 | Medium | 17,382 |
https://leetcode.com/problems/statistics-from-a-large-sample/discuss/1025177/Python3-statistics | class Solution:
def sampleStats(self, count: List[int]) -> List[float]:
total = sum(count)
mn = med0 = med1 = -1
psm = cnt = mode = 0
for i, x in enumerate(count):
if x:
if mn < 0: mn = i
mx = i
psm += i * x
... | statistics-from-a-large-sample | [Python3] statistics | ye15 | 0 | 119 | statistics from a large sample | 1,093 | 0.444 | Medium | 17,383 |
https://leetcode.com/problems/car-pooling/discuss/1669593/Python3-STRAIGHTFORWARD-()-Explained | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
path = [0]*1000
for num, a, b in trips:
for loc in range (a, b):
path[loc] += num
if path[loc] > capacity: return False
return True | car-pooling | โค [Python3] STRAIGHTFORWARD (โฟโ โฟโ ), Explained | artod | 11 | 826 | car pooling | 1,094 | 0.573 | Medium | 17,384 |
https://leetcode.com/problems/car-pooling/discuss/2158946/PYTHON-or-SIMPLE-INUTITIVE-APPROACH-or-EXPLANATION-WITH-PICTURES-or | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
trips.sort(key = lambda x:x[2])
stations = trips[-1][-1]
people = [0]*(stations+1)
for count , start , end in trips:
people[start] += count
people[end] -= count
if peo... | car-pooling | PYTHON | SIMPLE INUTITIVE APPROACH | EXPLANATION WITH PICTURES | | reaper_27 | 3 | 79 | car pooling | 1,094 | 0.573 | Medium | 17,385 |
https://leetcode.com/problems/car-pooling/discuss/2640751/Python-O(N)-Very-simple-to-understand | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
passangers = [0]*1001
# lazy propagation
for count, fr, to in trips:
if count > capacity: return False # optimization 1
passangers[fr] += count
passangers[to] -= count
... | car-pooling | Python O(N) Very simple to understand | ya332 | 1 | 94 | car pooling | 1,094 | 0.573 | Medium | 17,386 |
https://leetcode.com/problems/car-pooling/discuss/2164302/Python-sort | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
stops = []
for num, from_, to in trips:
stops.append((from_, num))
stops.append((to, -num))
stops.sort()
passengers = 0
for _, nu... | car-pooling | Python, sort | blue_sky5 | 1 | 40 | car pooling | 1,094 | 0.573 | Medium | 17,387 |
https://leetcode.com/problems/car-pooling/discuss/1866612/10-lines-code-or-O(nlogn)-or-minHeap-or-very-easy-or | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
uber = []
for psg, board, dest in trips: #No. of passengers, start(boarding), end(destination)
uber.append([board, psg])
uber.append([dest, -psg])
heapq.heapify(uber)... | car-pooling | 10 lines code | O(nlogn) | minHeap | very easy | | aamir1412 | 1 | 64 | car pooling | 1,094 | 0.573 | Medium | 17,388 |
https://leetcode.com/problems/car-pooling/discuss/1808070/The-simplest-python-solution-(no-sort) | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
counts = [0 for _ in range(1001)] # 0 ~ 1000
for num, f, t in trips:
for i in range(f, t):
counts[i] += num
return max(counts) <= capacity | car-pooling | The simplest python solution (no sort) | byuns9334 | 1 | 43 | car pooling | 1,094 | 0.573 | Medium | 17,389 |
https://leetcode.com/problems/car-pooling/discuss/1670807/Python-Prefix-Sum-Solution | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
last_drop=-1
for i in trips:
last_drop=max(last_drop,i[2])
events=[0]*(last_drop+1)
for pas,st,en in trips:
events[st]+=pas
events[en]-=pas
if ... | car-pooling | Python Prefix Sum Solution | aryanagrawal2310 | 1 | 65 | car pooling | 1,094 | 0.573 | Medium | 17,390 |
https://leetcode.com/problems/car-pooling/discuss/1568472/Python-No-heapNo-sort-No-queuejust-use-list | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
ans = [0] * 1001
for i in trips:
n, l, r = i[0], i[1], i[2]
ans[l] += n
ans[r] -= n
pre = 0
for j in range(len(ans)):
pre += ans[j]
if pre ... | car-pooling | Python/ No heap/No sort/ No queue/just use list | zixin123 | 1 | 62 | car pooling | 1,094 | 0.573 | Medium | 17,391 |
https://leetcode.com/problems/car-pooling/discuss/1546470/Easy-to-understand-oror-99.48-faster-oror-Greedy-. | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
seen = set()
dp = defaultdict(int)
for n,a,b in trips:
dp[a]+=n
dp[b]-=n
seen.add(a)
seen.add(b)
seen = sorted(list(seen))
occ = 0
for p in seen:
occ+=dp[p]
... | car-pooling | ๐๐ Easy-to-understand || 99.48% faster || Greedy .๐ | abhi9Rai | 1 | 148 | car pooling | 1,094 | 0.573 | Medium | 17,392 |
https://leetcode.com/problems/car-pooling/discuss/843564/Python-3-(Py3.8)-or-Sweep-Line-Heap-or-Explanation-(Meeting-Room-II) | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
heap, cur = [], 0
for n, s, e in trips:
heapq.heappush(heap, (s, n))
heapq.heappush(heap, (e, -n))
while heap:
if (cur := cur + heapq.heappop(heap)[1]) > capacity: return ... | car-pooling | Python 3 (Py3.8) | Sweep Line, Heap | Explanation (Meeting Room II) | idontknoooo | 1 | 121 | car pooling | 1,094 | 0.573 | Medium | 17,393 |
https://leetcode.com/problems/car-pooling/discuss/2806004/python-solution-using-hashmap | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
hash_dict = {}
for i in trips:
s = i[1]
e = i[2]
for j in range(s , e):
if j in hash_dict:
hash_dict[j] += i[0]
else:
... | car-pooling | python solution using hashmap | akashp2001 | 0 | 5 | car pooling | 1,094 | 0.573 | Medium | 17,394 |
https://leetcode.com/problems/car-pooling/discuss/2795792/Two-approaches%3A-O(N)-without-sorting-or-binary-search. | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
self.max_len = 1001
self.max_pickup, self.max_drop = 0, 0
self.pickuplist = [0]*self.max_len
self.droplist = [0]*self.max_len
for i in range(len(trips)):
trip = trips[i]
... | car-pooling | โ
Two approaches: O(N) without sorting or binary search. | sulabhkatiyar | 0 | 2 | car pooling | 1,094 | 0.573 | Medium | 17,395 |
https://leetcode.com/problems/car-pooling/discuss/2795792/Two-approaches%3A-O(N)-without-sorting-or-binary-search. | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
self.max_len = 1001
self.max_drop = 0
self.pax_list = [0]*self.max_len
for i in range(len(trips)):
trip = trips[i]
num_pax, pickup, drop = trip[0], trip[1], trip[2]
... | car-pooling | โ
Two approaches: O(N) without sorting or binary search. | sulabhkatiyar | 0 | 2 | car pooling | 1,094 | 0.573 | Medium | 17,396 |
https://leetcode.com/problems/car-pooling/discuss/2786536/Prefix-sum-solution-by-UC-Berkeley-Computer-Science-Honor-Society. | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
"""
we want to calculate the maximum number of passengers at each interval.
since fromi and toi are both within 1000, we can use prefix sums!
let pref[i] represent the maximum number of passenger... | car-pooling | Prefix sum solution by UC Berkeley Computer Science Honor Society. | berkeley_upe | 0 | 6 | car pooling | 1,094 | 0.573 | Medium | 17,397 |
https://leetcode.com/problems/car-pooling/discuss/2739759/difference-array | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
res = [0 for _ in range(1001)]
for trip in trips:
res[trip[1]] += trip[0]
res[trip[2]] -= trip[0]
temp = 0
for i in range(1001):
temp += res[i]
if temp... | car-pooling | difference array | kuroko_6668 | 0 | 2 | car pooling | 1,094 | 0.573 | Medium | 17,398 |
https://leetcode.com/problems/car-pooling/discuss/2736993/Delta-matters | class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
delta = [0]*1001
for trip in trips:
delta[trip[1]] += trip[0]
delta[trip[2]] -= trip[0]
# Traverse spots in ascending order, check if cap > capacity
cap = 0
... | car-pooling | Delta matters | KKCrush | 0 | 7 | car pooling | 1,094 | 0.573 | Medium | 17,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.