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/reverse-string-ii/discuss/1477226/Simple-or-Python-3-or-24-ms-faster-than-97.61
class Solution: def reverseStr(self, s: str, k: int) -> str: return ''.join(s[i:i+k][::-1] + s[i+k:i+k*2] for i in range(0, len(s), k*2))
reverse-string-ii
Simple | Python 3 | 24 ms, faster than 97.61%
deep765
2
205
reverse string ii
541
0.505
Easy
9,500
https://leetcode.com/problems/reverse-string-ii/discuss/1400209/One-multi-line-97-speed
class Solution: def reverseStr(self, s: str, k: int) -> str: return "".join(w if i % 2 else w[::-1] for i, w in enumerate([s[i * k: (i + 1) * k] for i in range(len(s) // k + 1)]))
reverse-string-ii
One multi-line, 97% speed
EvgenySH
2
200
reverse string ii
541
0.505
Easy
9,501
https://leetcode.com/problems/reverse-string-ii/discuss/1371012/Python-20ms-faster-than-99.64
class Solution: def reverseStr(self, s: str, k: int) -> str: result = "" for i in range(0, len(s), k*2): result += s[i:i+k][::-1] + s[i+k:i+k*2] return result
reverse-string-ii
Python, 20ms, faster than 99.64%
SG5
2
172
reverse string ii
541
0.505
Easy
9,502
https://leetcode.com/problems/reverse-string-ii/discuss/1272171/Easy-Python-Solution(97.20)
class Solution: def reverseStr(self, s: str, k: int) -> str: s=list(s) for i in range(0,len(s),2*k): # print(s[i:i+k:-1]) s[i:i+k]=reversed(s[i:i+k]) return "".join(s)
reverse-string-ii
Easy Python Solution(97.20%)
Sneh17029
2
361
reverse string ii
541
0.505
Easy
9,503
https://leetcode.com/problems/reverse-string-ii/discuss/1145073/Easy-python-solution-O(N)-Time-O(N)-Space
class Solution: def reverseStr(self, s: str, k: int) -> str: if k >= len(s): return s[::-1] i = 0 s = list(s) while i < len(s): l = i h = (i + k - 1) if (i + k - 1) < len(s) else len(s) - 1 while l < len(s) and l < h: ...
reverse-string-ii
Easy python solution O(N) Time, O(N) Space
vanigupta20024
2
194
reverse string ii
541
0.505
Easy
9,504
https://leetcode.com/problems/reverse-string-ii/discuss/2559705/Simple-Python-recursion-solution-(Beginner-Friendly)
class Solution: def reverseStr(self, s: str, k: int) -> str: def reverse1(s, k): if len(s)<k: return s[::-1] if len(s)>=k and len(s)<=2*k: s1 = s[:k]; s2 = s[k:] s1 = list(s1) i = 0; j = len(s1)-1 while(i...
reverse-string-ii
Simple Python recursion solution (Beginner Friendly)
Arana
1
125
reverse string ii
541
0.505
Easy
9,505
https://leetcode.com/problems/reverse-string-ii/discuss/1969603/easy-python-solution
class Solution: def reverseStr(self, s: str, k: int) -> str: s_rev, i = '', 0 while i<len(s): s_rev += (s[i:i+k])[::-1] + s[i+k:i+2*k] i += 2*k return s_rev
reverse-string-ii
easy python solution
wezel
1
159
reverse string ii
541
0.505
Easy
9,506
https://leetcode.com/problems/reverse-string-ii/discuss/1902410/Python3-or-2-solution-or-Recursively-or-Iteratively
class Solution: def reverseStr(self, s: str, k: int) -> str: return s[:k][::-1] + s[k:2*k] + self.reverseStr(s[2*k:], k) if s else ""
reverse-string-ii
✔Python3 | 2 solution | Recursively | Iteratively
Anilchouhan181
1
128
reverse string ii
541
0.505
Easy
9,507
https://leetcode.com/problems/reverse-string-ii/discuss/1902410/Python3-or-2-solution-or-Recursively-or-Iteratively
class Solution: def reverseStr(self, s: str, k: int) -> str: if k>len(s): return s[::-1] for i in range(0,len(s),2*k): s=s[:i]+s[i:i+k][::-1]+s[k+i:] return s
reverse-string-ii
✔Python3 | 2 solution | Recursively | Iteratively
Anilchouhan181
1
128
reverse string ii
541
0.505
Easy
9,508
https://leetcode.com/problems/reverse-string-ii/discuss/1821576/Python3-28ms-faster-than-96.94
class Solution: def reverseStr(self, s: str, k: int) -> str: out = '' reverse = True for i in range(0, len(s)+1, k): out += s[i:i+k][::-1 if reverse else 1] reverse = not reverse return out
reverse-string-ii
Python3 28ms, faster than 96.94%
Minh4893IT
1
160
reverse string ii
541
0.505
Easy
9,509
https://leetcode.com/problems/reverse-string-ii/discuss/1546351/Python3-Simple-Solution-but-not-fast
class Solution: def reverseStr(self, s: str, k: int) -> str: for idx in range(0, len(s), k * 2): s = s[:idx] + s[idx:idx+k][::-1] + s[idx+k:] return s
reverse-string-ii
[Python3] Simple Solution but not fast
JingMing_Chen
1
122
reverse string ii
541
0.505
Easy
9,510
https://leetcode.com/problems/reverse-string-ii/discuss/1222858/Python3-simple-solution
class Solution: def reverseStr(self, s: str, k: int) -> str: flag = True res = '' x = '' c = 0 for i in range(len(s)): if flag: if c < k: x = s[i] + x elif c == k: flag = False ...
reverse-string-ii
Python3 simple solution
EklavyaJoshi
1
76
reverse string ii
541
0.505
Easy
9,511
https://leetcode.com/problems/reverse-string-ii/discuss/1222858/Python3-simple-solution
class Solution: def reverseStr(self, s: str, k: int) -> str: flag = True res = '' for i in range(0,len(s),k): if flag: res += (s[i:i+k])[::-1] flag = False else: res += s[i:i+k] flag = True return...
reverse-string-ii
Python3 simple solution
EklavyaJoshi
1
76
reverse string ii
541
0.505
Easy
9,512
https://leetcode.com/problems/reverse-string-ii/discuss/1222858/Python3-simple-solution
class Solution: def reverseStr(self, s: str, k: int) -> str: res = '' for i in range(0,len(s),2*k): x = s[i:i+2*k] res += x[:k][::-1] + x[k:] return res
reverse-string-ii
Python3 simple solution
EklavyaJoshi
1
76
reverse string ii
541
0.505
Easy
9,513
https://leetcode.com/problems/reverse-string-ii/discuss/2833237/Python3
class Solution: def reverseStr(self, s: str, k: int) -> str: res = '' i = 0 while i < len(s): res += s[i : i + k][::-1] res += s[i + k: 2 * k + i] i += 2 * k return res
reverse-string-ii
Python3
adilet_1864
0
3
reverse string ii
541
0.505
Easy
9,514
https://leetcode.com/problems/reverse-string-ii/discuss/2806881/String-Function-Based-Recursive-Python-Solution-45-faster-than-rest-O(n2k)-Complexity
class Solution: def reverseStr(self, s: str, k: int) -> str: n=len(s) if n<k: return s[::-1] elif n<(2*k) and n>=k: s1=s[:k] s1=s1[::-1]+s[k:] return s1 else: s1=s[:2*k] s2=s1[:k] s2=s2[::-1]+s1[k:] ...
reverse-string-ii
String Function Based Recursive Python Solution 45% faster than rest O(n/2k) Complexity
SnehaGanesh
0
2
reverse string ii
541
0.505
Easy
9,515
https://leetcode.com/problems/reverse-string-ii/discuss/2786225/Reverse-String-2
class Solution: def reverseStr(self, s: str, k: int) -> str: return ''.join([s[i*k:(i+1)*k][::((-1)**(i+1))] for i in range(len(s)//k+1)])
reverse-string-ii
Reverse String 2
Luna-martinez
0
2
reverse string ii
541
0.505
Easy
9,516
https://leetcode.com/problems/reverse-string-ii/discuss/2749767/O(N)-time-or-O(1)-space-complexity
class Solution: def reverseStr(self, s: str, k: int) -> str: if len(s) < k: return s[::-1] i=0 ans = "" while i<len(s): l = 2*k+i # print(l) if l <= len(s): r = s[i:i+k] ans += r[::-1] ...
reverse-string-ii
O(N) time | O(1) space complexity
wakadoodle
0
9
reverse string ii
541
0.505
Easy
9,517
https://leetcode.com/problems/reverse-string-ii/discuss/2742606/Python-3-Solution
class Solution: def reverseStr(self, s: str, k: int) -> str: for i in range(0,len(s),2*k): if(i+k<len(s)): s=s[0:i]+s[i:i+k][::-1]+s[i+k:] else: s=s[0:i]+s[i:i+k][::-1] return s
reverse-string-ii
Python 3 Solution
dnvavinash
0
5
reverse string ii
541
0.505
Easy
9,518
https://leetcode.com/problems/reverse-string-ii/discuss/2739968/Python3-Recursive-Solution
class Solution: def reverseStr(self, s: str, k: int) -> str: if len(s)<k: return s[::-1] if len(s)<2*k: return s[:k][::-1]+s[k:] return s[:k][::-1]+s[k:2*k]+self.reverseStr(s[2*k:],k)
reverse-string-ii
Python3 Recursive Solution
jsheng
0
1
reverse string ii
541
0.505
Easy
9,519
https://leetcode.com/problems/reverse-string-ii/discuss/2710486/Python-Tri-liner
class Solution: def reverseStr(self, s: str, k: int) -> str: new_s = "" for i in range(0, len(s), 2*k): new_s += s[i : i+k][::-1] + s[i+k : i+2*k] return new_s
reverse-string-ii
Python Tri liner
code_snow
0
10
reverse string ii
541
0.505
Easy
9,520
https://leetcode.com/problems/reverse-string-ii/discuss/2697524/Python-simple-solution
class Solution: def reverseStr(self, s: str, k: int) -> str: s = [s[i:i+k] for i in range(0, len(s), k)] for i in range(0, len(s), 2): s[i] = s[i][::-1] return ''.join(s)
reverse-string-ii
Python simple solution
kruzhilkin
0
8
reverse string ii
541
0.505
Easy
9,521
https://leetcode.com/problems/reverse-string-ii/discuss/2675256/Easy-Python-Solution-oror-(Slicing)
class Solution: def reverseStr(self, s: str, k: int) -> str: for i in range(0,len(s),2*k): if(i+k<len(s)): s=s[0:i]+s[i:i+k][::-1]+s[i+k:] else: s=s[0:i]+s[i:i+k][::-1] return s
reverse-string-ii
Easy Python Solution || (Slicing)
Kiran_Rokkam
0
4
reverse string ii
541
0.505
Easy
9,522
https://leetcode.com/problems/reverse-string-ii/discuss/2650125/Python
class Solution: def reverseStr(self, s: str, k: int) -> str: s = list(s) fin = [] counter=0 def rev(res): l,r =0,len(res)-1 while(l<r): res[l],res[r] = res[r], res[l] l+=1 r-=1 return res for ...
reverse-string-ii
Python
naveenraiit
0
6
reverse string ii
541
0.505
Easy
9,523
https://leetcode.com/problems/reverse-string-ii/discuss/2611829/Simple-Implementation-or-Python
class Solution: def reverseStr(self, s: str, k: int) -> str: words = [s[i:i+2*k] for i in range(0, len(s),2*k)] for key, val in enumerate(words): if len(val)>= k: words[key] = val[:k][::-1] + val[k:] else: words[key] = val[::-1] ...
reverse-string-ii
Simple Implementation | Python
Abhi_-_-
0
30
reverse string ii
541
0.505
Easy
9,524
https://leetcode.com/problems/reverse-string-ii/discuss/2608278/Python-Solution
class Solution: def reverseStr(self, s: str, k: int) -> str: string = list(s) n = len(string) reverse = True for i in range(0, n, k): if reverse: left = i right = min(i + k - 1, n - 1) while left < right: ...
reverse-string-ii
Python Solution
mansoorafzal
0
82
reverse string ii
541
0.505
Easy
9,525
https://leetcode.com/problems/reverse-string-ii/discuss/2598984/Python-oror-Easy-and-well-explain-solution
class Solution(object): def reverseStr(self, s, k): """ :type s: str :type k: int :rtype: str """ for i in range(0,len(s), 2*k): if i==0: temp = s[i:k] temp = temp[::-1]+s[k:] s = temp else: ...
reverse-string-ii
Python || Easy and well explain solution
ride-coder
0
52
reverse string ii
541
0.505
Easy
9,526
https://leetcode.com/problems/reverse-string-ii/discuss/2519474/Python-O(n)-solution-simple-readable-code
class Solution: def reverseStr(self, s: str, k: int) -> str: counter = 0 ans = [c for c in s] # number of iterations that we will make in s iterations = len(s) // (2 * k) # if s is not perfectly divisible by 2k, add 1 extra iteration if len(s) % (2 * k): ...
reverse-string-ii
Python O(n) solution - simple readable code
ishaan06
0
42
reverse string ii
541
0.505
Easy
9,527
https://leetcode.com/problems/reverse-string-ii/discuss/2283319/Python-Fast-Solution
class Solution: def reverseStr(self, s: str, k: int) -> str: i = 0 res = '' while(i<len(s)): if i+k<len(s): res += (s[i:i+k])[::-1] i+=k if i+k<len(s): res+= s[i:i+k] i+=k ...
reverse-string-ii
Python Fast Solution
harsh30199
0
77
reverse string ii
541
0.505
Easy
9,528
https://leetcode.com/problems/reverse-string-ii/discuss/2281269/Python-fast-(beats-98.4-)-and-short-(almost-1-line)-solution-with-Python-3.8-features-(PEP572)
class Solution: def reverseStr(self, s: str, k: int) -> str: rev = False # ↓ PEP572 (assignment expressions + list comprehension, see Xavier Guihot's answer at https://stackoverflow.com/questions/16632124/how-to-emulate-sum-using-a-list-comprehension) return "".join([s[i:i...
reverse-string-ii
Python fast (beats 98.4 %) and short (almost 1 line) solution with Python 3.8 features (PEP572)
amaargiru
0
100
reverse string ii
541
0.505
Easy
9,529
https://leetcode.com/problems/reverse-string-ii/discuss/2155136/Python-or-Simple-Solution
class Solution: def reverseStr(self, s: str, k: int) -> str: ans = "" for i in range(0,len(s),2*k): ans+=s[i:i+k][::-1]+s[i+k:i+2*k] return ans;
reverse-string-ii
Python | Simple Solution
hemanthkakumanu1
0
117
reverse string ii
541
0.505
Easy
9,530
https://leetcode.com/problems/reverse-string-ii/discuss/1918234/Python-Faster-Than-97.32-Easy-To-Understand-Explained-Solution
class Solution: def reverseStr(self, s: str, k: int) -> str: res, flip, i = "", True, 0 while i <= len(s): if not flip: res += s[i : i + k] i += k flip ^= True else: res += s[i : i + k][::-1] ...
reverse-string-ii
Python Faster Than 97.32% Easy To Understand Explained Solution
Hejita
0
120
reverse string ii
541
0.505
Easy
9,531
https://leetcode.com/problems/reverse-string-ii/discuss/1762990/Simple-Python-Solution-oror-Faster-than-96
class Solution: def reverseStr(self, s: str, k: int) -> str: if len(s)<k: return s[::-1] elif len(s)>k and len(s)< 2*k: first_part = s[:k] second_part = s[k:] return first_part[::-1] + second_part else: ou...
reverse-string-ii
Simple Python Solution || Faster than 96%
Akhilesh_Pothuri
0
140
reverse string ii
541
0.505
Easy
9,532
https://leetcode.com/problems/reverse-string-ii/discuss/1723905/Python-Simple-or-Beats-100-in-Memory
class Solution: def reverseStr(self, s: str, k: int) -> str: op = [] for i in range(0,len(s),2*k): op.append(s[i:k+i][::-1]) op.append(s[k+i:i + (2*k)]) return "".join(op)
reverse-string-ii
Python Simple | Beats 100% in Memory
veerbhansari
0
114
reverse string ii
541
0.505
Easy
9,533
https://leetcode.com/problems/reverse-string-ii/discuss/1568905/Python-faster-than-98
class Solution: def reverseStr(self, s: str, k: int) -> str: res = '' for i in range(0, len(s), 2 * k): a = i + k b = i + 2*k res += s[i:a][::-1] + s[a:b] return res
reverse-string-ii
Python faster than 98%
dereky4
0
183
reverse string ii
541
0.505
Easy
9,534
https://leetcode.com/problems/reverse-string-ii/discuss/1482829/Literal-two-pointer-approach-in-Python
class Solution: def reverseStr(self, s: str, k: int) -> str: n, s = len(s), list(s) def tow_pointer_gen(): i, j = 0, k - 1 while i < n: yield i, j if j < n else n - 1 i += 2 * k j += 2 * k for l...
reverse-string-ii
Literal two-pointer approach in Python
mousun224
0
117
reverse string ii
541
0.505
Easy
9,535
https://leetcode.com/problems/reverse-string-ii/discuss/1298992/Python3-dollarolution
class Solution: def reverseStr(self, s: str, k: int) -> str: s = list(s) if len(s) < k: return (''.join(reversed(s))) for i in range(0,len(s),2*k): v = s[i:i+k] c = 1 for j in range(i,i+len(v)): s[j] = v[-c] c +=...
reverse-string-ii
Python3 $olution
AakRay
0
118
reverse string ii
541
0.505
Easy
9,536
https://leetcode.com/problems/reverse-string-ii/discuss/1265018/Python3-Solution-Straight-forward
class Solution: def reverseStr(self, s: str, k: int) -> str: s = list(s) for i in range(0, len(s), 2*k): s[i:i+k] = reversed(s[i:i+k]) return "".join(s)
reverse-string-ii
Python3 Solution Straight forward
Sanyamx1x
0
80
reverse string ii
541
0.505
Easy
9,537
https://leetcode.com/problems/reverse-string-ii/discuss/1211865/Python-or-3-liner
class Solution: def reverseStr(self, s: str, k: int) -> str: n = len(s) ans = [ s[i:i+k][::-1] + s[i+k:i+2*k] for i in range(0,n,2*k) ] return ''.join(ans)
reverse-string-ii
Python | 3 liner
Sanjaychandak95
0
54
reverse string ii
541
0.505
Easy
9,538
https://leetcode.com/problems/reverse-string-ii/discuss/1204568/Python-3-solution-Runtime%3A-20-ms-faster-than-99.26
class Solution: def reverseStr(self, s: str, k: int) -> str: res = '' for i in range(0, len(s), 2*k): # print(i, i+k, i+2*k, s[i: i+k], s[i+k: i+2*k]) res += s[i: i+k][::-1] + s[i+k: i+2*k] return res
reverse-string-ii
Python 3 solution Runtime: 20 ms, faster than 99.26%
hyiche
0
43
reverse string ii
541
0.505
Easy
9,539
https://leetcode.com/problems/reverse-string-ii/discuss/474545/Python3-93.30-(24-ms)100.00-(13.1-MB)-O(n)-time-O(n)-space-(output)-using-deque
class Solution: def reverseStr(self, s: str, k: int) -> str: ret = collections.deque() substring = collections.deque() counter = 0 to_reverse = True for letter in s: counter += 1 if (to_reverse): ...
reverse-string-ii
Python3 93.30% (24 ms)/100.00% (13.1 MB) -- O(n) time / O(n) space (output) -- using deque
numiek_p
0
114
reverse string ii
541
0.505
Easy
9,540
https://leetcode.com/problems/reverse-string-ii/discuss/438681/Python-3-100-time-100-memory
class Solution: def reverseStr(self, s: str, k: int) -> str: res = '' for i in range(len(s)//(2*k)): res += s[2*k*i:2*k*i+k][::-1] + s[2*k*i+k:2*k*(i+1)] remainder = len(s) % (2*k) if remainder <= k and remainder > 0: res += s[-remainder:][::...
reverse-string-ii
Python 3 100% time, 100% memory
ilee102
0
104
reverse string ii
541
0.505
Easy
9,541
https://leetcode.com/problems/01-matrix/discuss/1556018/WEEB-DOES-PYTHON-BFS
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: row, col = len(mat), len(mat[0]) queue = deque([]) for x in range(row): for y in range(col): if mat[x][y] == 0: queue.append((x, y, 1)) return self.bfs(row, col, queue, mat) def bfs(self, row, col, queue, grid): ...
01-matrix
WEEB DOES PYTHON BFS
Skywalker5423
7
414
01 matrix
542
0.442
Medium
9,542
https://leetcode.com/problems/01-matrix/discuss/2604794/Python-Solution-Faster-then-99.63
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: r=len(mat) c=len(mat[0]) for i in range(r): for j in range(c): if mat[i][j]!=0: top=mat[i-1][j] if i>0 else float('inf') left=mat[i][j-1] if j>...
01-matrix
Python Solution Faster then 99.63%
pranjalmishra334
3
408
01 matrix
542
0.442
Medium
9,543
https://leetcode.com/problems/01-matrix/discuss/741536/Easy-Python-BFS-Solution-with-Comments!
class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: if not matrix: return directions = ((1, 0), (-1, 0), (0, 1), (0, -1)) rows = len(matrix) cols = len(matrix[0]) # Iterate through our mtrx til we find vals != 0 for...
01-matrix
Easy Python BFS Solution with Comments!
Pythagoras_the_3rd
3
204
01 matrix
542
0.442
Medium
9,544
https://leetcode.com/problems/01-matrix/discuss/1919229/Python-BFS-Solution
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: queue = [] for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j] == 0: queue.append((i,j)) else: mat[i]...
01-matrix
Python BFS Solution
Brillianttyagi
2
186
01 matrix
542
0.442
Medium
9,545
https://leetcode.com/problems/01-matrix/discuss/1786045/Python-3-(700ms)-or-2-Traversal-Approach
class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: m, n = len(matrix), len(matrix and matrix[0]) for i in range(m): for j in range(n): if matrix[i][j] != 0: matrix[i][j] = float("inf") if i > 0 and m...
01-matrix
Python 3 (700ms) | 2 Traversal Approach
MrShobhit
2
281
01 matrix
542
0.442
Medium
9,546
https://leetcode.com/problems/01-matrix/discuss/874077/Python3-BFS-by-frontier
class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: m, n = len(matrix), len(matrix[0]) front = set((i, j) for i in range(m) for j in range(n) if not matrix[i][j]) # frontier seen = front.copy() # visited cell k = 0 while front: # bfs...
01-matrix
[Python3] BFS by frontier
ye15
2
144
01 matrix
542
0.442
Medium
9,547
https://leetcode.com/problems/01-matrix/discuss/874077/Python3-BFS-by-frontier
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m, n = len(mat), len(mat[0]) ans = [[inf]*n for _ in range(m)] for i in range(m): for j in range(n): if not mat[i][j]: ans[i][j] = 0 else: ...
01-matrix
[Python3] BFS by frontier
ye15
2
144
01 matrix
542
0.442
Medium
9,548
https://leetcode.com/problems/01-matrix/discuss/2387807/Python-Solution-Dynamic-Programmming-95-faster
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: rows = len(mat) cols = len(mat[0]) for i in range(rows): for j in range(cols): if mat[i][j] != 0: top = mat[i-1][j] if i>0 else float('inf') ...
01-matrix
Python Solution Dynamic Programmming, 95% faster
prachetshah26
1
167
01 matrix
542
0.442
Medium
9,549
https://leetcode.com/problems/01-matrix/discuss/2075926/Iterative-Tabulation-oror-EXPLAINED-oror-SIMPLE-FAST
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m=len(mat) n=len(mat[0]) dist = [[float('inf')]* n for _ in range(m)] for i in range(m): for j in range(n): if mat[i][j]==0: dist[i][j]=0 ...
01-matrix
Iterative Tabulation || EXPLAINED || SIMPLE FAST
karan_8082
1
79
01 matrix
542
0.442
Medium
9,550
https://leetcode.com/problems/01-matrix/discuss/1370034/DP-Solution-Explanation-with-Diagrams-%2B-Code-in-Python3
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: mat = mat n = len(mat) m = len(mat[0]) # init with +inf ans = [[float('inf')]*m for _ in range(n)] # from top left to bottom right, as we usually do for i in range(n): ...
01-matrix
DP Solution Explanation with Diagrams + Code in Python3
chaudhary1337
1
143
01 matrix
542
0.442
Medium
9,551
https://leetcode.com/problems/01-matrix/discuss/1356594/Elegant-Python-Iterative-BFS
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: ROWS, COLUMNS = len(mat), len(mat[0]) grid = [[0 for _ in range(COLUMNS)] for _ in range(ROWS)] queue = deque() visited = [[False for _ in range(COLUMNS)] for _ in range(ROWS)] for row ...
01-matrix
Elegant Python Iterative BFS
soma28
1
163
01 matrix
542
0.442
Medium
9,552
https://leetcode.com/problems/01-matrix/discuss/2831267/Python-Like-62.-Unique-Paths-but-also-from-Bottom-%2B-Right
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: ''' The idea is similar to Unique Paths, https://leetcode.com/problems/unique-paths/ in that, we get the minimum of the accumulated path totals from TOP + LEFT but here, we also then compare against accumulate...
01-matrix
[Python] Like 62. Unique Paths, but also from Bottom + Right
graceiscoding
0
3
01 matrix
542
0.442
Medium
9,553
https://leetcode.com/problems/01-matrix/discuss/2810966/Python-(Simple-Dynamic-Programming)
class Solution: def updateMatrix(self, mat): m, n, ans, visited = len(mat), len(mat[0]), [], set() for i in range(m): for j in range(n): if mat[i][j] == 0: ans.append((i,j,0)) visited.add((i,j)) dp = [[0]*n for _ ...
01-matrix
Python (Simple Dynamic Programming)
rnotappl
0
4
01 matrix
542
0.442
Medium
9,554
https://leetcode.com/problems/01-matrix/discuss/2801282/Python-3-BFS-simple-solution
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: def cell_n_neighbours (r, c): visited.add((r, c)) for dr_r, dr_c in drt: stack.append([r + dr_r, c + dr_c]) stack = deque() visited = set() drt = ...
01-matrix
Python 3 - BFS - simple solution
noob_in_prog
0
5
01 matrix
542
0.442
Medium
9,555
https://leetcode.com/problems/01-matrix/discuss/2726325/Python3-Intuitive-BFS
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: ROWS, COLS = len(mat), len(mat[0]) res = [[0] * COLS for _ in range(ROWS)] # get starting points q = deque() for r in range(ROWS): for c in range(COLS): if mat[r][c] ...
01-matrix
Python3 Intuitive BFS
jonathanbrophy47
0
15
01 matrix
542
0.442
Medium
9,556
https://leetcode.com/problems/01-matrix/discuss/2695576/Multi-source-BFS-Python-Easy-Solve
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: self.X = [1, 0, -1, 0] self.Y = [0, 1, 0, -1] vis = [[99999 for i in range(len(mat[0]))] for i in range(len(mat))] q = deque() for i in range(len(mat)): for j in range(len(mat[0])): ...
01-matrix
Multi source BFS Python Easy Solve
anu1rag
0
5
01 matrix
542
0.442
Medium
9,557
https://leetcode.com/problems/01-matrix/discuss/2677674/BFS-solution-in-python
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: visited=set() queue=deque() for i in range(len(mat)): for j in range(len(mat[0])): if mat[i][j]==0: visited.add((i,j)) queue.append((i,j)) ...
01-matrix
BFS solution in python
shashank_2000
0
74
01 matrix
542
0.442
Medium
9,558
https://leetcode.com/problems/01-matrix/discuss/2627439/python3-oror-easy-oror-bfs-solution
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: if not mat: return rowSize=len(mat) colSize=len(mat[0]) visited=[[0]*colSize for i in range(rowSize)] distanceArray=[[None]*colSize for i in range(len(mat)...
01-matrix
python3 || easy || bfs solution
_soninirav
0
30
01 matrix
542
0.442
Medium
9,559
https://leetcode.com/problems/01-matrix/discuss/2607637/Python-Simple-solution-easy-to-understand-with-explanation-faster-than-89
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: M = len(mat) N = len(mat[0]) def update(i,j,d): if not visited[i][j]: dist[i][j] = d q.append((i,j)) visited[i][j] = 1 def e...
01-matrix
[Python] Simple solution, easy to understand, with explanation [faster than 89%]
alexion1
0
34
01 matrix
542
0.442
Medium
9,560
https://leetcode.com/problems/01-matrix/discuss/2475891/Most-Optimal-Solution-with-Comments!
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: rows, cols = len(mat), len(mat[0]) # Note: it doesn't matter which direction you start, # you just have to understand that in the first go, # the 1's ARE NOT the distance, so don't take the mi...
01-matrix
Most Optimal Solution with Comments!
EdwinJagger
0
105
01 matrix
542
0.442
Medium
9,561
https://leetcode.com/problems/01-matrix/discuss/2354503/Python-runtime-77.32-memory-90.18
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: min_steps = {} for row in range(len(mat)): for col in range(len(mat[0])): if mat[row][col] != 0: mat[row][col] = self.updateCell(mat, min_steps, {}, row, col) ...
01-matrix
Python, runtime 77.32%, memory 90.18%
tsai00150
0
182
01 matrix
542
0.442
Medium
9,562
https://leetcode.com/problems/01-matrix/discuss/2354503/Python-runtime-77.32-memory-90.18
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: # start from top-left for row in range(len(mat)): for col in range(len(mat[0])): if mat[row][col] != 0: left = top = float('inf') if row-1 >= 0: ...
01-matrix
Python, runtime 77.32%, memory 90.18%
tsai00150
0
182
01 matrix
542
0.442
Medium
9,563
https://leetcode.com/problems/01-matrix/discuss/2248155/Python-DP.-O(mn)O(1)
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m, n = len(mat), len(mat[0]) for r in range(m): for c in range(n): if mat[r][c] == 1: mat[r][c] = math.inf if r > 0: mat[r][c] = min(ma...
01-matrix
Python, DP. O(mn)/O(1)
blue_sky5
0
28
01 matrix
542
0.442
Medium
9,564
https://leetcode.com/problems/01-matrix/discuss/2246707/Python-BFS-level-order-traversal.-Time%3A-O(m*n)-Space%3A-O(m*n)
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m, n = len(mat), len(mat[0]) q = deque() for r in range(m): for c in range(n): if mat[r][c] == 0: q.append((r, c)) else: mat[r]...
01-matrix
Python, BFS level-order traversal. Time: O(m*n), Space: O(m*n)
blue_sky5
0
49
01 matrix
542
0.442
Medium
9,565
https://leetcode.com/problems/01-matrix/discuss/2012854/Python-easy-to-read-and-understand-or-BFS
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m, n = len(mat), len(mat[0]) visit = set() q = [] for i in range(m): for j in range(n): if mat[i][j] == 0: visit.add((i, j)) q.append((...
01-matrix
Python easy to read and understand | BFS
sanial2001
0
196
01 matrix
542
0.442
Medium
9,566
https://leetcode.com/problems/01-matrix/discuss/1863061/Python3-or-Using-DP-or-Two-Pass
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m,n = len(mat), len(mat[0]) dp = [[float("inf") for i in range(n)] for j in range(m)] for i in range(m): for j in range(n): if mat[i][j] == 0: dp[i][j] = 0 ...
01-matrix
Python3 | Using DP | Two Pass
goyaljatin9856
0
71
01 matrix
542
0.442
Medium
9,567
https://leetcode.com/problems/01-matrix/discuss/1373278/DP-with-thinking-process
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: m, n = len(mat), len(mat[0]) max_v = max(m, n) ans = [[0 if c == 0 else max_v for c in mat[i]] for i in range(m)] for i in range(1, n): ans[0][i] = min(ans[0][i], ans[0][i-1]+1 if ans[0][i-1]...
01-matrix
DP with thinking process
ytb_algorithm
0
65
01 matrix
542
0.442
Medium
9,568
https://leetcode.com/problems/01-matrix/discuss/1370668/Python3-DP-solution-O(1)-space
class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: mx = len(mat) * len(mat[0]) for i in range(len(mat)): for j in range(len(mat[i])): if mat[i][j] == 0: continue top = mx ...
01-matrix
[Python3] DP solution O(1) space
maosipov11
0
80
01 matrix
542
0.442
Medium
9,569
https://leetcode.com/problems/01-matrix/discuss/1159451/Python-3-Getting-wrong-answer
class Solution: def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]: h, w = len(matrix), len(matrix[0]) output = [ [ float('inf') for _ in range(w) ] for _ in range (h) ] for x in range(h): for y in range(w): if ma...
01-matrix
[Python 3] Getting wrong answer
jaipoo
0
78
01 matrix
542
0.442
Medium
9,570
https://leetcode.com/problems/01-matrix/discuss/391652/Solution-in-Python-3-(Alternative-Approach)
class Solution: def updateMatrix(self, A: List[List[int]]) -> List[List[int]]: M, N, Q = len(A), len(A[0]), [] B = [[0]*N for _ in range(M)] for i,j in itertools.product(range(M),range(N)): if not A[i][j]: continue b = 1 for k,l in [[i-1,j],[i,j+1],[i+1,j],[i,j-1]]: if not (k...
01-matrix
Solution in Python 3 (Alternative Approach)
junaidmansuri
-2
258
01 matrix
542
0.442
Medium
9,571
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1515564/Python-Easy-to-understand-solution-w-Explanation
class Solution: def __init__(self): self.diameter = 0 # stores the maximum diameter calculated def depth(self, node: Optional[TreeNode]) -> int: """ This function needs to do the following: 1. Calculate the maximum depth of the left and right sides of the given node ...
diameter-of-binary-tree
Python Easy-to-understand solution w Explanation
zayne-siew
56
3,100
diameter of binary tree
543
0.561
Easy
9,572
https://leetcode.com/problems/diameter-of-binary-tree/discuss/480877/543.-Diameter-of-Binary-Tree-and-124.-Binary-Tree-Maximum-Path-Sum
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: def height(root): nonlocal diameter if not root: return 0 left = height(root.left) right = height(root.right) diameter = max(diameter, left + right)...
diameter-of-binary-tree
543. Diameter of Binary Tree and 124. Binary Tree Maximum Path Sum
shchshhappy
51
8,300
diameter of binary tree
543
0.561
Easy
9,573
https://leetcode.com/problems/diameter-of-binary-tree/discuss/480877/543.-Diameter-of-Binary-Tree-and-124.-Binary-Tree-Maximum-Path-Sum
class Solution: def maxPathSum(self, root: TreeNode) -> int: def maxPath(root): nonlocal maxSum if not root: return 0 left = maxPath(root.left) right = maxPath(root.right) maxSum = max(maxSum, left + right + root.val) ...
diameter-of-binary-tree
543. Diameter of Binary Tree and 124. Binary Tree Maximum Path Sum
shchshhappy
51
8,300
diameter of binary tree
543
0.561
Easy
9,574
https://leetcode.com/problems/diameter-of-binary-tree/discuss/574083/Python3-post-order-dfs
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: def fn(node): """Return length+1 and diameter rooted at node""" if not node: return (0, 0) l1, d1 = fn(node.left) l2, d2 = fn(node.right) return 1 + max(l1, l2), max(d1...
diameter-of-binary-tree
[Python3] post-order dfs
ye15
14
1,500
diameter of binary tree
543
0.561
Easy
9,575
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2678310/python-or-recursion
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: self.max_diameter = 0 self.getDiameter(root) return self.max_diameter def getDiameter(self, root): if not root: return 0 left_depth = self.getDiameter(root.left) ...
diameter-of-binary-tree
python | recursion
MichelleZou
11
1,400
diameter of binary tree
543
0.561
Easy
9,576
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2253278/Python-DFS-%2B-2-Solutions-Explained-Time-O(N)-or-Space-O(N)
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: result = [0] # Global result variable self.findDiameter(root, result) return result[0] def findDiameter(self, root, result): if not root: return 0 left = self.findDi...
diameter-of-binary-tree
[Python] DFS + 2 Solutions Explained - Time O(N) | Space O(N)
Symbolistic
9
652
diameter of binary tree
543
0.561
Easy
9,577
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2253278/Python-DFS-%2B-2-Solutions-Explained-Time-O(N)-or-Space-O(N)
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: return self.findDiameter(root)[1] def findDiameter(self, root): if not root: return (0, 0) left = self.findDiameter(root.left) right = self.findDiameter(root.right) ...
diameter-of-binary-tree
[Python] DFS + 2 Solutions Explained - Time O(N) | Space O(N)
Symbolistic
9
652
diameter of binary tree
543
0.561
Easy
9,578
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1351854/Python-Iterative-solution-40-ms-faster-than-90.06-15.2-MB-less-than-99.95
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: ans = 0 hm = {} stack = [(root, False)] while stack: node, visited = stack.pop() if node: if visited: lh = 0 if node.left is None else hm.pop(no...
diameter-of-binary-tree
Python, Iterative solution, 40 ms, faster than 90.06%, 15.2 MB, less than 99.95%
MihailP
8
1,100
diameter of binary tree
543
0.561
Easy
9,579
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1667695/Python-Simple-recursive-DFS-explained
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: self.diameter = 0 def dfs(root): if not root: return 0 leftLongestPath = dfs(root.left) rightLongestPath = dfs(root.right) # keep track of the lo...
diameter-of-binary-tree
[Python] Simple recursive DFS explained
buccatini
5
363
diameter of binary tree
543
0.561
Easy
9,580
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2222455/Recursive-dfs-solution-in-Python-with-detailed-comments-and-explanation
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: # Initialize a variable to keep track of the max diameter as we recursively traverse through each node in the tree max_diameter = 0 # Define a depth-first search function to count the number of nodes along a giv...
diameter-of-binary-tree
Recursive dfs solution, in Python, with detailed comments & explanation
zachtheyek
4
228
diameter of binary tree
543
0.561
Easy
9,581
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1542791/Python3-solution
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: def f(node: TreeNode) -> int: # return longest depth under node L = 1 + f(node.left) if node.left else 0 R = 1 + f(node.right) if node.right else 0 self.ans = max(self.ans, L+R) r...
diameter-of-binary-tree
Python3 solution
dalechoi
1
157
diameter of binary tree
543
0.561
Easy
9,582
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2549163/Python-3-solution-pass-by-reference
class Solution: def height(self, node, dia) -> int: if node == None: return 0 #print(node) lh = self.height(node.left, self.dia) rh = self.height(node.right, self.dia) #print("hi", self.dia, lh, rh) self.dia = max(self.dia, lh+rh) #print("hi2", sel...
diameter-of-binary-tree
Python 3 solution - pass by reference
sumedha19129
0
45
diameter of binary tree
543
0.561
Easy
9,583
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2452208/Bottom-up-O(n)-solution-by-Python-without-global-variable
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: _, diameter = self.helper(root) return diameter def helper(self, root: Optional[TreeNode]) -> (int, int): if root == None: return 0, 0 leftHeight, leftDiameter = self.helper...
diameter-of-binary-tree
Bottom-up O(n) solution by Python without global variable
amikai
0
123
diameter of binary tree
543
0.561
Easy
9,584
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2168815/Python-Solution-or-This-should-be-a-medium-tag-question-IMO
class Solution: diameterLength = 0 def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: self.findDiameter(root) return self.diameterLength-1 # -1 because for N nodes there are N-1 edges def findDiameter(self, root): if root is None: return 0 ...
diameter-of-binary-tree
Python Solution | This should be a medium tag question IMO
sudonitin
0
83
diameter of binary tree
543
0.561
Easy
9,585
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2144294/Simple-clean-recursive-solution-in-Python
class Solution: maxDiameter = 0 def __init(self): self.maxDiamter = maxDiameter def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: def maxDepth(root): if (not root): return 0; leftMax = maxDepth(root.left) rightMax = maxDepth(roo...
diameter-of-binary-tree
Simple clean recursive solution in Python
leqinancy
0
32
diameter of binary tree
543
0.561
Easy
9,586
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2073277/Python-Solution-MaxDepth-or-Height
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: diameter = [0] def dfs(root): if not root: return 0 left = dfs(root.left) right = dfs(root.right) diameter[0] = max(diam...
diameter-of-binary-tree
Python Solution, MaxDepth or Height
Osvaldo11
0
110
diameter of binary tree
543
0.561
Easy
9,587
https://leetcode.com/problems/diameter-of-binary-tree/discuss/2073162/python3-solution
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: def depth(root): if root: left, right = depth(root.left), depth(root.right) return 1+ max(left, right) return 0 if root: return max(depth(root.left) + ...
diameter-of-binary-tree
python3 solution
chloee_icebear
0
35
diameter of binary tree
543
0.561
Easy
9,588
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1997262/2-Solutions-or-O(N2)-or-O(N)-or-Simple
class Solution: def height(self,node): if node is None: return 0 else: return max(self.height(node.left), self.height(node.right)) + 1 def diameter(self,node): if node is None: return 0 dia1 = self.diameter(node.left) dia2 = self.diame...
diameter-of-binary-tree
2 Solutions | O(N^2) | O(N) | Simple
divyamohan123
0
118
diameter of binary tree
543
0.561
Easy
9,589
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1997262/2-Solutions-or-O(N2)-or-O(N)-or-Simple
class Solution: def diameter(self,node): if node is None: return 0,0 dia1,height1 = self.diameter(node.left) dia2,height2 = self.diameter(node.right) height = max(height1 , height2) + 1 dia3 = height1 + height2 + 1 return max(dia1, dia2, dia3), height ...
diameter-of-binary-tree
2 Solutions | O(N^2) | O(N) | Simple
divyamohan123
0
118
diameter of binary tree
543
0.561
Easy
9,590
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1972321/Python-Recursive-O(N)-Solution-Faster-Than-97.42-No-Memory
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: ans = [0] def dfs(tree): if not tree: return 0 left, right = dfs(tree.left), dfs(tree.right) ans[0] = max(ans[0], left + ri...
diameter-of-binary-tree
Python Recursive O(N) Solution, Faster Than 97.42%, No Memory
Hejita
0
115
diameter of binary tree
543
0.561
Easy
9,591
https://leetcode.com/problems/diameter-of-binary-tree/discuss/1758225/Python3-oror-O(n)-solution-oror-40ms
class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: if root is None: return 0 res = [0] def height(root): if not root: return -1 lh = height(root.left) rh = h...
diameter-of-binary-tree
Python3 || O(n) solution || 40ms
Dewang_Patil
0
90
diameter of binary tree
543
0.561
Easy
9,592
https://leetcode.com/problems/diameter-of-binary-tree/discuss/986475/Beats-99.92-or-Dynamic-Programming-(General-Syntax)-or-Python-or-Very-easy-to-understand
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: if not root: return 0 global res res = -1 def solve(root): global res #BC if not root: return 0 #Hypothesis ...
diameter-of-binary-tree
Beats 99.92% | Dynamic Programming (General Syntax) | Python | Very easy to understand
vinit-dash
0
134
diameter of binary tree
543
0.561
Easy
9,593
https://leetcode.com/problems/diameter-of-binary-tree/discuss/437084/Python3-Divide-Conquer
class Solution: def diameterOfBinaryTree(self, root: TreeNode) -> int: if not root: return 0 return self.dfs(root)[1] def dfs(self, root): if not root: return 0, -sys.maxsize l_depth, l_diameter = self.dfs(root.left) r_depth, r_diameter = self...
diameter-of-binary-tree
[Python3] Divide Conquer
Hornettao
0
138
diameter of binary tree
543
0.561
Easy
9,594
https://leetcode.com/problems/remove-boxes/discuss/1379392/Python3-dp
class Solution: def removeBoxes(self, boxes: List[int]) -> int: @cache def fn(lo, hi, k): """Return max score of removing boxes from lo to hi with k to the left.""" if lo == hi: return 0 while lo+1 < hi and boxes[lo] == boxes[lo+1]: lo, k = lo+1, k+1 ...
remove-boxes
[Python3] dp
ye15
2
293
remove boxes
546
0.479
Hard
9,595
https://leetcode.com/problems/remove-boxes/discuss/2787368/Python-Recursive-DP%3A-92-time-64-space
class Solution: def removeBoxes(self, boxes: List[int]) -> int: @cache def dp(l, r, count = 0): if l > r: return 0 // Initial count for the letter at boxes[l] count += 1 ptr = l + 1 while ptr <= r and boxes[l] == boxes[ptr]: ...
remove-boxes
Python Recursive DP: 92% time, 64% space
hqz3
0
17
remove boxes
546
0.479
Hard
9,596
https://leetcode.com/problems/number-of-provinces/discuss/727759/Python3-solution-with-detailed-explanation
class Solution(object): def findCircleNum(self, M): """ :type M: List[List[int]] :rtype: int """ n = len(M) #1 visited = [False]*n #2 count = 0 #3 if not M: #4 return 0 #5 def dfs(u): #6 for v in ...
number-of-provinces
Python3 solution with detailed explanation
peyman_np
17
1,600
number of provinces
547
0.634
Medium
9,597
https://leetcode.com/problems/number-of-provinces/discuss/479621/Intuitive-Disjoint-Set-Union-Find-in-JavaPython3
class Solution: def findCircleNum(self, M: List[List[int]]) -> int: dsu = DSU(len(M)) for i, r in enumerate(M): for j, v in enumerate(r): if j > i - 1: break if v == 1: dsu.union(i, j) return dsu.numFrdCir class DSU: de...
number-of-provinces
Intuitive Disjoint Set Union Find in [Java/Python3]
BryanBoCao
8
1,500
number of provinces
547
0.634
Medium
9,598
https://leetcode.com/problems/number-of-provinces/discuss/2670773/Python-3-Simple-DFS
class Solution: def findCircleNum(self, isConnected: List[List[int]]) -> int: n = len(isConnected) visited = [False]*n def trev(u): if not visited[u]: visited[u] = True for i in range(n): if isConnected[i][u]: #treveses all the ...
number-of-provinces
[Python 3] Simple DFS
user2667O
2
376
number of provinces
547
0.634
Medium
9,599