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/add-to-array-form-of-integer/discuss/1083523/Pythonic-one-line-decision
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: return [int(n) for n in list(str(int("".join([str(x) for x in A])) + K))]
add-to-array-form-of-integer
Pythonic one line decision
cruim
0
74
add to array form of integer
989
0.455
Easy
16,100
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/1037609/2-simple-methods-with-explanation-(Python3)
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: st = "" for i in A: st += str(i) return(list(str(int(st) + K)))
add-to-array-form-of-integer
2 simple methods with explanation (Python3)
vatsalbhuva11
0
85
add to array form of integer
989
0.455
Easy
16,101
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/1037609/2-simple-methods-with-explanation-(Python3)
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: return (list(str(int(''.join([str(x) for x in A]))+K)))
add-to-array-form-of-integer
2 simple methods with explanation (Python3)
vatsalbhuva11
0
85
add to array form of integer
989
0.455
Easy
16,102
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/751001/One-Line-Easy-Python-Solution
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: return list(map(int,list(str(int(''.join(list(map(str, A))))+K))))
add-to-array-form-of-integer
One Line Easy Python Solution
CrazyCoder007
0
124
add to array form of integer
989
0.455
Easy
16,103
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/670381/4-Line-Intuitive-Python-solution
class Solution(object): def addToArrayForm(self, A, K): num = 0 for i in range(len(A)): num = (num*10 + A[i]) return [int(i) for i in str(num + K)]
add-to-array-form-of-integer
4-Line Intuitive Python solution
la_vie_de_chat
0
116
add to array form of integer
989
0.455
Easy
16,104
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/376762/Python-100
class Solution(object): def addToArrayForm(self, A, K): """ :type A: List[int] :type K: int :rtype: List[int] """ A.reverse() i = 0 while K != 0: if i == len(A): A.append(0) A[i] = A[i] + K % 10 ...
add-to-array-form-of-integer
Python 100%
DenysCoder
0
342
add to array form of integer
989
0.455
Easy
16,105
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/305538/Python-3
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: a=int(''.join(str(i) for i in A)) res=a+K ans=[] for i in str(res): ans.append(int(i)) return ans
add-to-array-form-of-integer
Python 3
JasperZhou
0
131
add to array form of integer
989
0.455
Easy
16,106
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/289314/python-sol
class Solution: def addToArrayForm(self, A: List[int], K: int) -> List[int]: A_str="" for i in A: A_str=A_str+str(i) Ans = int(A_str)+K Ans_list=[] for j in str(Ans): Ans_list.append(int(j)) return Ans_list
add-to-array-form-of-integer
python sol
skywalker0803r
0
119
add to array form of integer
989
0.455
Easy
16,107
https://leetcode.com/problems/add-to-array-form-of-integer/discuss/246068/Python-Easy-Solution
class Solution(object): def addToArrayForm(self, A, K): """ :type A: List[int] :type K: int :rtype: List[int] """ A = int(''.join(map(str, A))) B = A + K C = list(map(int, str(B))) return C
add-to-array-form-of-integer
Python Easy Solution
ParthS007
0
162
add to array form of integer
989
0.455
Easy
16,108
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2624938/python3-oror-13-lines-sets-oror-1-TM%3A-8967
class Solution: # Here's the plan: # 1) We make an undirected graph in which the nodes are integers # (as lower-case letters) and each edge connects integers # that are equal. # 2) We use a union-find process to determine ...
satisfiability-of-equality-equations
python3 || 13 lines, sets || 1 T/M: 89%/67%
warrenruud
26
1,800
satisfiability of equality equations
990
0.508
Medium
16,109
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2626592/Python-Union-Find-Solution-Two-Passes-or-Beats-99-Time-or-Time%3A-O(N)-Space%3A-O(N)
class Solution: def equationsPossible(self, equations: List[str]) -> bool: """ Time: O(N), Space: O(N) """ parent = {} def union(c1, c2): pc1 = find(c1) pc2 = find(c2) if pc1 == pc2: return parent[pc2] =...
satisfiability-of-equality-equations
Python Union Find Solution - Two Passes | Beats 99% Time | Time: O(N), Space: O(N)
dos_77
1
67
satisfiability of equality equations
990
0.508
Medium
16,110
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2624849/Python-or-Union-Find-or-With-Explanation-or-Easy-to-Understand
class Solution: def equationsPossible(self, equations: List[str]) -> bool: # We use union-find (or called disjoint-set) in this problem # the basic idea of union-find is to connect the components that share a same root # e.g. a == b, b == c, c == d, then a, b, c, d should be put together ...
satisfiability-of-equality-equations
Python | Union Find | With Explanation | Easy to Understand
Mikey98
1
93
satisfiability of equality equations
990
0.508
Medium
16,111
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2628242/Python-Easy-AF-Solution-or-Union-Find
class Solution: def equationsPossible(self, equations: List[str]) -> bool: roots, get = [i for i in range(26)], lambda x: ord(x) - ord('a') def find(x): return x if roots[x] == x else find(roots[x]) for a,op,_,b in equations: if op == '=': ...
satisfiability-of-equality-equations
✅ Python Easy AF Solution | Union Find
dhananjay79
0
33
satisfiability of equality equations
990
0.508
Medium
16,112
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2628061/Lazy-solution-with-Floyd-Warshall-in-Python
class Solution: def parse(self, eq): return ord(eq[0]) - ord('a'), eq[1:3], ord(eq[3]) - ord('a') def equationsPossible(self, equations: List[str]) -> bool: graph = [[0] * 26 for _ in range(26)] for eq in equations: x, r, y = self.parse(eq) if r == '==': ...
satisfiability-of-equality-equations
Lazy solution with Floyd-Warshall in Python
metaphysicalist
0
5
satisfiability of equality equations
990
0.508
Medium
16,113
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2627766/Python-Simple-Python-Solution-Using-Graph-and-DFS
class Solution: def equationsPossible(self, equations: List[str]) -> bool: graph = {} notequal = [] for equation in equations: a = equation[0] b = equation[3] option = equation[1:3] if option == '==': if a not in graph: graph[a] = set(b) else: graph[a].add(b) if b not in ...
satisfiability-of-equality-equations
[ Python ] ✅✅ Simple Python Solution Using Graph and DFS 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
63
satisfiability of equality equations
990
0.508
Medium
16,114
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2625803/python3-oror-lowest-Memory-usage-oror-Runtime-%3A-38
class Solution: def equationsPossible(self, equations: List[str]) -> bool: word_dict = {chr(i+97):i for i in range(26)} dif = [] for i in equations: xy = sorted([i[0],i[3]]) if i[1] == '!': dif.append(xy) else: num = word_di...
satisfiability-of-equality-equations
python3 || lowest Memory usage || Runtime : 38%
seogudwns
0
17
satisfiability of equality equations
990
0.508
Medium
16,115
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2625776/python3-efficient-solution-using-graph-concept
class Solution: def equationsPossible(self, equations: List[str]) -> bool: dic=defaultdict(list) for i in equations: if i[1]=='=' and i[2]=='=': dic[i[0]].append(i[3]) dic[i[3]].append(i[0]) for j in equations: if j[1]=='!': ...
satisfiability-of-equality-equations
python3 efficient solution using graph concept
benon
0
23
satisfiability of equality equations
990
0.508
Medium
16,116
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2625700/O(nlogn)-using-Union-FInd-for-building-equal-equations-and-checking-differences
class Solution: def equationsPossible(self, equations: List[str]) -> bool: def find(root, u): root[u] = root.get(u, u) if u != root[u]: root[u] = find(root, root[u]) return root[u] diff = {} root = {} for ei in equations: ...
satisfiability-of-equality-equations
O(nlogn) using Union-FInd for building equal equations and checking differences
dntai
0
4
satisfiability of equality equations
990
0.508
Medium
16,117
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2625034/python3-DFS-sol-for-reference
class Solution: def equationsPossible(self, equations: List[str]) -> bool: g = defaultdict(set) def dfs(s, target): ret = False visiting[s] = 1 for nei in g[s]: if visiting[nei] == 0: if nei == target: ...
satisfiability-of-equality-equations
[python3] DFS sol for reference
vadhri_venkat
0
7
satisfiability of equality equations
990
0.508
Medium
16,118
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2505046/Python-union-find
class Solution: def equationsPossible(self, equations: List[str]) -> bool: def find(n): n = ord(n) - ord('a') while root[n] != n: n = root[n] return n def union(n1, n2): r1, r2 = find(n1), find(...
satisfiability-of-equality-equations
Python, union-find
blue_sky5
0
32
satisfiability of equality equations
990
0.508
Medium
16,119
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/2420527/Satisfiability-of-Equality-Equations-oror-Python3-oror-Union-Find
class Solution: def equationsPossible(self, equations: List[str]) -> bool: parent = {} rank = {} for eq in equations: if(eq[1:3] == '=='): self.set_param(eq[0], parent, rank) self.set_param(eq[-1], parent, rank) self.union(...
satisfiability-of-equality-equations
Satisfiability of Equality Equations || Python3 || Union-Find
vanshika_2507
0
6
satisfiability of equality equations
990
0.508
Medium
16,120
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/1986591/PYTHON-SOL-oror-UNION-FIND-AND-PATH-COMPRESSION-oror-VERY-EASY-SOL-oror-EXPLAINED-WELL-oror
class Solution: def findParent(self,x): if self.parent[x] != x: self.parent[x] = self.findParent(self.parent[x]) return self.parent[x] def union(self,a,b): p1 = self.findParent(a) p2 = self.findParent(b) if p1 != p2: if self.rank[p1] > self.ra...
satisfiability-of-equality-equations
PYTHON SOL || UNION FIND AND PATH COMPRESSION || VERY EASY SOL || EXPLAINED WELL ||
reaper_27
0
66
satisfiability of equality equations
990
0.508
Medium
16,121
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/1584430/Python3
class Solution: EQUAL = '==' NOT_EQUAL = '!=' def equationsPossible(self, equations: List[str]) -> bool: groups = {} for equation in equations: left = equation[0] op = equation[1] + equation[2] right = equation[3] if left == ...
satisfiability-of-equality-equations
Python3
shiv-sj
0
61
satisfiability of equality equations
990
0.508
Medium
16,122
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/1473134/Python3-or-Union-Find
class Solution: def equationsPossible(self, equations: List[str]) -> bool: self.disjoint() for i in equations: if i[1]+i[2]=="==": self.union(i[0],i[3]) for i in equations: if i[1]+i[2]=="!=" and not self.verify(i[0],i[1]+i[2],i[3]): re...
satisfiability-of-equality-equations
[Python3] | Union-Find
swapnilsingh421
0
48
satisfiability of equality equations
990
0.508
Medium
16,123
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/1465349/Python-solution-using-Union-Find
class Solution: def equationsPossible(self, equations: List[str]) -> bool: equalities = [s.split("==") for s in equations if s[1:3] == "=="] inequalities =[s.split("!=") for s in equations if s[1:3] == "!="] f = {} def find(x): f.setdefault(x, x) if x != f[x]:...
satisfiability-of-equality-equations
Python solution using Union Find
Vikram006
0
46
satisfiability of equality equations
990
0.508
Medium
16,124
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/961205/Python-top-percentile-elegant
class Solution: def equationsPossible(self, equations: List[str]) -> bool: equalities = filter(lambda _: "==" in _, equations) inequalities = filter(lambda _: "!=" in _, equations) sets = [] # Sorting equalities guarantees deterministic clustering for x, _, __, y in sorted(equalit...
satisfiability-of-equality-equations
Python top-percentile, elegant
tyril
0
72
satisfiability of equality equations
990
0.508
Medium
16,125
https://leetcode.com/problems/satisfiability-of-equality-equations/discuss/852605/Python-Easy-Disjoint-Union-Set
class Solution: def __init__(self): self.id_ = [i for i in range(26)] def find(self,x): while x != self.id_[x]: self.id_[x] = self.id_[self.id_[x]] x = self.id_[x] return x def union(self,x,y): x = self.find(x) y = self.find(y) ...
satisfiability-of-equality-equations
[Python} Easy Disjoint Union Set
realslimshady
0
89
satisfiability of equality equations
990
0.508
Medium
16,126
https://leetcode.com/problems/broken-calculator/discuss/1033822/Easy-and-Clear-Solution-Python-3
class Solution: def brokenCalc(self, x: int, y: int) -> int: if y<=x: return x-y else: res=0 while x<y: if y%2==1: y+=1 else: y=y//2 res+=1 res+=(x-y) r...
broken-calculator
Easy & Clear Solution Python 3
moazmar
2
182
broken calculator
991
0.541
Medium
16,127
https://leetcode.com/problems/broken-calculator/discuss/2416694/Python-95.67-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Greedy
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: res = 0 # taking a counter. while target > startValue: # checking if target value is greater then startValue. res += 1 # as if target is greater implies we`ll be having atleast one operation. if tar...
broken-calculator
Python 95.67% faster | Simplest solution with explanation | Beg to Adv | Greedy
rlakshay14
0
54
broken calculator
991
0.541
Medium
16,128
https://leetcode.com/problems/broken-calculator/discuss/1988675/PYTHON-SOL-oror-LOG-SOL-oror-GO-BACKWARDS-oror-EXPLAINED-oror
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: operations = 0 cpy = target while True: if target <= startValue: break if target %2 == 1: target += 1 operations += 1 ...
broken-calculator
PYTHON SOL || LOG SOL || GO BACKWARDS || EXPLAINED ||
reaper_27
0
51
broken calculator
991
0.541
Medium
16,129
https://leetcode.com/problems/broken-calculator/discuss/1897757/Python-3-or-Easy-solution-or-No-tricks-and-no-shortcuts-used
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: tempTarget = target tempStartValue = startValue ct1_p1 = 0 while tempTarget > startValue: if tempTarget%2 == 0: tempTarget = tempTarget//2 else: ...
broken-calculator
Python 3 | Easy solution | No tricks and no shortcuts used
krunalk013
0
72
broken calculator
991
0.541
Medium
16,130
https://leetcode.com/problems/broken-calculator/discuss/1878573/Python-Easy-Solution-with-Explanation
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: step = 0 while target != startValue: if target < startValue: step = step + (startValue - target) return step else: if target%2 == 0: ...
broken-calculator
Python Easy Solution with Explanation
EnergyBoy
0
9
broken calculator
991
0.541
Medium
16,131
https://leetcode.com/problems/broken-calculator/discuss/1877899/Python-greedy-solution-or-With-Explanation-or-Complexity-Analysis
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: if startValue == target: return 0 # can only subtract if startValue > target: return startValue-target # greedy, better to do division than plus ops = 0 while target > s...
broken-calculator
Python greedy solution | With Explanation | Complexity Analysis
diqing-SHU
0
9
broken calculator
991
0.541
Medium
16,132
https://leetcode.com/problems/broken-calculator/discuss/1876589/Python-Iterative-Solution
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: steps = 0 if startValue == target: return steps while target > startValue: steps += 1 if target % 2 == 1: target += 1 else: target //= ...
broken-calculator
Python Iterative Solution
pradeep288
0
25
broken calculator
991
0.541
Medium
16,133
https://leetcode.com/problems/broken-calculator/discuss/1876091/Python3Java-Simple-Solution-oror-Greedy
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: ans = 0 while startValue < target: if target % 2 == 0: ans += 1 target //= 2 else: ans += 2 target = (target+1)//2 return ans + s...
broken-calculator
[Python3/Java] Simple Solution || Greedy
abhijeetmallick29
0
28
broken calculator
991
0.541
Medium
16,134
https://leetcode.com/problems/broken-calculator/discuss/1875847/Python3-easy-to-understand-O(1)-space-and-O(n)-time
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: ops = 0 while target!=startValue: if target>startValue and target%2==0: ops+=1 target//=2 elif target>startValue: diff = target ...
broken-calculator
Python3 easy to understand O(1) space and O(n) time
shubham3
0
28
broken calculator
991
0.541
Medium
16,135
https://leetcode.com/problems/broken-calculator/discuss/1875636/Python-BFS-Solution
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: if startValue >= target: return startValue - target queue = [(target, 0)] visited = {} while queue: val, count = queue.pop(0) if val + 1 == startValue or (val // 2 == startV...
broken-calculator
Python BFS Solution
atiq1589
0
42
broken calculator
991
0.541
Medium
16,136
https://leetcode.com/problems/broken-calculator/discuss/1875393/Python3-oror-TC%3A-O(log(target))-oror-SC%3AO(1)oror-Greedy
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: #traverse backwards, so we have two options, addition and division steps = 0 while target > startValue: if target &amp; 1: target += 1 else: target = target // ...
broken-calculator
Python3 || TC: O(log(target)) || SC:O(1)|| Greedy
s_m_d_29
0
29
broken calculator
991
0.541
Medium
16,137
https://leetcode.com/problems/broken-calculator/discuss/1875213/Faster-Than-Faster-Python-Easy-Solution-Using-Greedy-Approach
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: result = 0 while True: if target <= startValue: result = result + startValue - target break if target % 2 == 0: target = target // 2 result = result + 1 else: target = target + 1 result = result +...
broken-calculator
Faster Than Faster ✔✌🔥🔥 [Python] Easy Solution Using Greedy Approach
ASHOK_KUMAR_MEGHVANSHI
0
44
broken calculator
991
0.541
Medium
16,138
https://leetcode.com/problems/broken-calculator/discuss/1875191/Double-until-Exceed-and-then-Subtract
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: if startValue >= target: return startValue - target count=0 while startValue < target: count += 1 startValue *= 2 ans=count need_substract=startValue-target...
broken-calculator
Double until Exceed and then Subtract
k3232908
0
13
broken calculator
991
0.541
Medium
16,139
https://leetcode.com/problems/broken-calculator/discuss/1874840/python-ez-greedy-solution
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: step = 0 while target > startValue: if target % 2 == 0: target /= 2 else: target += 1 step += 1 step += startValue - target return int(step)
broken-calculator
python ez greedy solution
yingziqing123
0
50
broken calculator
991
0.541
Medium
16,140
https://leetcode.com/problems/broken-calculator/discuss/1874814/Easy-Commented-Solution-in-Python
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: #instead of going from startValue to target we will go from target to startValue #in this case our operation would change #instead of multiplying we will be divinding and if the number is odd then we will first add then divide #incase o...
broken-calculator
Easy Commented Solution in Python
Jazzyb1999
0
42
broken calculator
991
0.541
Medium
16,141
https://leetcode.com/problems/broken-calculator/discuss/1874768/python-3-oror-simple-iterative-solution-oror-O(logn)O(1)
class Solution: def brokenCalc(self, startValue: int, target: int) -> int: res = 0 while target > startValue: res += 1 if target % 2: target += 1 else: target //= 2 return res + startValue - target
broken-calculator
python 3 || simple iterative solution || O(logn)/O(1)
dereky4
0
64
broken calculator
991
0.541
Medium
16,142
https://leetcode.com/problems/broken-calculator/discuss/1077397/Simple-Python-Sol-(faster-than-99)
class Solution: def brokenCalc(self, X: int, Y: int) -> int: if X >= Y: return X - Y if Y % 2 == 0: return 1 + self.brokenCalc(X, Y // 2) if Y % 2 == 1: return 2 + self.brokenCalc(X, (Y + 1) // 2)
broken-calculator
Simple Python Sol (faster than 99%)
JulesCui
0
111
broken calculator
991
0.541
Medium
16,143
https://leetcode.com/problems/broken-calculator/discuss/985294/Python3-greedy-O(logN)
class Solution: def brokenCalc(self, X: int, Y: int) -> int: ans = 0 while X < Y: if Y&amp;1: Y += 1 else: Y //= 2 ans += 1 return ans + X - Y
broken-calculator
[Python3] greedy O(logN)
ye15
0
77
broken calculator
991
0.541
Medium
16,144
https://leetcode.com/problems/broken-calculator/discuss/1076095/Python-or-Fast-and-Easy-or-Beats-97
class Solution: def brokenCalc(self, X: int, Y: int) -> int: c = 0 while Y>X: if Y%2: Y += 1 else: Y //= 2 c += 1 return c + (X - Y)
broken-calculator
Python | Fast & Easy | Beats 97%
SlavaHerasymov
-1
88
broken calculator
991
0.541
Medium
16,145
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/1215277/Python-Sliding-Window-or-Set-%2B-HashMap
class Solution: def subarraysWithKDistinct(self, nums: List[int], k: int) -> int: def window(nums, k): left = 0 right = 0 res = 0 in_set = set() hash_map = collections.Counter() while right < len(nums): in_set...
subarrays-with-k-different-integers
[Python] Sliding Window | Set + HashMap
Sai-Adarsh
5
813
subarrays with k different integers
992
0.545
Hard
16,146
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/1988735/PYTHON-SOL-oror-FASTER-THAN-99.59-oror-SIMPLE-oror-EXPLAINED-oror-SLIDING-WINDOW-oror-LINEAR-TIME-oror
class Solution: def subarraysWithKDistinct(self, nums: List[int], k: int) -> int: ans = 0 n = len(nums) latest = {} unique = 0 prev = 0 for i in range(n): if nums[i] not in latest: unique += 1 latest[nums[i]] = i if ...
subarrays-with-k-different-integers
PYTHON SOL || FASTER THAN 99.59% || SIMPLE || EXPLAINED || SLIDING WINDOW || LINEAR TIME ||
reaper_27
3
273
subarrays with k different integers
992
0.545
Hard
16,147
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/1312341/Python3-sliding-window-%2B-queue
class Solution: def subarraysWithKDistinct(self, nums: List[int], k: int) -> int: ans = ii = 0 freq = defaultdict(int) queue = deque() for i, x in enumerate(nums): freq[x] += 1 queue.append(i) if len(freq) > k: ii = queue[0]+1 ...
subarrays-with-k-different-integers
[Python3] sliding window + queue
ye15
3
419
subarrays with k different integers
992
0.545
Hard
16,148
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/853831/Simple-Python-Solution
class Solution: def helper(self,A,B): count = 0 left = 0 right = 0 d = {} while right<len(A): if A[right] not in d: d[A[right]] = 0 d[A[right]] += 1 while len(d)>B: d[A[left]] -= 1 ...
subarrays-with-k-different-integers
Simple Python Solution
Ayu-99
2
438
subarrays with k different integers
992
0.545
Hard
16,149
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/1993417/Sliding-window-with-little-help-(weird-but-effective
class Solution: def subarraysWithKDistinct(self, nums: List[int], k: int) -> int: ans = 0 n = len(nums) next = [-1] * n t_record = dict() for i in range(n - 1, -1, -1): if nums[i] in t_record: next[i] = t_record[nums[i]] t_record[nums[i...
subarrays-with-k-different-integers
Sliding window with little help (weird but effective
luyi4me
0
146
subarrays with k different integers
992
0.545
Hard
16,150
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/1659423/Sliding-Window-and-HashMap
class Solution: def subarraysWithKDistinct(self, l: List[int], k: int) -> int: def aps (l,k): if k==0: return 0 d={} for i in l: d[i]=0 j=diff=ans=0 for i in range(len(l)): d[l[i]]+=1 ...
subarrays-with-k-different-integers
Sliding Window and HashMap
gamitejpratapsingh998
0
381
subarrays with k different integers
992
0.545
Hard
16,151
https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/1502477/Python3-Sliding-widow-solution
class Solution: def atMostK(self, nums, k): good_count = 0 counter = collections.Counter() begin = 0 for end in range(len(nums)): if counter[nums[end]] == 0: k -= 1 counter[nums[end]] += 1 while k < 0: ...
subarrays-with-k-different-integers
[Python3] Sliding widow solution
maosipov11
0
283
subarrays with k different integers
992
0.545
Hard
16,152
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1527334/Python-BFS%3A-Easy-to-understand-solution-w-Explanation
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: # Check if root node is x or y if root.val == x or root.val == y: return False # Prepare for BFS, initialise variables curr, flag = [root.left, root.right], False while curr: ...
cousins-in-binary-tree
Python BFS: Easy-to-understand solution w Explanation
zayne-siew
8
423
cousins in binary tree
993
0.542
Easy
16,153
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1165675/Python-BFS-Level-Order-Traversal-oror-Iterative
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: q = [root] while(q): toggle = 0 parents = [] for i in range(len(q)): # Level wise exploration starts here temp = q[0] # storing the first element in a temp variable ...
cousins-in-binary-tree
Python BFS - Level Order Traversal || Iterative
avinash0007
2
229
cousins in binary tree
993
0.542
Easy
16,154
https://leetcode.com/problems/cousins-in-binary-tree/discuss/2000463/Python-Recursive-DFS-Clean-and-Simple!
class Solution: def isCousins(self, root, x, y): self.x, self.y = x, y self.foundLevel, self.foundParent = None, None return self.dfs(root, 0, None) def dfs(self, node, level, parent): if node.val == self.x or node.val == self.y: if self.foundLevel and se...
cousins-in-binary-tree
Python - Recursive DFS - Clean and Simple!
domthedeveloper
1
121
cousins in binary tree
993
0.542
Easy
16,155
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618715/Python3-tree-traversal-(DFS-and-BFS)
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: def fn(node, k, parent): """Traverse the subtree rooted at node""" if node is None: return if node.val in (x, y): ans[node.val] = (k, parent) fn(node.left, k+1, node) ...
cousins-in-binary-tree
[Python3] tree traversal (DFS & BFS)
ye15
1
48
cousins in binary tree
993
0.542
Easy
16,156
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618715/Python3-tree-traversal-(DFS-and-BFS)
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: ans = dict() stack = [(root, 0, None)] while stack: node, k, parent = stack.pop() if not node: continue if node.val in (x, y): ans[node.val] = (k, parent) stack.appen...
cousins-in-binary-tree
[Python3] tree traversal (DFS & BFS)
ye15
1
48
cousins in binary tree
993
0.542
Easy
16,157
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618715/Python3-tree-traversal-(DFS-and-BFS)
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: queue = [(root, None)] while queue: newq = [] seen = 0 for n, p in queue: if n.val in (x, y): if not seen: seen = p el...
cousins-in-binary-tree
[Python3] tree traversal (DFS & BFS)
ye15
1
48
cousins in binary tree
993
0.542
Easy
16,158
https://leetcode.com/problems/cousins-in-binary-tree/discuss/2663515/Python3-solution-or-DFS-with-recursion
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: self.d1 = self.parent1 = self.d2 = self.parent2 = -1 def DFS(r, depth, parent): if r: if r.val == x: self.d1, self.parent1 = depth, parent ...
cousins-in-binary-tree
Python3 solution | DFS with recursion
mediocre-coder
0
12
cousins in binary tree
993
0.542
Easy
16,159
https://leetcode.com/problems/cousins-in-binary-tree/discuss/2407545/Python3-or-BFS-and-hash-map-or-minimal-space-usage
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: h = defaultdict(list) q = deque([(root,0,-1)]) while q: node, level, parent = q.popleft() if node: if node.val == x or node.val == y: ...
cousins-in-binary-tree
Python3 | BFS and hash map | minimal space usage
Ploypaphat
0
32
cousins in binary tree
993
0.542
Easy
16,160
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1771061/Python3-BFS-solution
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: from collections import deque q=deque() q.append((root,None,0)) mydict={} while q: node,parent,level=q.popleft() mydict[node....
cousins-in-binary-tree
Python3 BFS solution
Karna61814
0
40
cousins in binary tree
993
0.542
Easy
16,161
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1691228/Python3%3A-BFS-and-DFS-approach
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: f = [True] def finddepthofelement(root, element): if root == None: return 10e5 else: if root.left and root.right: if (root.left.val == x ...
cousins-in-binary-tree
Python3: BFS and DFS approach
devansh_raj
0
100
cousins in binary tree
993
0.542
Easy
16,162
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1601452/Python-3-recursion
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: self.x_depth = self.y_depth = math.inf self.x_parent = self.y_parent = None def helper(root, depth, parent): if root is None or depth > self.x_depth or depth > self.y_depth: re...
cousins-in-binary-tree
Python 3 recursion
dereky4
0
143
cousins in binary tree
993
0.542
Easy
16,163
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1533814/Simple-python3-solution-using-BFS
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: mydict = {} q = collections.deque() q.append(root) level = 0 while q: items = len(q) for i in range(items): que = q.popleft() if que....
cousins-in-binary-tree
Simple python3 solution using BFS
sonali1597
0
63
cousins in binary tree
993
0.542
Easy
16,164
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1517368/Python3-bfs-solution
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: q = [root] while q: z = [] n = len(q) for j in range(n): i = q.pop(0) if i.left and i.right and ((i.left.val == x and i.right.val == y) or (i.lef...
cousins-in-binary-tree
Python3 bfs solution
EklavyaJoshi
0
27
cousins in binary tree
993
0.542
Easy
16,165
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1444411/Python-using-level-order-traversal
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: q=[root] depth=0 c1,c2=0,0 while q: new=[] depth+=1 for i in q: if i.left and i.right: if i.left.val==x and i.right.val==y or i.left.va...
cousins-in-binary-tree
Python - using level order traversal
prajwalahluwalia
0
28
cousins in binary tree
993
0.542
Easy
16,166
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1398745/Python3-Sets-with-BFS
class Solution: def isCousins(self, root: Optional[TreeNode], x: int, y: int) -> bool: # Easiest way is breadth first search def BFS(node, xy): if node is None: return False queue = [node] values = [node.val] ...
cousins-in-binary-tree
[Python3] Sets with BFS
whitehatbuds
0
113
cousins in binary tree
993
0.542
Easy
16,167
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1342676/Python-Simple-DFS-%2B-HashMap
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: depthParent = {} # key: node value. value: (depth, parent node value) def getDepth(node, depth, parent=None): if node: getDepth(node.left, depth + 1, node) getDepth(nod...
cousins-in-binary-tree
[Python] Simple DFS + HashMap
genefever
0
72
cousins in binary tree
993
0.542
Easy
16,168
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1255775/Python-traditional-recursive-solution
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: self.xDepth = float('inf') self.yDepth = float('inf') self.xParent = float('inf') self.yParent = float('inf') if not root: return False def helper(root, height,parent): ...
cousins-in-binary-tree
Python traditional recursive solution
dee7
0
115
cousins in binary tree
993
0.542
Easy
16,169
https://leetcode.com/problems/cousins-in-binary-tree/discuss/1068712/Python-Recursive
class Solution: def __init__(self): self.track = collections.defaultdict(tuple) def dfs(self,root,level,parent): if root == None: return None self.track[root.val] = (level,parent) self.dfs(root.left,level+1,root.val) self.dfs(root.right,level+1,root.val) d...
cousins-in-binary-tree
Python Recursive
Akarsh_B
0
47
cousins in binary tree
993
0.542
Easy
16,170
https://leetcode.com/problems/cousins-in-binary-tree/discuss/762750/Straightforward-Python-Solution-using-Stack-and-namedtuple
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: lookup = {} stack = [(root, 0, None)] while stack: node, depth, parent = stack.pop() if node.val in (x, y): lookup[node.val] = (depth, parent) if node.left: ...
cousins-in-binary-tree
Straightforward Python Solution using Stack and namedtuple
parkershamblin
0
68
cousins in binary tree
993
0.542
Easy
16,171
https://leetcode.com/problems/cousins-in-binary-tree/discuss/625660/Intuitive-approach-by-adding-depth-and-parent-information-to-each-node
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: # 0) Build depth,parent for each node root.depth = 0 root.parent = -1 queue = [root] n2n_dict = {} while queue: n = queue.pop(0) n2n_dict[n.val] = n ...
cousins-in-binary-tree
Intuitive approach by adding depth and parent information to each node
puremonkey2001
0
37
cousins in binary tree
993
0.542
Easy
16,172
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618475/Python-Recursive-and-Iterative-approaches
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: def dfs(node, parent, depth): # don't need to traverse till the end if both x, y are traversed. if not node or len(node_dict) == 2: return if node.val == x o...
cousins-in-binary-tree
[Python] Recursive and Iterative approaches
var42
0
117
cousins in binary tree
993
0.542
Easy
16,173
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618475/Python-Recursive-and-Iterative-approaches
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: node_data = collections.defaultdict(list) # tuple: (node, parent, depth) queue = collections.deque([(root, None, 0)]) while queue: if len(node_data) > 2: break node, p...
cousins-in-binary-tree
[Python] Recursive and Iterative approaches
var42
0
117
cousins in binary tree
993
0.542
Easy
16,174
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618273/PYTHON-3-Level-Order-Traversal-or-BFS-or-Dictionary
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: dic = {} def levelOrder(node , level , parent): if not node: return 0 dic[node.val] = [level , parent] levelOrder(node.left , level + 1 , node.val) levelOrder(...
cousins-in-binary-tree
[PYTHON 3] Level Order Traversal | BFS | Dictionary
mohamedimranps
0
37
cousins in binary tree
993
0.542
Easy
16,175
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618131/Python3-DFS-Simplest-beating-88
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: def dfs(parent,curr, level, x): if not curr: return [] if curr.val == x: return [level, parent] return dfs(curr, curr.left, level+1, x) + dfs(curr, cu...
cousins-in-binary-tree
[Python3] DFS Simplest, beating 88%
purushottam3
0
25
cousins in binary tree
993
0.542
Easy
16,176
https://leetcode.com/problems/cousins-in-binary-tree/discuss/618053/python-97.22
class Solution: def isCousins(self, root: TreeNode, x: int, y: int) -> bool: def fest(b,a,c,d): if b: if b.val==a: return [c,d] c=b.val return fest(b.left,a,c,d+1) or fest(b.right,a,c,d+1) return fest(root,x,False,0)[0]!...
cousins-in-binary-tree
python 97.22%
kisdown
0
93
cousins in binary tree
993
0.542
Easy
16,177
https://leetcode.com/problems/cousins-in-binary-tree/discuss/297164/Python-faster-than-96-20-ms
class Solution(object): def isCousins(self, root, x, y): """ :type root: TreeNode :type x: int :type y: int :rtype: bool """ def f(root, v, p): if root.val == v: _d = 1 _p = p return _d, _p ...
cousins-in-binary-tree
Python - faster than 96%, 20 ms
il_buono
0
398
cousins in binary tree
993
0.542
Easy
16,178
https://leetcode.com/problems/rotting-oranges/discuss/1546489/Python-BFS%3A-Easy-to-understand-with-Explanation
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: visit, curr = set(), deque() # find all fresh and rotten oranges for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: visit.add((i, j)) el...
rotting-oranges
Python BFS: Easy-to-understand with Explanation
zayne-siew
59
3,900
rotting oranges
994
0.525
Medium
16,179
https://leetcode.com/problems/rotting-oranges/discuss/280964/python-44-ms-faster-than-99.87
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: rotten = [] # build initial array of rotten oranges rows = len(grid) columns = len(grid[0]) for i in range(rows): for j in range(columns): if grid[i][j] == 2: ...
rotting-oranges
python 44 ms, faster than 99.87%
talistern21
13
2,300
rotting oranges
994
0.525
Medium
16,180
https://leetcode.com/problems/rotting-oranges/discuss/1786056/Python-3-(70ms)-or-BFS-Solution
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: visit, curr = set(), deque() for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: visit.add((i, j)) elif grid[i][j] == 2: ...
rotting-oranges
Python 3 (70ms) | BFS Solution
MrShobhit
4
332
rotting oranges
994
0.525
Medium
16,181
https://leetcode.com/problems/rotting-oranges/discuss/1461265/Well-Explained-oror-Easy-approach-oror-94faster
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: m,n,l = len(grid),len(grid[0]),0 rot = [] for i in range(m): for j in range(n): if grid[i][j]==2: rot.append([i,j,l]) def convert(i,j,l): nonlocal rot if i<0 or i...
rotting-oranges
📌📌 Well-Explained || Easy-approach || 94%faster 🐍
abhi9Rai
4
328
rotting oranges
994
0.525
Medium
16,182
https://leetcode.com/problems/rotting-oranges/discuss/424012/Python3-BFS-solution-with-comments-faster-than-99
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: row, col = len(grid), len(grid[0]) visited = set() queue, new_queue = [], [] dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)] # first find all initial rotten oranges for i in range(row)...
rotting-oranges
Python3 BFS solution with comments faster than 99%
yanshengjia
3
870
rotting oranges
994
0.525
Medium
16,183
https://leetcode.com/problems/rotting-oranges/discuss/2721269/Python-oror-BFS-oror-Beginners-friendly-oror-Easy
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: q,ones = [],0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j]==2: q.append([[i,j],0]) if grid[i][j]==1: ones+=1 ...
rotting-oranges
Python || BFS || Beginners friendly || Easy
its_iterator
2
124
rotting oranges
994
0.525
Medium
16,184
https://leetcode.com/problems/rotting-oranges/discuss/1634092/Python-BFS-Solution
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # add rotten oranges to a queue and keep track of fresh oranges queue = [] num_fresh = 0 minute = 0 # initialize the queue with rotten oranges for m in range(len(grid)): for n...
rotting-oranges
Python BFS Solution
dahal_
2
152
rotting oranges
994
0.525
Medium
16,185
https://leetcode.com/problems/rotting-oranges/discuss/1546575/Python-or-90-time-and-space-or-2-ways
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: minutes = 0 m, n = len(grid), len(grid[0]) visited = set() while True: changed = False rotten = [] for i in range(m): for j in range(n): ...
rotting-oranges
Python | 90 % time and space | 2 ways
mshanker
2
59
rotting oranges
994
0.525
Medium
16,186
https://leetcode.com/problems/rotting-oranges/discuss/1546575/Python-or-90-time-and-space-or-2-ways
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: minutes = 0 m, n = len(grid), len(grid[0]) rotten = [] fresh = [] for i in range(m): for j in range(n): if grid[i][j] == 2: ...
rotting-oranges
Python | 90 % time and space | 2 ways
mshanker
2
59
rotting oranges
994
0.525
Medium
16,187
https://leetcode.com/problems/rotting-oranges/discuss/1437571/Python-Clean-and-concise-BFS-solution
class Solution(object): def orangesRotting(self, grid): """ :type grid: List[List[int]] :rtype: int """ rows = len(grid) if rows == 0: return -1 cols = len(grid[0]) # Track fresh oranges fresh = 0 # Track bad oranges lvl by lvl...
rotting-oranges
[Python] Clean and concise BFS solution
jsanchez78
2
196
rotting oranges
994
0.525
Medium
16,188
https://leetcode.com/problems/rotting-oranges/discuss/2643520/Python-BFS
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: fresh,time=0,0 q=deque() R,C=len(grid),len(grid[0]) for r in range(R): for c in range(C): if grid[r][c]==1: fresh+=1 if grid[r][c]==2: q.append([r,c]) directions=[[0,1],[0,-1],[1,0],[-1,0]] while q and fresh>0: ...
rotting-oranges
[Python] BFS
gauravtiwari91
1
35
rotting oranges
994
0.525
Medium
16,189
https://leetcode.com/problems/rotting-oranges/discuss/2560361/Python-Solution-with-comments-or-Using-BFS-or-faster-than-96-or-O(n.m)
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: q = deque() time, fresh = 0, 0 R = len(grid) C = len(grid[0]) for r in range(R): for c in range(C): if grid[r][c] == 1: fresh += 1 #get count of all fresh oranges if grid[r][c] == 2 : q.append([r, c]) #Add the rott...
rotting-oranges
Python Solution with comments | Using BFS | faster than 96% | O(n.m)
nikhitamore
1
53
rotting oranges
994
0.525
Medium
16,190
https://leetcode.com/problems/rotting-oranges/discuss/2474675/Python-Accurate-Solution-oror-Documented
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: q = collections.deque() count = 0 # count the fresh oranges # add all rotten-orange cell co-ordinates into queue for x in range(len(grid)): for j in range(len(gr...
rotting-oranges
[Python] Accurate Solution || Documented
Buntynara
1
58
rotting oranges
994
0.525
Medium
16,191
https://leetcode.com/problems/rotting-oranges/discuss/2152846/python-3-oror-simple-bfs
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) fresh = 0 q = collections.deque() for i, row in enumerate(grid): for j, orange in enumerate(row): if orange == 1: fresh += 1...
rotting-oranges
python 3 || simple bfs
dereky4
1
173
rotting oranges
994
0.525
Medium
16,192
https://leetcode.com/problems/rotting-oranges/discuss/1986644/Python3-Runtime%3A-46ms-97.35-Memory%3A-13.8mb-92.44
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: if len(grid) == 0: return -1 rottenOranges = list() freshOranges = set() for r in range(len(grid)): for c in range(len(grid[r])): if grid[r][c] == 1: ...
rotting-oranges
Python3 Runtime: 46ms 97.35% Memory: 13.8mb 92.44%
arshergon
1
57
rotting oranges
994
0.525
Medium
16,193
https://leetcode.com/problems/rotting-oranges/discuss/1930388/PYTHON-DFS
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: column_length = len(grid[0]) row_length = len(grid) initial_rotten = [] for col in range(column_length): for row in range(row_length): if grid[row][col] == 2...
rotting-oranges
[PYTHON] DFS
authier
1
242
rotting oranges
994
0.525
Medium
16,194
https://leetcode.com/problems/rotting-oranges/discuss/1337575/easy-python
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: fresh=[] rotten=[] newrotten=[] row=len(grid) col=len(grid[0]) for i in range(row): for j in range(col): if grid[i][j]==1: fresh.append((i,j)) ...
rotting-oranges
easy python
pavanto3d
1
43
rotting oranges
994
0.525
Medium
16,195
https://leetcode.com/problems/rotting-oranges/discuss/749751/Easy-Python-BFS-Solution-beats-99-with-Description-and-Comments!
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) q = collections.deque([]) oranges = 0 # Populate our deque with the rotting orange locations and get orange count. for row in range(rows): ...
rotting-oranges
Easy Python BFS Solution beats 99% with Description and Comments!
Pythagoras_the_3rd
1
153
rotting oranges
994
0.525
Medium
16,196
https://leetcode.com/problems/rotting-oranges/discuss/433979/Python3-BFS-faster-than-90
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: numMinutes = 0 twosPositions = [] zerosPositions = [] rows = len(grid) cols = len(grid[0]) for row in range(rows): for col in range(cols): if grid[row...
rotting-oranges
Python3 --BFS-- faster than 90%
sishar4
1
222
rotting oranges
994
0.525
Medium
16,197
https://leetcode.com/problems/rotting-oranges/discuss/382204/Solution-in-Python-3-(beats-~99)-(nine-lines)
class Solution: def orangesRotting(self, G: List[List[int]]) -> int: M, N, S, E, c = len(G), len(G[0]), [], sum(G,[]).count(1), 0 R = [(i,j) for i,j in itertools.product(range(M),range(N)) if G[i][j] == 2] while E != 0: for [i,j] in R: for a,b in (i-1,j),(i,j+1),(i+1,j),(i,j-1): ...
rotting-oranges
Solution in Python 3 (beats ~99%) (nine lines)
junaidmansuri
1
647
rotting oranges
994
0.525
Medium
16,198
https://leetcode.com/problems/rotting-oranges/discuss/2839563/Easiest-Python-Solution-oror-Fully-Explained-with-COMMENTS
class Solution: def orangesRotting(self, grid: List[List[int]]) -> int: # number of rows rows = len(grid) if rows == 0: # check if grid is empty return -1 # number of columns cols = len(grid[0]) # keep track of fresh oranges fre...
rotting-oranges
✔Easiest Python Solution || 💯Fully Explained with COMMENTS🆗💭
AdityaTrivedi88
0
1
rotting oranges
994
0.525
Medium
16,199