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/decompress-run-length-encoded-list/discuss/2713811/recursive-solution-or-Python-3
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: freq = 0 val = 1 newArr = [] if len(nums) != 0: newArr += nums[freq] * [nums[val]] else: return newArr return newArr + self.decompressRLElist(nums[val+1:])
decompress-run-length-encoded-list
recursive solution | Python 3
mr6nie
0
2
decompress run length encoded list
1,313
0.859
Easy
19,600
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2705907/Python-best-Solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res =[] for i in range(0,len(nums),2): res+=([nums[i+1]]*nums[i]) return res
decompress-run-length-encoded-list
Python best Solution
kartik_5051
0
3
decompress run length encoded list
1,313
0.859
Easy
19,601
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2669590/one-line-Python
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: return list(itertools.chain.from_iterable([nums[2 * i + 1]] * nums[i * 2] for i in range(len(nums) // 2)))
decompress-run-length-encoded-list
one line Python
MaryLuz
0
2
decompress run length encoded list
1,313
0.859
Easy
19,602
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2668124/Explained-efficient-Python-solution-utilizing-Generators
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: """ Straightforward basic solution Try to optimize via moving to generators """ # i = 0 # res = [] # while i < len(nums): # freq = nums[i] # val = nums[i+1] # while freq > 0: # res.append(val) # freq -= 1 # i += 2 # return res """ Generator based solution. All for the sake of avoiding append. But I`m not sure, how reasonable it is, since append is O(1). """ def _get_val_sequence(freq, val): while freq: freq -= 1 yield val """ I will try one more approach, to get rid of counter variable. """ # def _generate_sequences(nums): # i = 0 # while i < len(nums): # for val in _get_val_sequence(nums[i], nums[i+1]): # yield val # i += 2 """ Not that big of a difference in pperforamnce. """ def _generate_sequences(nums): for i in range(0, len(nums), 2): for val in _get_val_sequence(nums[i], nums[i+1]): yield val return list(_generate_sequences(nums))
decompress-run-length-encoded-list
Explained, efficient Python solution utilizing Generators
NGUgeneral
0
1
decompress run length encoded list
1,313
0.859
Easy
19,603
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2664570/Simple-python-code-with-explanation
class Solution: def decompressRLElist(self, nums): #create a empty list(freq) to store frequency which is at even indexes freq = [] #create a empty list(val) to store value which is at odd indexes val = [] #iterate over the elements in list(nums) for i in range(len(nums)): #if the index is even if i%2 == 0 : #then store the value at that index in list(freq) freq.append(nums[i]) #if the index is odd else: #then store that value at that index in list(val) val.append(nums[i]) #create a list(res) to store result res = [] #iterate over the elements in list(freq) for i in range(len(freq)): #for each index in freq #iterate (value at ith index of freq) times for j in range(freq[i]): #that many times append (the value at same index in val) to res(list) res.append(val[i]) #return the res(list) return res
decompress-run-length-encoded-list
Simple python code with explanation
thomanani
0
13
decompress run length encoded list
1,313
0.859
Easy
19,604
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2611099/72-ms-faster-than-89.56-of-Python3
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res = [] for i in range(len(nums)//2): res+=[nums[2*i+1] for j in range(nums[2*i])] return res
decompress-run-length-encoded-list
72 ms, faster than 89.56% of Python3
Abdulahad_Abduqahhorov
0
20
decompress run length encoded list
1,313
0.859
Easy
19,605
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2530222/Python-Solution-easy-and-fast
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res = [] l = len(nums) for i in range(0,l,2): for j in range(nums[i]): res.append(nums[i+1]) return res
decompress-run-length-encoded-list
Python Solution easy and fast
savvy_phukan
0
49
decompress run length encoded list
1,313
0.859
Easy
19,606
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2508908/Python-or-2-easy-to-read-solutions
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res = [] i = 0 while(i < len(nums)): for _ in range(nums[i]): res.append(nums[i+1]) i += 2 return res
decompress-run-length-encoded-list
Python | 2 easy to read solutions
Wartem
0
50
decompress run length encoded list
1,313
0.859
Easy
19,607
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2508908/Python-or-2-easy-to-read-solutions
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: freq = nums[::2] val = nums[1::2] res = [] for i in range(len(freq)): for _ in range(freq[i]): res.append(val[i]) return res
decompress-run-length-encoded-list
Python | 2 easy to read solutions
Wartem
0
50
decompress run length encoded list
1,313
0.859
Easy
19,608
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2439954/1313.-Decompress-Run-Length-Encoded-List%3A-1-Liner
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: return [nums[index] for index,element in enumerate(nums) if index%2!=0 for x in range(nums[index-1])]
decompress-run-length-encoded-list
1313. Decompress Run-Length Encoded List: 1 Liner
rogerfvieira
0
12
decompress run length encoded list
1,313
0.859
Easy
19,609
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2421878/easiest-python-solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res=[] for i in range(1,len(nums),2): for j in range(nums[i-1]): res.append(nums[i]) return res
decompress-run-length-encoded-list
easiest python solution
Abdulrahman_Ahmed
0
34
decompress run length encoded list
1,313
0.859
Easy
19,610
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2382225/Python3-solution-O(N2)-time-complexity-O(N)-space-complexity-90-faster
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: answer = [] for i in range(0, len(nums), 2): for j in range(0, nums[i]): answer.append(nums[i + 1]) return answer
decompress-run-length-encoded-list
Python3 solution, O(N^2) time complexity, O(N) space complexity, 90% faster
matteogianferrari
0
33
decompress run length encoded list
1,313
0.859
Easy
19,611
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2249142/Easy-to-Understand-Python-3-solution....-faster-than-98
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: lst=[] for i in range(0,len(nums),2): lst.extend(nums[i]*[nums[i+1]]) return lst
decompress-run-length-encoded-list
Easy to Understand Python 3 solution.... faster than 98%
guneet100
0
67
decompress run length encoded list
1,313
0.859
Easy
19,612
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2050646/1-loop-solution-(Python3)
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: array = [] for i in range(1, len(nums), 2): array += nums[i-1] * [nums[i]] return array ```
decompress-run-length-encoded-list
1 loop solution (Python3)
Encelad
0
47
decompress run length encoded list
1,313
0.859
Easy
19,613
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2035203/Python3-simple-code
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: i = 0; decompressed = [] for _ in nums: if i < len(nums): for _ in range(nums[i]): decompressed.append(nums[i + 1]) i += 2 return decompressed
decompress-run-length-encoded-list
[Python3] simple code
Shiyinq
0
30
decompress run length encoded list
1,313
0.859
Easy
19,614
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1927914/Faster-than-95-Python-Solution-6-Lines
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: arr = [] for i in range(1,len(nums),2): l = nums[i-1:i+1] for j in range(0,l[0]): arr.append(l[1]) return arr
decompress-run-length-encoded-list
Faster than 95% Python Solution 6 Lines
itsmeparag14
0
35
decompress run length encoded list
1,313
0.859
Easy
19,615
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1869681/My-easy-to-understand-Python-submission.
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: i,j=0,1 ans=[] while j<len(nums): temp=[nums[j]]*nums[i] ans+=temp i+=2 j+=2 return ans
decompress-run-length-encoded-list
My easy to understand Python submission.
tkdhimanshusingh
0
63
decompress run length encoded list
1,313
0.859
Easy
19,616
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1866587/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: arr = [] for i in range(1, len(nums), 2): for j in range(nums[i-1]): arr.append(nums[i]) return arr
decompress-run-length-encoded-list
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
26
decompress run length encoded list
1,313
0.859
Easy
19,617
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1863245/Python-solution-faster-than-86
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res = [] for i in range(len(nums)-1): if i % 2 == 0: res.extend([nums[i+1]]*nums[i]) return res
decompress-run-length-encoded-list
Python solution faster than 86%
alishak1999
0
33
decompress run length encoded list
1,313
0.859
Easy
19,618
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1855502/Python-3-solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: n = 2 ans = [] for i in [nums[num:num+n] for num in range(0, len(nums), n)]: freq, val = i[0], i[1] ans += [val] * freq return ans
decompress-run-length-encoded-list
Python 3 solution
zuzannakilar
0
23
decompress run length encoded list
1,313
0.859
Easy
19,619
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1812962/3-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-92
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: ans = [] for i in range(0,len(nums),2): ans += [nums[i+1]]*nums[i] return ans
decompress-run-length-encoded-list
3-Lines Python Solution || 60% Faster || Memory less than 92%
Taha-C
0
46
decompress run length encoded list
1,313
0.859
Easy
19,620
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1730044/Python-dollarolution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: v = [] for i in range(0,len(nums),2): for _ in range(nums[i]): v.append(nums[i+1]) return v
decompress-run-length-encoded-list
Python $olution
AakRay
0
83
decompress run length encoded list
1,313
0.859
Easy
19,621
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1723145/Decompress-Run-Length-Encoded-List-Python-Solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: flst=[] j=0 lst=[] for i in range(1,len(nums),2): lst.append([nums[i]]*nums[j]) j+=2 for x in lst: for y in x: flst.append(y) return flst
decompress-run-length-encoded-list
Decompress Run-Length Encoded List Python Solution
seabreeze
0
16
decompress run length encoded list
1,313
0.859
Easy
19,622
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1691802/2-Methods-or-Python3
class Solution: def decompressRLElist(self, nums): i = 0 res = [] while i<len(nums): res+=([nums[i+1]]*nums[i]) i+=2 return res
decompress-run-length-encoded-list
2 Methods | Python3 🐍
hritik5102
0
88
decompress run length encoded list
1,313
0.859
Easy
19,623
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1691802/2-Methods-or-Python3
class Solution: def decompressRLElist(self, nums): return [res for i in range(0,len(nums),2) for res in [nums[i+1]]*nums[i]] obj = Solution() print(obj.decompressRLElist([1,2,3,4]))
decompress-run-length-encoded-list
2 Methods | Python3 🐍
hritik5102
0
88
decompress run length encoded list
1,313
0.859
Easy
19,624
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1632649/Python-Easy-Solution-Using-While-loop
class Solution(object): def decompressRLElist(self, nums): i=0 l=[] while(i<len(nums)): for j in range(nums[i]): l.append(nums[i+1]) i+=2 return l
decompress-run-length-encoded-list
Python Easy Solution Using While loop
Manasa_Alapaka
0
38
decompress run length encoded list
1,313
0.859
Easy
19,625
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1383067/Barely-2-line-solution-in-Python
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: from itertools import chain return list(chain.from_iterable((val for _ in range(freq)) for freq, val in zip(nums[::2], nums[1::2])))
decompress-run-length-encoded-list
Barely 2-line solution in Python
mousun224
0
84
decompress run length encoded list
1,313
0.859
Easy
19,626
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1281389/Python-Runtime%3A-60-ms-faster-than-92.69-.
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: ans = [] for i in range(0, len(nums), 2): ans.extend([nums[i + 1]] * nums[i]) return ans
decompress-run-length-encoded-list
Python Runtime: 60 ms, faster than 92.69% .
aditi_shree
0
128
decompress run length encoded list
1,313
0.859
Easy
19,627
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1223818/Python-Solution-(76.47)-oror-please-suggest-optimization
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: arr = [] for i in range(0, len(nums), 2): arr+=[nums[i+1]]*nums[i] return arr
decompress-run-length-encoded-list
Python Solution (76.47%) || please suggest optimization
sanjam531
0
66
decompress run length encoded list
1,313
0.859
Easy
19,628
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1221077/80ms-Python3-(with-comments)
class Solution(object): def decompressRLElist(self, nums): """ :type nums: List[int] :rtype: List[int] """ #We create a variable to save the result result = [] #We iterate through the the starting indexes of each pair in any even elements list for i in range(0,len(nums)-1,2): #We add the desired list to our output variable result+=nums[i]*[nums[i+1]] return result
decompress-run-length-encoded-list
80ms, Python3 (with comments)
Akshar-code
0
46
decompress run length encoded list
1,313
0.859
Easy
19,629
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1161738/Python-One-Liner
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: return list(itertools.chain(*[[nums[i + 1]]*nums[i] for i in range(0, len(nums) - 1, 2)]))
decompress-run-length-encoded-list
Python One Liner
BrendMU
0
103
decompress run length encoded list
1,313
0.859
Easy
19,630
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1010107/Python3-solution.-very-straightforward
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: k = 0 arr = [] while k < len(nums): freq = nums[k] val = nums[k+1] for i in range(freq): arr.append(val) k += 2 return arr
decompress-run-length-encoded-list
Python3 solution. very straightforward
izekchen0222
0
74
decompress run length encoded list
1,313
0.859
Easy
19,631
https://leetcode.com/problems/matrix-block-sum/discuss/711729/Python-DP-O(m*n)
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) mat[:] = [[0] * (n + 1)] + [[0] + row for row in mat] res = [[0] * n for i in range(m)] for i in range(1, m + 1): for j in range(1, n + 1): mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1] for i in range(m): for j in range(n): r1, r2 = max(i - K, 0), min(i + K + 1, m) c1, c2 = max(j - K, 0), min(j + K + 1, n) res[i][j] = mat[r2][c2] - mat[r2][c1] - mat[r1][c2] + mat[r1][c1] return res
matrix-block-sum
Python DP O(m*n)
SWeszler
8
1,100
matrix block sum
1,314
0.754
Medium
19,632
https://leetcode.com/problems/matrix-block-sum/discuss/1521997/Python-Two-Approaches%3A-Easy-to-understand-w-Explanation
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) result = [[0 for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): result[i][j] = sum(sum(mat[x][max(0, j-k):min(n, j+k+1)]) for x in range(max(0, i-k), min(m, i+k+1))) return result
matrix-block-sum
Python, Two Approaches: Easy-to-understand w Explanation
zayne-siew
7
478
matrix block sum
1,314
0.754
Medium
19,633
https://leetcode.com/problems/matrix-block-sum/discuss/1521997/Python-Two-Approaches%3A-Easy-to-understand-w-Explanation
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) # Calculate the prefix sum prefix = mat[:][:] # essentially copies the entire array for i in range(m): for j in range(n): prefix[i][j] += (prefix[i-1][j] if i > 0 else 0) + \ # add prefix sum of (i-1, j) if it exists (prefix[i][j-1] if j > 0 else 0) - \ # add prefix sum of (i, j-1) if it exists (prefix[i-1][j-1] if i > 0 and j > 0 else 0) # subtract prefix sum of (i-1, j-1) if it exists # Calculate the block sum from the prefix sum result = [[0 for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): result[i][j] = prefix[min(i+k, m-1)][min(j+k, n-1)] + \ # S(D), bounded by m x n (prefix[i-k-1][j-k-1] if i-k > 0 and j-k > 0 else 0) - \ # S(A), if it exists (prefix[i-k-1][min(j+k, n-1)] if i-k > 0 else 0) - \ # S(B), if it exists (prefix[min(i+k, m-1)][j-k-1] if j-k > 0 else 0) # S(C), if it exists return result # we could technically shorten the block sum calculation into one line of code, but that is super unreadable
matrix-block-sum
Python, Two Approaches: Easy-to-understand w Explanation
zayne-siew
7
478
matrix block sum
1,314
0.754
Medium
19,634
https://leetcode.com/problems/matrix-block-sum/discuss/1799507/Python-easy-to-read-and-understand-or-Range-query-sum-2D
class Solution: def sumRegion(self, matrix, row1, col1, row2, col2): ans = 0 for i in range(row1, row2+1): x1 = matrix[i][col2] x2 = 0 if col1 == 0 else matrix[i][col1-1] ans += x1-x2 return ans def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) t = [[0 for _ in range(n)] for _ in range(m)] for i in range(m): sums = 0 for j in range(n): sums += mat[i][j] t[i][j] = sums for i in range(m): for j in range(n): r1, r2 = max(0, i-k), min(m-1, i+k) c1, c2 = max(0, j-k), min(n-1, j+k) mat[i][j] = self.sumRegion(t, r1, c1, r2, c2) return mat
matrix-block-sum
Python easy to read and understand | Range query sum 2D
sanial2001
1
208
matrix block sum
1,314
0.754
Medium
19,635
https://leetcode.com/problems/matrix-block-sum/discuss/1524791/Python3-Self-explanatory-Prefix-Sum-Based-Solution
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: """ [+][ ][ ][-] [ ][a][b][ ] [ ][c][d][ ] dp[d] = dp[b] + dp[c] - dp[a] + mat[d] [-][ ][ ][+] lower_right upper_right lower_left upper_left block_sum[(i,j), k] = dp[i+k, j+k] - dp[i-k-1, j+k] - dp[i+k, j-k-1] - dp[i-k-1, j-k-1] """ # dp[i][j] is the prefix sum of all elemnt before i, j dp = [[0 for _ in range(len(mat[0]))] for _ in range(len(mat))] # init dp for i in range(len(dp)): for j in range(len(dp[0])): # init dp left &amp; upper border if i == 0 and j == 0: dp[i][j] = mat[i][j] continue elif i == 0: dp[i][j] = mat[i][j] + dp[i][j-1] continue elif j == 0: dp[i][j] = mat[i][j] + dp[i-1][j] continue dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + mat[i][j] # for m in mat: # print(m) # print("--------------") # for i in dp: # print(i) res = [[0 for _ in range(len(mat[0]))] for _ in range(len(mat))] # calculate block sum row_max = len(res) - 1 col_max = len(res[0]) - 1 for i in range(len(res)): for j in range(len(res[0])): lower_right = dp[min(i+k, row_max)][min(j+k, col_max)] upper_left = 0 if (i-k-1<0 or j-k-1<0) else dp[i-k-1][j-k-1] lower_left = 0 if (j-k-1<0) else dp[min(i+k, row_max)][j-k-1] upper_right = 0 if (i-k-1<0) else dp[i-k-1][min(j+k, col_max)] res[i][j] = lower_right - upper_right - lower_left + upper_left return res
matrix-block-sum
[Python3] Self-explanatory Prefix-Sum Based Solution
nick19981122
1
263
matrix block sum
1,314
0.754
Medium
19,636
https://leetcode.com/problems/matrix-block-sum/discuss/477111/Python3-prefix-sum
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) prefix = [[0]*(n+1) for _ in range(m+1)] for i in range(m): for j in range(n): prefix[i+1][j+1] = mat[i][j] + prefix[i][j+1] + prefix[i+1][j] - prefix[i][j] ans = [[0]*n for _ in range(m)] for i in range(m): for j in range(n): r0, r1 = max(0, i-k), min(m-1, i+k) c0, c1 = max(0, j-k), min(n-1, j+k) ans[i][j] = prefix[r1+1][c1+1] - prefix[r0][c1+1] - prefix[r1+1][c0] + prefix[r0][c0] return ans
matrix-block-sum
[Python3] prefix sum
ye15
1
183
matrix block sum
1,314
0.754
Medium
19,637
https://leetcode.com/problems/matrix-block-sum/discuss/2642852/Python3-or-Solved-Using-Prefix-Sum
class Solution: #Time-Complexity: O(m^2*n) #Space-Complexity: O(m*n) def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) prefix_sum = [[None for _ in range(n)] for z in range(m)] for a in range(m): prefix_sum[a][0] = mat[a][0] #for each row, initialize the prefix sum! for i in range(0, m, 1): for j in range(1, n, 1): prefix_sum[i][j] = prefix_sum[i][j-1] + mat[i][j] #now, for each entry of mat (r,c), we need to get sum of all elements in (r- k) <= row <= r + k #and columns s.t. c - k <= col <= c + k! #we have to not consider rows that are out of bounds! ans = [[-100 for _ in range(n)] for _ in range(m)] #we will fill up this answer as we go! for r in range(m): for j in range(n): #for the current ans[r][j] entry we are answering for, we can compute the #lowest and highest in-bounds value for row-wise and column-wise! lr, hr = max(0, r - k), min(r + k, m - 1) lc, hc = max(0, j - k), min(j + k, n - 1) total = 0 for row in range(lr, hr + 1): subtraction = None if(lc == 0): subtraction = 0 else: subtraction = prefix_sum[row][lc-1] sub_sum = prefix_sum[row][hc] - subtraction total += sub_sum ans[r][j] = total return ans
matrix-block-sum
Python3 | Solved Using Prefix Sum
JOON1234
0
46
matrix block sum
1,314
0.754
Medium
19,638
https://leetcode.com/problems/matrix-block-sum/discuss/2458482/Python3-Solution-with-using-prefix-sum
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: n, m = len(mat), len(mat[0]) """ // _sum[i][j] is sum of all elements from rectangle [0,0,i,j] """ _sum = [[0] * (m + 1) for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, m + 1): """ mat[i - 1][j - 1] - base value for this rectangle _sum[i - 1][j] - sum of up rectangle _sum[i][j - 1] - sum of left rectangle _sum[i - 1][j - 1] - sum of rectangle that is part of _sum[i - 1][j] and _sum[i][j - 1] => we shoud remove this sum """ _sum[i][j] = mat[i - 1][j - 1] + _sum[i - 1][j] + _sum[i][j - 1] - _sum[i - 1][j - 1] res = [[0] * m for _ in range(n)] for i in range(n): for j in range(m): """We shoud do "+ 1" because _sum matrix has shift by 1""" r_up, r_down = max(0, i - k) + 1, min(n - 1, i + k) + 1 c_left, c_right = max(0, j - k) + 1, min(m - 1, j + k) + 1 res[i][j] = _sum[r_down][c_right] - _sum[r_up - 1][c_right] - _sum[r_down][c_left - 1] + _sum[r_up - 1][c_left - 1] return res
matrix-block-sum
[Python3] Solution with using prefix sum
maosipov11
0
57
matrix block sum
1,314
0.754
Medium
19,639
https://leetcode.com/problems/matrix-block-sum/discuss/2397383/Python3-Sliding-Window
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m, n = len(mat), len(mat[0]) # sum rows ans = [r[:] for r in mat] for i in range(m): ans[i][0] = sum(mat[i][:k+1]) for j in range(1,n): ans[i][j] = ans[i][j-1] ans[i][j] -= mat[i][j-k-1] if j>=k+1 else 0 ans[i][j] += mat[i][j+k] if j<n-k else 0 # sum cols mat = [r[:] for r in ans] for j in range(n): mat[0][j] = sum(ans[l][j] for l in range(k+1)) for i in range(1,m): mat[i][j] = mat[i-1][j] mat[i][j] -= ans[i-k-1][j] if i>=k+1 else 0 mat[i][j] += ans[i+k][j] if i<m-k else 0 return mat
matrix-block-sum
[Python3] Sliding Window
ruosengao
0
42
matrix block sum
1,314
0.754
Medium
19,640
https://leetcode.com/problems/matrix-block-sum/discuss/2353211/Simple-prefix-solution-uses-range-sum-query
class Solution: def matrixBlockSum(self, matrix: List[List[int]], k: int) -> List[List[int]]: rows,cols = len(matrix), len(matrix[0]) self.dp = [[0]*(cols+1) for _ in range(rows+1)] for r in range(rows): # for Prefix sum of 2d array prefix = 0 for c in range(cols): prefix+=matrix[r][c] above = self.dp[r][c+1] self.dp[r+1][c+1] = prefix + above self.ans = [[0]*(cols) for _ in range(rows)] for i in range(rows): for j in range(cols): r1,c1,r2,c2 = i-k,j-k,i+k,j+k if i-k < 0: r1 = 0 if j-k < 0: c1 = 0 if i+k > rows-1: r2 = rows-1 if j+k > cols-1: c2 = cols-1 self.ans[i][j] = self.sumRegion(r1,c1,r2,c2) return self.ans def sumRegion(self, r1: int, c1: int, r2: int, c2: int) -> int: # Gives range sum query of 2d block r1+=1 r2+=1 c1+=1 c2+=1 bottomRight = self.dp[r2][c2] topRight = self.dp[r1-1][c2] bottomLeft = self.dp[r2][c1-1] topLeft = self.dp[r1-1][c1-1] return bottomRight - topRight - bottomLeft + topLeft
matrix-block-sum
Simple prefix solution uses range sum query
sidrAG
0
45
matrix block sum
1,314
0.754
Medium
19,641
https://leetcode.com/problems/matrix-block-sum/discuss/2074059/Python-Prefix-Sum-O(M*N)-Solution
class Solution: def matrixBlockSum(self, nums: List[List[int]], k: int) -> List[List[int]]: # create prefix sum (cummulative sum) where nums[i][j] denotes sum of all elements from [0,0] to [i][j] # for sum from (r1,c1) to (r2,c2), we get ans = nums[r2][c2]-nums[r2][c1-1]-nums[r1-1][c2]+nums[r1-1][c1-1] # TC: O(n*m) where n is no. of rows and m is no. of columns n = len(nums) m = len(nums[0]) for i in range(n): for j in range(m): if i==0 and j==0: continue elif i==0: nums[i][j]+=nums[i][j-1] elif j==0: nums[i][j]+=nums[i-1][j] else: nums[i][j]+=(nums[i-1][j]+nums[i][j-1]-nums[i-1][j-1]) answer = [[0 for i in range(m)] for j in range(n)] for i in range(n): for j in range(m): r,c = i,j r1 = 0 if i-k<0 else i-k c1 = 0 if j-k<0 else j-k r2 = n-1 if i+k>=n else i+k c2 = m-1 if j+k>=m else j+k ans = 0 ans+=nums[r2][c2] if c1>0: ans-=(nums[r2][c1-1]) if r1>0: ans-=(nums[r1-1][c2]) if c1>0 and r1>0: ans+=(nums[r1-1][c1-1]) answer[i][j] = ans return answer
matrix-block-sum
Python Prefix Sum O(M*N) Solution
dsalgobros
0
94
matrix block sum
1,314
0.754
Medium
19,642
https://leetcode.com/problems/matrix-block-sum/discuss/2069089/Python-or-DP
class Solution: def matrixBlockSum(self, ma: List[List[int]], k: int) -> List[List[int]]: n = len(ma) m = len(ma[0]) def isval(x,y): if x<0 or y<0 or x>=n or y>=m: return False return True t = [[0 for i in range(m)]for j in range(n)] for i in range(min(n,k+1)): for j in range(min(k+1,m)): t[0][0] += ma[i][j] for i in range(n): for j in range(m): if i != 0: t[i][j] = t[i-1][j] if isval(i-k-1,j): for v in range(-1*k,k+1): if isval(i-k-1,j+v): t[i][j] -= ma[i-k-1][j+v] if isval(i+k,j): for v in range(-1*k,k+1): if isval(i+k,j+v): t[i][j] += ma[i+k][j+v] else: if j!=0: t[i][j] = t[i][j-1] if isval(i,j-k-1): for v in range(-1*k,k+1): if isval(i+v,j-k-1): t[i][j] -= ma[i+v][j-k-1] if isval(i,j+k): for v in range(-1*k,k+1): if isval(i+v,j+k): t[i][j] += ma[i+v][j+k] return t
matrix-block-sum
Python | DP
Shivamk09
0
58
matrix block sum
1,314
0.754
Medium
19,643
https://leetcode.com/problems/matrix-block-sum/discuss/1934562/solution-using-prefix-sum
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: mat.insert(0,[0]*(len(mat[0]))) for i in mat: i.insert(0,0) compute=[[0 for i in range(len(mat[0]))] for j in range(len(mat))] def pre_compute(): for i in range(1,len(mat)): for j in range(1,len(mat[0])): compute[i][j]=mat[i][j]+compute[i-1][j]+compute[i][j-1]-compute[i-1][j-1] pre_compute() # print(compute) answer=[[0 for i in range(len(mat[0]))] for j in range(len(mat))] for i in range(1,len(mat)): for j in range(1,len(mat[0])): [top,left]=[max(i-k,1),max(j-k,1)] [bottom,right]=[min(i+k,len(mat)-1),min(j+k,len(mat[0])-1)] answer[i][j]=compute[bottom][right]-compute[top-1][right]-compute[bottom][left-1]+compute[top-1][left-1] answer=[i[1:] for i in answer[1:]] return answer
matrix-block-sum
solution using prefix sum
prateek4463
0
55
matrix block sum
1,314
0.754
Medium
19,644
https://leetcode.com/problems/matrix-block-sum/discuss/1652678/python-simple-solution-or-95.37
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m,n = len(mat),len(mat[0]) P = [[0]*(n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): P[i][j] = P[i-1][j] + P[i][j-1] - P[i-1][j-1] + mat[i-1][j-1] def getSumOfRec(i1,j1,i2,j2): if i1<1: i1 = 1 if j1<1: j1 = 1 if i2>m: i2 = m if j2>n: j2 = n return P[i2][j2] - P[i1-1][j2] - P[i2][j1-1] + P[i1-1][j1-1] ans = [[0]*n for _ in range(m)] for i in range(1,m+1): for j in range(1,n+1): ans[i-1][j-1] = getSumOfRec(i-k,j-k,i+k,j+k) return ans
matrix-block-sum
python simple solution | 95.37%
1579901970cg
0
279
matrix block sum
1,314
0.754
Medium
19,645
https://leetcode.com/problems/matrix-block-sum/discuss/1245267/Python-Prefix-Sum-based-solution
class Solution: def matrixBlockSum(self, mat: List[List[int]], k: int) -> List[List[int]]: m = len(mat) n = len(mat[0]) sumM = [[0]*n for _ in range(m)] for i in range(m): cs = 0 for j in range(n): cs += mat[i][j] sumM[i][j] = cs retM = [[0]*n for _ in range(m)] for i in range(m): for j in range(n): lr = max(0,i-k) hr = min(m-1,i+k) ps = 0 for r in range(lr,hr+1): hc = min(n-1,j+k) lc = j - k - 1 if lc < 0: ps += sumM[r][hc] else: ps += sumM[r][hc] - sumM[r][lc] retM[i][j] = ps return retM
matrix-block-sum
Python Prefix Sum based solution
worker-bee
0
175
matrix block sum
1,314
0.754
Medium
19,646
https://leetcode.com/problems/matrix-block-sum/discuss/1123134/Python3-Six-lines-different-than-prefix-sum-88ms-(99)-speed-97-memory
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: def nearby_sum(seq: Sequence) -> Iterable[int]: total = sum(seq[:K]) for i, n in enumerate(seq): total += (seq[i+K] if i+K < len(seq) else 0) - (seq[i-K-1] if i-K-1 >= 0 else 0) yield total return zip(*map(nearby_sum,(zip(*map(nearby_sum, mat)))))
matrix-block-sum
[Python3] Six lines - different than prefix sum - 88ms (99%) speed, 97% memory
travisjungroth
0
199
matrix block sum
1,314
0.754
Medium
19,647
https://leetcode.com/problems/matrix-block-sum/discuss/483502/Python-3-(Cumulative-Sum)-(beats-~96)
class Solution: def matrixBlockSum(self, G: List[List[int]], K: int) -> List[List[int]]: M, N, G[0] = len(G), len(G[0]), list(itertools.accumulate(G[0])); A = [[0]*N for _ in range(M)] for i in range(1,M): s = 0 for j in range(N): s += G[i][j]; G[i][j] = s + G[i-1][j] for i,j in itertools.product(range(M),range(N)): R, C = min(M-1,i+K), min(N-1,j+K) A[i][j] = G[R][C] - (i-K > 0 and G[i-K-1][C]) - (j-K > 0 and G[R][j-K-1]) + (i-K > 0 and j-K > 0 and G[i-K-1][j-K-1]) return A - Junaid Mansuri - Chicago, IL
matrix-block-sum
Python 3 (Cumulative Sum) (beats ~96%)
junaidmansuri
0
476
matrix block sum
1,314
0.754
Medium
19,648
https://leetcode.com/problems/matrix-block-sum/discuss/477222/Python3-brute-force-and-DP
class Solution: def matrixBlockSum(self, mat: List[List[int]], K: int) -> List[List[int]]: m,n,res,dp=len(mat),len(mat[0]),[],[] for row in mat: res.append([0]*n) dp.append([0]*(n+1)) dp.append([0]*(n+1)) for i in range(m): for j in range(n): dp[i+1][j+1] = dp[i+1][j]+dp[i][j+1]-dp[i][j]+mat[i][j] for i in range(m): for j in range(n): a,b,c,d=min(i+K,m-1),min(j+K,n-1),max(0,i-K),max(0,j-K) res[i][j]=dp[a+1][b+1]-dp[a+1][d]-dp[c][b+1]+dp[c][d] return res def matrixBlockSumBruteForce(self, mat: List[List[int]], K: int) -> List[List[int]]: m,n,res=len(mat),len(mat[0]),[] for row in mat: res.append([0]*n) ht = {} for i in range(m): for j in range(n): ht[(i,j)] = mat[i][j] for cor in ht: for i in range(cor[0]-K,cor[0]+K+1): for j in range(cor[1]-K,cor[1]+K+1): if 0<=i<m and 0<=j<n: res[cor[0]][cor[1]]+=mat[i][j] return res
matrix-block-sum
Python3 brute force and DP
jb07
0
185
matrix block sum
1,314
0.754
Medium
19,649
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/514777/Python3-96ms-solution
class Solution: def __init__(self): self.summary = 0 def sumEvenGrandparent(self, root: TreeNode) -> int: self.walk(root, False, False) return self.summary def walk(self, node: TreeNode, parent_even: bool, grand_parent_even: bool): if node is None: return if grand_parent_even: self.summary += node.val next_parent_even = True if node.val % 2 == 0 else False self.walk(node.left, next_parent_even, parent_even) self.walk(node.right, next_parent_even, parent_even) return
sum-of-nodes-with-even-valued-grandparent
Python3 96ms solution
tjucoder
7
740
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,650
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2085065/Python-iterative-stack-easy-to-understand-O(N)-time-and-O(N)-space
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: root.parent = None root.grandparent = None result = 0 stack = [root] while len(stack): node = stack.pop() if node.left: node.left.parent = node node.left.grandparent = node.parent stack.append(node.left) if node.right: node.right.parent = node node.right.grandparent = node.parent stack.append(node.right) if node.grandparent and node.grandparent.val % 2 == 0: result += node.val return result
sum-of-nodes-with-even-valued-grandparent
Python iterative stack easy to understand O(N) time and O(N) space
tushar-rishav
2
22
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,651
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/1763985/Python-Simple-Solution
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: ans = 0 def utility(current: TreeNode, parent=-1, grandparent=-1): nonlocal ans if current is None: return 0 if grandparent % 2 == 0: ans += current.val utility(current.left, current.val, parent) utility(current.right, current.val, parent) if root.left: utility(root.left, root.val) if root.right: utility(root.right, root.val) return ans
sum-of-nodes-with-even-valued-grandparent
Python Simple Solution
anCoderr
2
96
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,652
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2732359/Recursive-or-Time-Complexity-O(N)
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: def helper(grandparent, parent, node): if not node:return if grandparent and grandparent.val%2 == 0:self.ans += node.val helper(parent, node, node.left) helper(parent, node, node.right) self.ans = 0 helper(None, None, root) return self.ans
sum-of-nodes-with-even-valued-grandparent
Recursive | Time Complexity - O(N)
Rahul-Krishna
1
27
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,653
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2329403/Easiest-Solution-To-Understand-Beats-90-Beginner-DFS-Solution
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: evenGrandParent = [] def dfs(node, parent=None, grandParent=None): if node is None: return if grandParent and grandParent % 2 == 0: nonlocal evenGrandParent evenGrandParent.append(node.val) dfs(node.right, node.val, parent) dfs(node.left, node.val, parent) dfs(root) return sum(evenGrandParent)
sum-of-nodes-with-even-valued-grandparent
Easiest Solution To Understand - Beats 90% - Beginner DFS Solution
7yler
1
44
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,654
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/483529/Python-3-(DFS)-(beats-~93)
class Solution: def sumEvenGrandparent(self, R: TreeNode) -> int: S = [0] def dfs(N): if N and not N.val % 2: if N.left: if N.left.left: S[0] += N.left.left.val if N.left.right: S[0] += N.left.right.val if N.right: if N.right.left: S[0] += N.right.left.val if N.right.right: S[0] += N.right.right.val if N: dfs(N.left), dfs(N.right) dfs(R) return S[0] - Junaid Mansuri - Chicago, IL
sum-of-nodes-with-even-valued-grandparent
Python 3 (DFS) (beats ~93%)
junaidmansuri
1
308
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,655
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2715007/95-Accepted-or-Using-Deque-or-Easy-to-Understand-or-Python
class Solution(object): def sumEvenGrandparent(self, root): q = deque() q.append(root) ans = 0 while q: for _ in range(len(q)): p = q.popleft() if p.val % 2 == 0: if p.left: if p.left.left: ans += p.left.left.val if p.left.right: ans += p.left.right.val if p.right: if p.right.left: ans += p.right.left.val if p.right.right: ans += p.right.right.val if p.left: q.append(p.left) if p.right: q.append(p.right) return ans
sum-of-nodes-with-even-valued-grandparent
95% Accepted | Using Deque | Easy to Understand | Python
its_krish_here
0
5
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,656
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2695884/Python3-Simple-Solution
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: self.res = 0 def DFS(r, p, gp): if r: if gp % 2 == 0: self.res += r.val DFS(r.left, r.val, p) DFS(r.right, r.val, p) DFS(root, -1, -1) return self.res
sum-of-nodes-with-even-valued-grandparent
Python3 Simple Solution
mediocre-coder
0
3
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,657
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2685269/python-iterative-solution
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: stk=[[root,None,None]] ans=0 while stk: temp=stk.pop() if temp[2] and not temp[2]%2: ans+=temp[0].val if temp[0].left: stk.append([temp[0].left,temp[0].val,temp[1]]) if temp[0].right: stk.append([temp[0].right,temp[0].val,temp[1]]) return ans
sum-of-nodes-with-even-valued-grandparent
python iterative solution
benon
0
8
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,658
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2623208/Simple-Traversal-or-Python
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: ans = [0] def pre(root): if not root: return if root.val&amp;1 == 0: ans[0] += root.left.left.val if root.left and root.left.left else 0 ans[0] += root.left.right.val if root.left and root.left.right else 0 ans[0] += root.right.right.val if root.right and root.right.right else 0 ans[0] += root.right.left.val if root.right and root.right.left else 0 pre(root.left) pre(root.right) pre(root) return ans[0]
sum-of-nodes-with-even-valued-grandparent
Simple Traversal | Python
RajatGanguly
0
11
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,659
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2531238/Easy-Python-Solution-or-bfs
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: stack = [] stack.append(root) ans = 0 while len(stack): size = len(stack) while size: node = stack[0] del stack[0] if node.val%2==0: if node.left and node.left.left: ans+=node.left.left.val if node.left and node.left.right: ans+=node.left.right.val if node.right and node.right.left: ans+=node.right.left.val if node.right and node.right.right: ans+=node.right.right.val if node.left: stack.append(node.left) if node.right: stack.append(node.right) size-=1 return ans
sum-of-nodes-with-even-valued-grandparent
Easy Python Solution | bfs
coolakash10
0
11
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,660
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2497107/Clean-and-easy-to-understand-python-solution
class Solution: def __init__(self): self.total = 0 def sumEvenGrandparent(self, root: TreeNode) -> int: if root.val % 2 == 0: if root.left: if root.left.left: self.total += root.left.left.val if root.left.right: self.total += root.left.right.val if root.right: if root.right.left: self.total += root.right.left.val if root.right.right: self.total += root.right.right.val if root.left: self.sumEvenGrandparent(root.left) if root.right: self.sumEvenGrandparent(root.right) return self.total
sum-of-nodes-with-even-valued-grandparent
Clean and easy to understand python solution
aruj900
0
17
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,661
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2401645/python-easy-pisy-soln-dfs
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: #algorithm just preserve their parents in stack like [node,[parents.vaues]] stk=[[root,[]]] ans=0 while stk: # print(stk) temp=stk.pop() try: if temp[1][-2]%2==0: ans+=temp[0].val except: pass #print(temp[0].val,temp[1]) new=[*temp[1],temp[0].val] if temp[0].left: stk.append([temp[0].left,new]) if temp[0].right: stk.append([temp[0].right,new]) return ans
sum-of-nodes-with-even-valued-grandparent
python easy pisy soln dfs
benon
0
12
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,662
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2207764/python-3-or-simple-dfs
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: def helper(root, parentVal, grandparentVal): if root is None: return 0 return ((1 - grandparentVal % 2) * root.val + helper(root.left, root.val, parentVal) + helper(root.right, root.val, parentVal)) return helper(root, -1, -1)
sum-of-nodes-with-even-valued-grandparent
python 3 | simple dfs
dereky4
0
16
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,663
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/2112802/Just-Some-Conditional-Cases-with-Explanation-or-DFS-Preorder-and-Recursion-or-104ms-or
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: s = 0 def dfs(root): nonlocal s if root is None: return if root.val % 2 == 0: if root.left is not None and root.left.left is not None: s += root.left.left.val if root.left is not None and root.left.right is not None: s += root.left.right.val if root.right is not None and root.right.left is not None: s += root.right.left.val if root.right is not None and root.right.right is not None: s += root.right.right.val dfs(root.left) dfs(root.right) dfs(root) return s
sum-of-nodes-with-even-valued-grandparent
Just Some Conditional Cases with Explanation | DFS Preorder & Recursion | 104ms |
omkulkarni22
0
9
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,664
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/1631510/Easy-Recursion
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: self.sums = 0 def helper(root): if root==None: return if root.left==None and root.right==None: return if root.val % 2 == 0: if root.left: if root.left.left: self.sums+=root.left.left.val if root.left.right: self.sums+=root.left.right.val if root.right: if root.right.left: self.sums+=root.right.left.val if root.right.right: self.sums+=root.right.right.val helper(root.left) helper(root.right) helper(root) return self.sums
sum-of-nodes-with-even-valued-grandparent
Easy Recursion
Manashi_itsme
0
19
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,665
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/1178496/Python-3-dfs-plus-helper-function-for-beginners
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: #global variable to count values self.c = 0 def rec(root): if root: #check if value is even if root.val%2==0: #find the values of grandchildren k = value(root) self.c+=k rec(root.left) rec(root.right) def value(root): temp = 0 #check if the root has has left present so it doesn't return None if root.left: if root.left.left: temp+=root.left.left.val if root.left.right: temp+=root.left.right.val #check if the root has has left present so it doesn't return None if root.right: if root.right.left: temp+=root.right.left.val if root.right.right: temp+=root.right.right.val return temp rec(root) return self.c
sum-of-nodes-with-even-valued-grandparent
[Python 3] dfs plus helper function for beginners
rajsinxh
0
67
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,666
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/1162423/93.91-Faster-Runtime.-Simple-Python-Solution.
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: level = [(root,None,None)] summ = 0 while level: next_level = [] for node,parent,grandParent in level: if grandParent and grandParent.val%2 == 0: summ += node.val if node.left: next_level.append((node.left,node,parent)) if node.right: next_level.append((node.right,node,parent)) level = next_level return summ
sum-of-nodes-with-even-valued-grandparent
93.91% Faster Runtime. Simple Python Solution.
bhagwataditya226
0
34
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,667
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/753788/Intuitive-Python-Recursive-DFS-beats-85-with-Comments!
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: total = 0 def helper(node): nonlocal total # Return when we run out of nodes. if not node: return # If our current nodes val is even. if node.val % 2 == 0: # Visit child nodes and their vals to the running total. if node.left and node.left.left: total += node.left.left.val if node.left and node.left.right: total += node.left.right.val if node.right and node.right.right: total += node.right.right.val if node.right and node.right.left: total += node.right.left.val # Recurse left and right subtrees. helper(node.left) helper(node.right) helper(root) return total
sum-of-nodes-with-even-valued-grandparent
Intuitive Python Recursive DFS, beats 85% with Comments!
Pythagoras_the_3rd
0
66
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,668
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/679138/PythonPython3-Sum-of-Nodes-with-Even-Valued-Grandparent
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: s = 0 #'set sum of nodes value to zero if not root: return 0 if root.val%2==0: if root.left: if root.left.left: s += root.left.left.val # if root.left: if root.left.right: s += root.left.right.val if root.right: if root.right.left: s += root.right.left.val if root.right.right: s += root.right.right.val s += self.sumEvenGrandparent(root.left) + self.sumEvenGrandparent(root.right) return s
sum-of-nodes-with-even-valued-grandparent
[Python/Python3] Sum of Nodes with Even-Valued Grandparent
newborncoder
0
95
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,669
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/478198/Python3-DFS-with-tracking
class Solution: ans = 0 def sumEvenGrandparent(self, root: TreeNode) -> int: def bfs(node: TreeNode, parent: TreeNode, grantParent: TreeNode): if grantParent and grantParent.val % 2 == 0: self.ans += node.val if node.left: bfs(node.left, node, parent) if node.right: bfs(node.right, node, parent) bfs(root, None, None) return self.ans
sum-of-nodes-with-even-valued-grandparent
[Python3] DFS with tracking
tp99
0
121
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,670
https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/477116/Python3-depth-first-traversal
class Solution: def sumEvenGrandparent(self, root: TreeNode) -> int: if not root: return 0 #edge case ans = 0 #preorder dfs stack = [(root, None, None)] #node-parent-grandparent while stack: node, parent, grand = stack.pop() if grand and grand &amp; 1 == 0: ans += node.val #even-valued grandparent if node.left: stack.append((node.left, node.val, parent)) if node.right: stack.append((node.right, node.val, parent)) return ans
sum-of-nodes-with-even-valued-grandparent
[Python3] depth-first traversal
ye15
0
81
sum of nodes with even valued grandparent
1,315
0.856
Medium
19,671
https://leetcode.com/problems/distinct-echo-substrings/discuss/1341886/Python-3-Rolling-hash-(5780ms)
class Solution: def distinctEchoSubstrings(self, text: str) -> int: n = len(text) def helper(size): base = 1 << 5 M = 10 ** 9 + 7 a = pow(base, size, M) t = 0 vis = defaultdict(set) vis_pattern = set() ans = 0 for i in range(n): t = (base * t + ord(text[i]) - ord('a')) % M if i >= size: t -= a * (ord(text[i - size]) - ord('a')) t %= M if t not in vis_pattern and (i - size * 2 + 1) in vis[t]: ans += 1 vis_pattern.add(t) if i >= size - 1: vis[t].add(i - size + 1) return ans return sum(helper(size) for size in range(1, n//2+1))
distinct-echo-substrings
[Python 3] Rolling hash (5780ms)
chestnut890123
2
203
distinct echo substrings
1,316
0.497
Hard
19,672
https://leetcode.com/problems/distinct-echo-substrings/discuss/1419989/python-or-better-than-100-or-simple-bruteforce
class Solution(object): def distinctEchoSubstrings(self, text): n=len(text) ans=0 s=set() for i in range(1,n): for j in range(i//2+1,i+1): l=i+1-j temp=text[j-l:j] if temp==text[j:i+1]: s.add(temp) return len(s)
distinct-echo-substrings
python | better than 100% | simple bruteforce
heisenbarg
0
166
distinct echo substrings
1,316
0.497
Hard
19,673
https://leetcode.com/problems/distinct-echo-substrings/discuss/477257/Python3-simple-accepted-solution
class Solution: def distinctEchoSubstrings(self, text: str) -> int: res = [] for i in range(len(text)): for j in range(i+1,len(text)+1): if len(text[i:j])%2==0 and text[i:j][:len(text[i:j])//2]==text[i:j][len(text[i:j])//2:] and text[i:j] not in res: res.append(text[i:j]) return len(res)
distinct-echo-substrings
Python3 simple accepted solution
jb07
0
78
distinct echo substrings
1,316
0.497
Hard
19,674
https://leetcode.com/problems/distinct-echo-substrings/discuss/477121/Python3-Brute-force-solution
class Solution: def distinctEchoSubstrings(self, text: str) -> int: ans = set() for i in range(len(text)-1): for j in range(i+1, (i+len(text))//2+1): if text[i:j] == text[j:2*j-i]: ans.add(text[i:j]) return len(ans)
distinct-echo-substrings
[Python3] Brute force solution
ye15
0
53
distinct echo substrings
1,316
0.497
Hard
19,675
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1219360/Python-Fast-and-Easy-Soln
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: left = 0 right = n ans = [] while True: if str(left).count("0")==0 and str(right).count("0")==0: ans.append(left) ans.append(right) break left+=1 right-=1 return ans
convert-integer-to-the-sum-of-two-no-zero-integers
Python Fast & Easy Soln
iamkshitij77
2
212
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,676
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/2812823/Python-Easy-Solution-Faster-Than-98.86
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: def check(num): while num>0: if num%10==0: return False num//=10 return True for i in range(1,n): t=n-i if check(t) and check(i): return [i,t]
convert-integer-to-the-sum-of-two-no-zero-integers
Python Easy Solution Faster Than 98.86%
DareDevil_007
1
49
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,677
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1115791/Python3-simple-solution-beats-100-user
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: for i in range(1,n): if '0' not in str(i) and '0' not in str(n-i): return [i,n-i]
convert-integer-to-the-sum-of-two-no-zero-integers
Python3 simple solution beats 100% user
EklavyaJoshi
1
108
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,678
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/915654/Python3-100-faster-100-less-memory-(16ms-14.1mb)
class Solution: def getNoZeroIntegers(self, n): for x in range(1, n): for c in str(x): if c == '0': break else: for y in str(tmp := n - x): if y =='0': break else: return x, tmp
convert-integer-to-the-sum-of-two-no-zero-integers
Python3 100% faster, 100% less memory (16ms, 14.1mb)
haasosaurus
1
363
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,679
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/477732/Python-3-(brute-force)-(two-lines)-(beats-100)-(24-ms)
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: for i in range(1,n): if '0' not in str(i) + str(n-i): return [i,n-i] - Junaid Mansuri - Chicago, IL
convert-integer-to-the-sum-of-two-no-zero-integers
Python 3 (brute force) (two lines) (beats 100%) (24 ms)
junaidmansuri
1
252
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,680
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/2069648/Using-while-loop-to-solve-22ms-Best-Faster-~97
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: a, b = 1, n - 1 while "0" in str(a) + str(b): a += 1 b -= 1 return [a, b]
convert-integer-to-the-sum-of-two-no-zero-integers
Using while loop to solve 22ms Best Faster ~97%
andrewnerdimo
0
51
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,681
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/2047550/Python-simple-solution
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: ans = [] for i in range(1, n): if '0' not in str(i) and '0' not in str(n-i): ans.append(i) ans.append(n-i) break return ans
convert-integer-to-the-sum-of-two-no-zero-integers
Python simple solution
StikS32
0
77
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,682
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1922973/Python-easy-solution-with-memory-less-than-97
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: for i in range(1, n+1): if str(i).count('0') == 0 and str(n - i).count('0') == 0: return [i, n-i]
convert-integer-to-the-sum-of-two-no-zero-integers
Python easy solution with memory less than 97%
alishak1999
0
78
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,683
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1879500/Python-or-Simple-Solution
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: num1 = 1 num2 = n-1 while True: if ('0' in str(num1)) or ('0' in str(num2)): num1 += 1 num2 -= 1 else: return [num1, num2]
convert-integer-to-the-sum-of-two-no-zero-integers
Python | Simple Solution
i_architect
0
66
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,684
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1848846/2-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-97
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: for i in range(1,n): if '0' not in str(i)+str(n-i): return [i,n-i]
convert-integer-to-the-sum-of-two-no-zero-integers
2-Lines Python Solution || 60% Faster || Memory less than 97%
Taha-C
0
42
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,685
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1848846/2-Lines-Python-Solution-oror-60-Faster-oror-Memory-less-than-97
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: return next([i,n-i] for i in range(n) if '0' not in str(i)+str(n-i))
convert-integer-to-the-sum-of-two-no-zero-integers
2-Lines Python Solution || 60% Faster || Memory less than 97%
Taha-C
0
42
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,686
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1820531/python-solution
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: if n % 2 == 0: for i in range(n//2+1): if str(n//2-i).count('0') == 0 and str(n//2+i).count('0') == 0: return [n//2-i,n//2+i] else: for i in range(n//2+1): if str(n//2-i).count('0') == 0 and str(n//2+1+i).count('0') == 0: return [n//2-i,n//2+i+1]
convert-integer-to-the-sum-of-two-no-zero-integers
python solution
MS1301
0
31
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,687
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1533462/Python-Weird-Binary-Search
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: lo, hi = 1, n-1 while lo < hi: mid = (lo + hi) // 2 if '0' in str(n-mid) + str(mid): hi = mid + 1 else: return [mid, n-mid] return [lo, n-lo]
convert-integer-to-the-sum-of-two-no-zero-integers
[Python] Weird Binary Search
dev-josh
0
57
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,688
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/1106805/Python3-brute-force
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: for x in range(1, n//2+1): if "0" not in str(x) and "0" not in str(n-x): return [x, n-x]
convert-integer-to-the-sum-of-two-no-zero-integers
[Python3] brute-force
ye15
0
38
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,689
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/708012/simple-of-simple
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: a = 1 while True: b = n-a if '0' not in str(a) and \ '0' not in str(b): break a += 1 return [a, b]
convert-integer-to-the-sum-of-two-no-zero-integers
simple of simple
seunggabi
0
57
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,690
https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/discuss/668370/Python-O(logN)-beats-100
class Solution: def getNoZeroIntegers(self, n: int) -> List[int]: if n<10: return [1,n-1] a = int(str(n)[1:])+1 a = int(''.join(['1' if i == '0' else i for i in str(a)])) return [a,n-a]
convert-integer-to-the-sum-of-two-no-zero-integers
Python O(logN) beats 100%
AWDawson
0
123
convert integer to the sum of two no zero integers
1,317
0.56
Easy
19,691
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/489623/Python-Simple-Solution-Python-Memory-usage-less-than-100
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: count = 0 while a or b or c: if (a &amp; 1) | (b &amp; 1) != (c &amp; 1): if (c &amp; 1): count += 1 else: count += (a &amp; 1) + (b &amp; 1) a, b, c = a >> 1, b >> 1, c >> 1 return count
minimum-flips-to-make-a-or-b-equal-to-c
Python - Simple Solution - Python - Memory usage less than 100%
mmbhatk
5
339
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,692
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/477759/Python-3-(five-lines)-(beats-100)-(20-ms)
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: a, b, c, t, D = bin(a)[2:], bin(b)[2:], bin(c)[2:], 0, {'010':1, '100':1, '001':1, '110':2} M = max(len(a),len(b),len(c)) a, b, c = a.zfill(M), b.zfill(M), c.zfill(M) for i in range(M): t += D.get(a[i]+b[i]+c[i],0) return t - Junaid Mansuri - Chicago, IL
minimum-flips-to-make-a-or-b-equal-to-c
Python 3 (five lines) (beats 100%) (20 ms)
junaidmansuri
2
173
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,693
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/2808950/Python3-Solution-or-Bit-Manipulation
class Solution: def minFlips(self, a, b, c): count = lambda x : bin(x).count('1') return count((a | b) ^ c) + count(a &amp; b &amp; ~c)
minimum-flips-to-make-a-or-b-equal-to-c
✔ Python3 Solution | Bit Manipulation
satyam2001
1
21
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,694
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/2283071/Python3-Bit-Manipulation-Constant-Time-and-Memory
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: res = 0 for i in range(32): if (a &amp; 1) | (b &amp; 1) != (c &amp; 1): if (c &amp; 1) == 1: # (a &amp; 1) | (b &amp; 1) should be == 1 ; so changing any of a, b we can get 1 res += 1 else: # (a &amp; 1) | (b &amp; 1) should be == 0 ; is (a &amp; 1) == 1 and (b &amp; 1) == 1 we need to change both to 0 so res += 1; if any of them is 1 then change only 1 i.e. res += 1 res += (a &amp; 1) + (b &amp; 1) a, b, c = a>>1, b>>1, c>>1 # right-shift by 1 return res # Time: O(1) # Space: O(1)
minimum-flips-to-make-a-or-b-equal-to-c
[Python3] Bit Manipulation Constant Time and Memory
samirpaul1
1
78
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,695
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/2721742/Python3-or-Clean-and-Concise-or-Bit-Manipulation
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: ans=0 for i in range(32): bit_a = (a&amp;(1<<i)) >> i bit_b = (b&amp;(1<<i)) >> i bit_c = (c&amp;(1<<i)) >> i if bit_a | bit_b != bit_c: ans+=max(1,bit_a + bit_b) else: continue return ans
minimum-flips-to-make-a-or-b-equal-to-c
[Python3] | Clean & Concise | Bit Manipulation
swapnilsingh421
0
6
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,696
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/2709256/Simple-python-code-with-explanation
class Solution: def minFlips(self, a, b, c): #create a variable count to store flips count = 0 #this while loop works untill #all the current bits of a , b , c = 0 while (a!= 0 or b!=0 or c!=0): #store the (a&amp;1)last bit of a in x x = (a&amp;1) #store the (b&amp;1)last bit of b in y y = (b&amp;1) #store the (c&amp;1)last bit of c in z z = (c&amp;1) #if (x or y) is not equal to z #then there is need to flip if (x | y != z): #if the x == 1 | y == 1 not equal to z #that means z = 0 #so we need to change both bits (x,y) if x == 1 and y == 1 : #so count = count + 2 count +=2 #in all the 3 situations below #x = 0 | y = 1 != z(0) #x = 1 | y = 0 != z(0) #x = 0 | y = 0 != z(1) else: #we need only one change #so count = count + 1 count +=1 #right shift the bits in a by 1 a>>=1 #right shift the bits in b by 1 b>>=1 #right shift the bits in c by 1 c>>=1 #after filping all the bits required #return the count return count
minimum-flips-to-make-a-or-b-equal-to-c
Simple python code with explanation
thomanani
0
2
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,697
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/2681248/Python3-Solution-oror-O(log-N)-Time-and-O(1)-Space-Complexity
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: if a|b==c: return 0 flipCount=0 while a>0 or b>0 or c>0: if (a|b)&amp;1 != c&amp;1: if c&amp;1==1: flipCount+=1 else: if a&amp;1==1: flipCount+=1 if b&amp;1==1: flipCount+=1 a,b,c=a>>1,b>>1,c>>1 return flipCount
minimum-flips-to-make-a-or-b-equal-to-c
Python3 Solution || O(log N) Time & O(1) Space Complexity
akshatkhanna37
0
1
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,698
https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c/discuss/2424293/Python-Bit-Manipulation-95-faster
class Solution: def minFlips(self, a: int, b: int, c: int) -> int: count = 0 while a != 0 or b!= 0 or c!=0: x , y, z = b&amp;1,a&amp;1,c&amp;1 if (x|y)!=z: if x == 1 and y == 1: count += 2 else: count+=1 b >>= 1 c >>= 1 a >>= 1 return count
minimum-flips-to-make-a-or-b-equal-to-c
[Python] Bit Manipulation 95% faster
Nish786
0
51
minimum flips to make a or b equal to c
1,318
0.66
Medium
19,699