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/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/1916791/One-for-loop-with-list-slicing | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
# arr = [1,2,1,1,2,1,1,1,3], m = 3, k = 2
for i in range(len(arr)):
p = arr[i:i+m] # sub-array of length m
if arr[i:i+m*k]==p*k: # check if sub-array of length m*k == p*k
return True... | detect-pattern-of-length-m-repeated-k-or-more-times | One for loop with list slicing | back2square1 | 0 | 44 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,100 |
https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/1884940/Python3-Solution | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
for i in range(len(arr) - m * k + 1):
pattern = arr[i:i + m]
for j in range(k - 1):
if arr[i + m * (j + 1):i + m * (j + 2)] != pattern:
break
if j ==... | detect-pattern-of-length-m-repeated-k-or-more-times | Python3 Solution | hgalytoby | 0 | 41 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,101 |
https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/1480365/One-pass-of-k-slicing-88-speed | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
len_arr = len(arr)
if len_arr < m * k:
return False
for start in range(len_arr + 1 - m * k):
pattern = arr[start: start + m]
if all(arr[start + i * m: start + (i + 1) * m] =... | detect-pattern-of-length-m-repeated-k-or-more-times | One pass of k-slicing, 88% speed | EvgenySH | 0 | 118 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,102 |
https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/1022315/Python-3.-Iterate-over-pre-defined-sliding-windows-easy-to-understand | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
str_arr = str(arr[0])
for ii in range(1, len(arr)):
str_arr += str(arr[ii])
for jj in range(len(arr)-m+1):
pat = str_arr[jj:jj+m]
st_idx = list(range(jj, len(arr)-m+1, m))
... | detect-pattern-of-length-m-repeated-k-or-more-times | Python 3. Iterate over pre-defined sliding windows; easy to understand | user4043Xl | 0 | 104 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,103 |
https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/920387/Python-3-faster-than-98.70-Memory-Usage-less-than-100.00 | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
for i in range(len(arr)):
## length not long enough
if len(arr) - i < m * k:
break
hasPattern = True
# check the value index ranged (i + m, i + m * k -1) against (i, i + m -1) co... | detect-pattern-of-length-m-repeated-k-or-more-times | Python 3 - faster than 98.70%, Memory Usage less than 100.00% | zju3757 | 0 | 222 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,104 |
https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/823157/Intuitive-approach-by-iteratively-search | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
if m == 1 and k == 1:
return True
mk_len = m * k
arr_str = ''.join(map(lambda e: str(e), arr))
for i in range(len(arr) - m*k + 1):
if arr_str[i:i+mk_len] == arr_str... | detect-pattern-of-length-m-repeated-k-or-more-times | Intuitive approach by iteratively search | puremonkey2001 | 0 | 59 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,105 |
https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/discuss/1179938/easy-solution | class Solution:
def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
i=0
d=m
if m >len(arr) or k >len(arr):
return(False)
while d < len(arr):
p=arr[i:d]
count=1
j,l=i+m,d+m
while l<len(arr)+1:
... | detect-pattern-of-length-m-repeated-k-or-more-times | easy solution | janhaviborde23 | -1 | 94 | detect pattern of length m repeated k or more times | 1,566 | 0.436 | Easy | 23,106 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/819332/Python3-7-line-O(N)-time-and-O(1)-space | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
ans = pos = neg = 0
for x in nums:
if x > 0: pos, neg = 1 + pos, 1 + neg if neg else 0
elif x < 0: pos, neg = 1 + neg if neg else 0, 1 + pos
else: pos = neg = 0 # reset
ans = max(ans, pos)
... | maximum-length-of-subarray-with-positive-product | [Python3] 7-line O(N) time & O(1) space | ye15 | 59 | 2,700 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,107 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1742691/Python-Sol-by-tracking-%2Bve-and-ve-subarray-lengths. | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
max_len = pos = neg = 0
for i in nums:
if i == 0: # Case 1 -> Reset the subarrays
pos = neg = 0
elif i > 0: # Case 2 -> +ve subarray remains +ve and -ve remains -ve after adding the new value
... | maximum-length-of-subarray-with-positive-product | Python Sol by tracking +ve & -ve subarray lengths. | anCoderr | 11 | 387 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,108 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1759738/Python-O(n)-simple-solution | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
ans = neg = pos = 0
for x in nums:
if x > 0:
if neg > 0:
neg += 1
pos += 1
elif x < 0:
neg, pos = pos+1, (neg+1 if neg else 0)
else:
... | maximum-length-of-subarray-with-positive-product | Python O(n) simple solution | DaiSier | 8 | 566 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,109 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1790137/Python3-O(n)-time-O(1)-space | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
curLenPos = 0
curLenNeg = 0
ans = 0
for num in nums:
if num > 0:
curLenPos,curLenNeg = curLenPos + 1, curLenNeg + 1 if curLenNeg != 0 else 0
elif num < 0:
curLenNeg,cu... | maximum-length-of-subarray-with-positive-product | Python3 O(n) time O(1) space | Yihang-- | 6 | 287 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,110 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1487643/Beats-100-or-Detailed-Explanation-or-From-Beginner-to-Pro-or-Python | class Solution:
def findMax(self,start,end,totalNegitive,firstNegitive,lastNegitive):
if totalNegitive%2==0:
return end+1-start
else:
if firstNegitive:
return max(lastNegitive-start,end-firstNegitive)
else:
return end-start
def... | maximum-length-of-subarray-with-positive-product | Beats 100% | Detailed Explanation | From Beginner to Pro | Python | kshitijb | 2 | 498 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,111 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/839439/Python-Greedy-Solution | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
def helper(nums):
result = 0
positiveSoFar = 0
negativeSoFar = 0
for i in range(len(nums)):
if nums[i] == 0:
positiveSoFar = 0
negati... | maximum-length-of-subarray-with-positive-product | Python Greedy Solution | pochy | 2 | 318 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,112 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1689625/A-fast-O(n)-Python-solution | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
n = len(nums)
start = 0
neg_places = [] # track placement of negatives, and how many there are
longest = 0
# Note adding a zero to the end of the array for the loop because when x==0 we check lengths and update
... | maximum-length-of-subarray-with-positive-product | A fast O(n) Python solution | briancoj | 1 | 197 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,113 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/822633/Python-O(n)-or-Split-on-0-or-Explanation | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
start = 0 ## starting index of subarray
ans = 0
first_neg, last_neg = -1, -1 ## first and last negative initially -1
c = 0 ## count of... | maximum-length-of-subarray-with-positive-product | Python O(n) | Split on 0 | Explanation | mihirrane | 1 | 137 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,114 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2801449/Python-(Simple-Dynamic-Programming) | class Solution:
def getMaxLen(self, nums):
n = len(nums)
pos, neg = [0]*n, [0]*n
pos[0] = 1 if nums[0] > 0 else 0
neg[0] = 1 if nums[0] < 0 else 0
for i in range(1,n):
if nums[i] > 0:
pos[i] = pos[i-1] + 1
neg[i] = neg[i-1] + 1 ... | maximum-length-of-subarray-with-positive-product | Python (Simple Dynamic Programming) | rnotappl | 0 | 2 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,115 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2743162/Simple-Fast-and-*Readable*-Python-Explained.-O(n)-Time-O(1)-Space. | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
positive = 0
negative = 0
max_len = 0
for num in nums:
if num == 0:
# Case 1: Reset our subarrays.
positive = 0
negative = 0
elif num > 0:
... | maximum-length-of-subarray-with-positive-product | Simple, Fast, and *Readable* Python Explained. O(n) Time, O(1) Space. | ShippingContainer | 0 | 9 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,116 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2704472/Easy-to-understand-O(n)-Python-solution-(Beat-90) | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
neg, pos, res = 0, 0, 0
for n in nums:
new_neg, new_pos = 0, 0
# n is positvie
if n > 0:
new_pos = pos + 1
new_neg = neg + 1 if neg else 0
... | maximum-length-of-subarray-with-positive-product | Easy to understand O(n) Python solution (Beat 90%) | Vansterochrhoger | 0 | 11 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,117 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2524420/Easy-python-solution-TC%3A-O(N) | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
dp=[-1]*len(nums)
neg=[]
ze=-1
for i in range(len(nums)):
if nums[i]==0:
dp[i]=0
neg=[]
ze=i
else:
if nums[i]<0:
if... | maximum-length-of-subarray-with-positive-product | Easy python solution TC: O(N) | shubham_1307 | 0 | 88 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,118 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2511965/Python-runtime-90.21-memory-43.69 | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
pos = [0]
neg = [0]
for n in nums:
if n == 0:
pos.append(0)
neg.append(0)
elif n > 0:
pos.append(pos[-1]+1)
if neg[-1] != 0:
... | maximum-length-of-subarray-with-positive-product | Python, runtime 90.21%, memory 43.69% | tsai00150 | 0 | 92 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,119 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2511965/Python-runtime-90.21-memory-43.69 | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
ans = negCount = curCount = 0
zeroPos = firstNegPos = -1
for i, e in enumerate(nums):
if e == 0:
negCount = 0
zeroPos = firstNegPos = i
continue
elif e < 0:
... | maximum-length-of-subarray-with-positive-product | Python, runtime 90.21%, memory 43.69% | tsai00150 | 0 | 92 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,120 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2483418/Yet-another-python-O(n)-O(n)-lazy-solution | class Solution:
def splitZeros(self, nums: List[int]) -> tuple:
# partition into nonzero consecutive subarrays
# with detecting negative elements local indices
nnz, nnz_buf = [], []
negs, negs_buf = [], []
i = 0
for num in nums:
if num != 0:
... | maximum-length-of-subarray-with-positive-product | Yet another python O(n) O(n) lazy solution | thatdeep | 0 | 78 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,121 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2470577/Python-dp-with-space-compression | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
# p[i] represents the maximum length of subarray with positive product
# including nums[i]
p = 0
# n[i] represents the maximum length of subarray with positive product
# including nums[i]
n = 0
# Ini... | maximum-length-of-subarray-with-positive-product | Python dp with space compression | Aden-Q | 0 | 36 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,122 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2470428/Python-sliding-window-O(N)-time-O-(1)-space-solution | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
nums.append(0)
left, right = 0, 0
res = 0
while left < len(nums) and right < len(nums):
if nums[left] == 0:
left += 1
continue
# left points to the first nonz... | maximum-length-of-subarray-with-positive-product | Python sliding window O(N) time O (1) space solution | Aden-Q | 0 | 63 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,123 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2401873/python-O(n) | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
start = 0
neg = 0
sn = -1
en = -1
ans = 0
for i in range(len(nums)):
if nums[i] < 0:
neg += 1
if sn == -1:
sn = i
... | maximum-length-of-subarray-with-positive-product | python O(n) | akashp2001 | 0 | 125 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,124 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/2292274/Python-O(n)-runtime-O(1)-space | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
if len(nums) == 1:
return 1 if nums[0] > 0 else 0
negative_count = 0
first_negative_index = -1
max_len = 0
nearest_zero_point = -1
for i,n in enumerate(nums):
if n == 0:
... | maximum-length-of-subarray-with-positive-product | Python O(n) runtime O(1) space | JimmyZhu1234 | 0 | 101 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,125 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1849793/Python-One-Pass-or-First-ever-solution-posted-by-me | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
n=len(nums)
totalNeg, firstNegIndex, lastNegIndex = 0, n, -1
start = maxLength = 0
for i,num in enumerate(nums):
#if zero, divide the array into subarray and calculate maxLength until now
... | maximum-length-of-subarray-with-positive-product | Python One Pass | First ever solution posted by me | deepanksinghal | 0 | 317 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,126 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1750840/Python%3A-Calculate-lengths-using-indexes | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
"""
All subarrays can be found between zeroes. When there are negative numbers involved, we
only care about calculating the lengths of subarrays containing even number of negative
numbers, otherwise the product of subarray... | maximum-length-of-subarray-with-positive-product | Python: Calculate lengths using indexes | johnro | 0 | 184 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,127 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1706310/2-pass-scan-beats-100-memory.-O(n)-time-O(1)-space | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
# reduce calculation time
for i, n in enumerate(nums):
if n < 0:
nums[i] = -1
elif n > 0:
nums[i] = 1
# from left to right, and from right to left
return max(self.getM... | maximum-length-of-subarray-with-positive-product | 2 pass scan, beats 100% memory. O(n) time, O(1) space | r04922101 | 0 | 144 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,128 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1691863/Split-the-array-int-subarray-with-no-zero-element.-and-then-calculate-the-maximum-length-of-it | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
num_size = len(nums) - 1
# Edge case for only one element.
if num_size == 0:
return 1 if nums[0] > 0 else 0
def _find_max_len(bi, ei, ni_list):
"""Find maximum length.
... | maximum-length-of-subarray-with-positive-product | Split the array int subarray with no zero element. and then calculate the maximum length of it | puremonkey2001 | 0 | 108 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,129 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/1601136/Python3-One-pass-solution | class Solution:
def getMaxLen(self, nums: List[int]) -> int:
res = neg = pos = 0
for i in range(len(nums)):
if nums[i] > 0:
pos += 1
if neg != 0:
neg += 1
elif nums[i] < 0:
neg, pos = pos, neg
... | maximum-length-of-subarray-with-positive-product | [Python3] One pass solution | maosipov11 | 0 | 177 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,130 |
https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/861226/Python-Simple-and-concise-linear-time-solution-O(1)-space | class Solution:
@staticmethod
def process(st, end, cnt_neg, arr):
if st >= 0 and st <= end and end >= 0:
if not (cnt_neg % 2):
return end - st + 1
first_neg_ind = st
last_neg_ind = end
while(first_neg_ind <= end and arr[first_neg_ind]... | maximum-length-of-subarray-with-positive-product | [Python] Simple and concise linear time solution O(1) space | vasu6 | 0 | 108 | maximum length of subarray with positive product | 1,567 | 0.438 | Medium | 23,131 |
https://leetcode.com/problems/minimum-number-of-days-to-disconnect-island/discuss/819360/Python3-bfs | class Solution:
def minDays(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0]) # dimension
grid = "".join("".join(map(str, x)) for x in grid)
@lru_cache(None)
def fn(s):
"""Return True if grid is disconnected."""
row, grid = [], []
... | minimum-number-of-days-to-disconnect-island | [Python3] bfs | ye15 | 0 | 66 | minimum number of days to disconnect island | 1,568 | 0.468 | Hard | 23,132 |
https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/discuss/819349/Python3-math-ish | class Solution:
def numOfWays(self, nums: List[int]) -> int:
def fn(nums):
"""Post-order traversal."""
if len(nums) <= 1: return len(nums) # boundary condition
ll = [x for x in nums if x < nums[0]]
rr = [x for x in nums if x > nums[0]]
l... | number-of-ways-to-reorder-array-to-get-same-bst | [Python3] math-ish | ye15 | 2 | 337 | number of ways to reorder array to get same bst | 1,569 | 0.481 | Hard | 23,133 |
https://leetcode.com/problems/number-of-ways-to-reorder-array-to-get-same-bst/discuss/2783524/A-cheating-way | class Solution:
def numOfWays(self, nums: List[int]) -> int:
FACT = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 227020758, 178290591, 674358851, 789741546, 425606191, 660911389, 557316307, 146326063, 72847302, 602640637, 860734560, 657629300, 440732388, 459042011, 3... | number-of-ways-to-reorder-array-to-get-same-bst | A cheating way | Fredrick_LI | 0 | 4 | number of ways to reorder array to get same bst | 1,569 | 0.481 | Hard | 23,134 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1369404/PYTHON-Best-solution-with-explanation.-SC-%3A-O(1)-TC-%3A-O(n) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
"""
The primary diagonal is formed by the elements A00, A11, A22, A33.
Condition for Primary Diagonal:
The row-column condition is row = column.
The secondary diagonal is formed by the elements A03, A12,... | matrix-diagonal-sum | [PYTHON] Best solution with explanation. SC : O(1) TC : O(n) | er1shivam | 5 | 233 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,135 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2074738/Python-Easy-To-Understand-Solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
total = 0
for i, x in enumerate(mat):
if i == len(mat)-i-1:
total += x[i]
else:
total += x[i] + x[len(mat)-i-1]
return total | matrix-diagonal-sum | Python Easy To Understand Solution | Shivam_Raj_Sharma | 4 | 182 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,136 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1775861/Python3-solution-or-O(n)-or-Single-For-loop-or-86.30-lesser-memory | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
x = 0
y = len(mat)-1
dsum = 0
for _ in range(len(mat)):
if x == y:
dsum += mat[x][x]
x += 1
y -= 1
continue
dsum += (ma... | matrix-diagonal-sum | Python3 solution | O(n) | Single For loop | 86.30% lesser memory | Coding_Tan3 | 3 | 146 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,137 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2573377/SIMPLE-PYTHON3-SOLUTION-lineartime | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = []
count = 0
rightcount = len(mat[0]) - 1
for i in range(len(mat)):
if rightcount == count: ans.append(mat[i][count])
if i == count and rightcount != count:
ans.append(ma... | matrix-diagonal-sum | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔ lineartime | rajukommula | 1 | 108 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,138 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2238950/Python3-one-loop-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = len(mat)
result,primary,i,secondary = 0,0,0,n-1
while i < n:
x, y = mat[i][primary], mat[i][secondary]
if primary!=secondary:
result+= x + y
else:
... | matrix-diagonal-sum | 📌 Python3 one loop solution | Dark_wolf_jss | 1 | 34 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,139 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1748070/Easy-Python | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
r = len(mat)
s = 0
for i in range(r):
for j in range(r):
if i == j or i+j == r-1:
s += mat[i][j]
return s | matrix-diagonal-sum | Easy Python | MengyingLin | 1 | 53 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,140 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1437485/Python-Simple-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
s = 0
for x in range(len(mat)):
s += mat[x][x] + mat[x][-x-1]
if len(mat) % 2 != 0:
s -= mat[len(mat)//2][len(mat)//2]
return s | matrix-diagonal-sum | [Python] Simple solution | sushil021996 | 1 | 86 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,141 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1394870/Python3-Faster-than-97-of-the-Solutions | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
s = 0
l = len(mat)
for i in range(l):
s = s + mat[i][i]
for i in range(l):
s = s + mat[i][l-i-1]
if l%2 != 0:
s = s - mat[l//2][l//2]
... | matrix-diagonal-sum | Python3 - Faster than 97% of the Solutions | harshitgupta323 | 1 | 36 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,142 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1136020/Python-faster-than-94 | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
d1, d2 = 0, 0
for i in range(len(mat)):
# top left to bottom right
d1 += mat[i][i]
# bottom left to top right, skips if it reaches the mid
d2 += mat[len(mat)-i-1][i] if len(mat)-i-1 != i else 0... | matrix-diagonal-sum | Python faster than 94% | 111110100 | 1 | 137 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,143 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/830529/Python3-straightforward-5-line | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
for i in range(len(mat)):
ans += mat[i][i]
if 2*i+1 != len(mat): ans += mat[i][~i]
return ans | matrix-diagonal-sum | [Python3] straightforward 5-line | ye15 | 1 | 49 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,144 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2839517/one-liner-easy-to-understand-python-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
return sum(map(lambda idx: mat[idx][idx]+mat[idx][len(mat)-1-idx] if idx!=len(mat)-1-idx else mat[idx][idx],range(len(mat)))) | matrix-diagonal-sum | one-liner, easy to understand python solution | goezsoy | 0 | 1 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,145 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2833459/Simple-Python-Solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
length = len(mat)
summ = 0
for i in range(length):
for j in range(length):
if i== j:
summ += mat[i][j]
elif i+j == length-1:
summ += mat[i][... | matrix-diagonal-sum | Simple Python Solution | danishs | 0 | 1 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,146 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2832914/Python3-one-line-solution-beats-94-with-explanation | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
return sum([mat[i][i] for i in range(len(mat))] + [mat[i][len(mat)-i-1] for i in range(len(mat)) if i/1 != (len(mat)-1) / 2]) | matrix-diagonal-sum | Python3 one-line solution beats 94% - with explanation | sipi09 | 0 | 2 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,147 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2832914/Python3-one-line-solution-beats-94-with-explanation | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
for i in range(len(mat)):
ans += mat[i][i]
if i/1 != (len(mat)-1) / 2:
ans += mat[i][len(mat)-i-1]
return ans | matrix-diagonal-sum | Python3 one-line solution beats 94% - with explanation | sipi09 | 0 | 2 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,148 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2817567/Easy-Beginner-Friendly-Solution-Python | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
c = 0
for i in range(len(mat)):
temp = mat[i]
for j in range(len(temp)):
if(i == j or i+j == len(mat)-1):
c += temp[j]
return c | matrix-diagonal-sum | [Easy Beginner Friendly Solution Python] | imash_9 | 0 | 2 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,149 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2814908/Matrix-Diagonal-Sum-or-Python-3.x-or-O(n) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
side = len(mat)
if side % 2 == 0:
correction = 0
else:
correction = mat[side // 2][side // 2]
sum_diags = sum(mat[idx][idx] + mat[idx][side - idx - 1] for idx in range(side))... | matrix-diagonal-sum | Matrix Diagonal Sum | Python 3.x | O(n) | SnLn | 0 | 3 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,150 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2814883/Matrix-Diagonal-Sum-or-Python-3.x | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
side = len(mat)
if side % 2 == 0:
correction = 0
else:
correction = mat[side // 2][side // 2]
prime_diag = sum(mat[idx][idx] for idx in range(side))
secon_diag = sum(... | matrix-diagonal-sum | Matrix Diagonal Sum | Python 3.x | SnLn | 0 | 2 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,151 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2809547/MATRIX-DIAGONAL-SUM-or-PYTHON | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
j = len(mat) - 1
count = 0
for i in range(len(mat)):
count += mat[i][i] + mat[i][j - i]
if len(mat) % 2 == 0:
return count
return count - mat[int(j/2)][int(j/2)] | matrix-diagonal-sum | MATRIX DIAGONAL SUM | PYTHON | jashii96 | 0 | 2 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,152 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2772678/Python-Fastest-Solution-Beats-93-users-with-full-explanation-in-comments | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n=len(mat)
sum=0
mid=n//2
#loop for iteration in matrix
for i in range(n):
#for primary diagonal we have to add the values of mat[i][i] like [0][0] ,[1][1] position
sum+=mat[i][i]
... | matrix-diagonal-sum | Python Fastest Solution Beats 93 % users with full explanation in comments | mritunjayyy | 0 | 1 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,153 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2746262/Use-minus-index-to-find-inverse-line | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
"""TC: O(n), SC: O(n)"""
diag_sum = 0
for i in range(len(mat)):
diag_sum += mat[i][i]
diag_sum += mat[-i-1][i]
if len(mat) % 2 == 1:
middle_value = mat[int(len(mat)/2)][int(len(mat... | matrix-diagonal-sum | Use minus index to find inverse line | woora3 | 0 | 2 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,154 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2738477/python-easy-solution-93-faster | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
n = len(mat)
for i in range(n):
k = mat[i] # row data list
ans += k[i] + k[-(i+1)] # sum diagonal element
if n > 1:
if n&1:
ans -= mat[n//2][n//2] # su... | matrix-diagonal-sum | python easy solution 93% faster | arifkhan1990 | 0 | 4 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,155 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2682272/Python-or-Simple-O(n)-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
for i in range(len(mat)):
ans += mat[i][i]
for i in range(len(mat) - 1, -1, -1):
if len(mat) and len(mat) - i - 1 == i:
continue
ans += mat[len(mat) - i - 1][i]
... | matrix-diagonal-sum | Python | Simple O(n) solution | LordVader1 | 0 | 20 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,156 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2668317/Pythonor-O(N)-Time-Complexityor-n2-time-loop-execution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
res = 0
n = len(mat)
for i in range(0,n//2 if n%2==0 else n//2 + 1):
if i != n-i-1:
res = res + mat[i][i] + mat[i][n-i-1] + mat[n-i-1][i] + mat[n-i-1][n-i-1]
else:
res ... | matrix-diagonal-sum | Python| O(N) Time Complexity| n//2 time loop execution | tanaydwivedi095 | 0 | 3 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,157 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2625461/Simple-approach-or-Python-or-begineers-friendly | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
row=len(mat)
dia_left=0
for i in range(0,row):
dia_left+=mat[i][i]
left=0
right=row-1
dia_right=0
for i in range(0,row):
dia_right+=mat[left][right]
left+=1... | matrix-diagonal-sum | Simple approach | Python | begineers friendly | Mom94 | 0 | 6 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,158 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2556639/Easy-Python3-Solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
summ=0
for i in range(len(mat)):
summ+=mat[i][i]
for i in range(len(mat)):
if len(mat[i])-1-i!=i:
summ+=mat[i][len(mat[i])-1-i]
return summ | matrix-diagonal-sum | Easy Python3 Solution | pranjalmishra334 | 0 | 31 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,159 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2549638/Python-matrix-O(n) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
s = 0
for i in range(len(mat)):
s += mat[i][i]
if i != len(mat) - i - 1:
s += mat[i][len(mat) - i - 1]
return s | matrix-diagonal-sum | Python matrix O(n) | nika_macharadze | 0 | 24 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,160 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2521506/Python-or-Short-and-easy-or-Explained | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = len(mat)
# add both primary and secondary diagonals
s = sum([mat[i][i] + mat[i][n-1-i] for i in range(n)])
# if matrix size is odd, the middle element will be repeated --> subtract it
if... | matrix-diagonal-sum | Python | Short and easy | Explained | anon82214 | 0 | 36 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,161 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2463976/Python-using-builtin-sum | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
dim = len(mat[0])
center = dim // 2
s = sum(mat[idx][idx] + mat[dim - idx - 1][idx] for idx in range(dim))
return s - mat[center][center] if dim & 1 else s | matrix-diagonal-sum | Python using builtin sum | Potentis | 0 | 12 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,162 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2448198/Python-Simple-Solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
for i in range(len(mat)):
ans += (mat[i][i] + mat[i][-i-1])
if len(mat)%2 != 0:
ans -= mat[len(mat)//2][len(mat)//2]
return ans | matrix-diagonal-sum | Python Simple Solution | xinhuangtien | 0 | 32 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,163 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2431950/Python-indexing-but-not-memory-efficient | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = len(mat)
total = sum([mat[i][i] + mat[i][n - i - 1] for i in range(n)])
total -= mat[n//2][n//2] if n % 2 else 0
return total | matrix-diagonal-sum | Python indexing but not memory efficient | russellizadi | 0 | 20 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,164 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2407071/Python-solution-91.11-faster | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
total_sum = 0
for i in range(len(mat)):
total_sum += mat[i][i]
if len(mat) % 2 != 0 and i != len(mat) // 2:
total_sum += mat[i][-1 - i]
if len(mat) % 2 == 0:
total_... | matrix-diagonal-sum | Python solution 91.11% faster | samanehghafouri | 0 | 15 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,165 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2317608/Faster-than-99.41 | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
size = len(mat)
res = sum([(mat[i][i] + mat[size-1-i][size-1-i] +
mat[i][size - 1 - i] + mat[size - 1 - i][i])
for i in range(size//2)])
if size % 2 == 1:
res += mat[size... | matrix-diagonal-sum | Faster than 99.41% | trohin | 0 | 21 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,166 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2315264/Fast-and-Easy-python-3-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
# There're at most two elements that can be counted in the sum per row which is mat[i][i] and mat[i][n - i - 1]
n = len(mat)
res = 0
for i in range(n):
res += mat[i][i]
if i != n - i... | matrix-diagonal-sum | Fast and Easy python 3 solution | Ultraviolet0909 | 0 | 12 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,167 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2136910/Easy-and-simple-Python3-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
ans = 0
j = 0
k = -1
for i in range(len(mat)):
if mat[i][j] == mat[i][k] and len(mat)%2 != 0 and i == (len(mat)-1)//2:
ans += mat[i][j]
j += 1
k -= 1
... | matrix-diagonal-sum | Easy and simple Python3 solution | rohansardar | 0 | 54 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,168 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2058482/Python-Generator-Expression-greater-oneliner | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
# return sum(row[i] + row[len(mat) - 1 - i] if i != len(mat) - 1 - i else row[i] for i, row in enumerate(mat))
return sum(
row[i] + row[len(mat) - 1 - i]
if i != len(mat) - 1 - i
else row[i... | matrix-diagonal-sum | Python Generator Expression > oneliner | kedeman | 0 | 55 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,169 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2053462/Faster-than-94.68-or-Simple-Python-code | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n=len(mat)
x=[]
m=[]
for i in range(n):
x.append(mat[i][i])
x.append(mat[n-1-i][i])
if n%2!=0:
middle=n//2
x.remove(mat[middle][middle])
ret... | matrix-diagonal-sum | Faster than 94.68% | Simple Python code | glimloop | 0 | 37 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,170 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2018943/Python-Simple-O(n)-Solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
sum = 0
matLen = len(mat)
for i, x in enumerate(mat):
if i == matLen-i-1:
sum += x[i]
else:
sum += x[i] + x[matLen-i-1]
return sum | matrix-diagonal-sum | Python Simple O(n) Solution | parksung | 0 | 47 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,171 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/2013945/Python-95-Memory-Solution! | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
visited = []
output = 0
for i in range(len(mat)):
if (i, i) not in visited:
output += mat[i][i]
visited.append((i, i))
y = len(mat) - 1
for x in range(len(mat)):
... | matrix-diagonal-sum | 🔥 Python 95% Memory Solution! 🔥 | PythonerAlex | 0 | 109 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,172 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1996910/Python-Easiest-Soltuion!!-or-Simple-or-Optimized-or-One-loop | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = t = len(mat)
total = 0
for i in range(n):
for j in range(n):
if i == j:
total += mat[i][j]
elif j == t - 1:
total += mat[i][j]
... | matrix-diagonal-sum | [Python] Easiest Soltuion!! | Simple | Optimized | One loop | jamil117 | 0 | 54 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,173 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1989577/python3-solution-with-2-pointers | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
result = 0
d = len(mat)
l, r = 0, d - 1
for row in mat:
result += row[l] + row[r]
l += 1
r -= 1
if d % 2:
m = d//2
result -= mat[m][m]
retur... | matrix-diagonal-sum | python3 solution with 2 pointers | turael | 0 | 35 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,174 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1985300/Python-or-Simple-Loop | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = len(mat)
result = 0
for i in range(n):
result += mat[i][i] + mat[n-1-i][i]
if n % 2 == 1:
result -= mat[n//2][n//2]
return result | matrix-diagonal-sum | Python | Simple Loop | Mikey98 | 0 | 36 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,175 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1979180/Python-Understandable-solution-O(n)-runtime | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int
diagonalsSum = 0
for index, array in enumerate(mat):
if index != len(array) - index - 1:
diagonalsSum += array[index] + array[-1 - index]
else:
diagonalsSum += array[index]
... | matrix-diagonal-sum | [Python] Understandable solution O(n) runtime | romanduenin | 0 | 35 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,176 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1966687/Understandable-solution-Py | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
prim = [(x,x) for x in range(len(mat))]
sec = [(x,y) for x,y in enumerate(range(len(mat)-1,-1,-1))]
ans = 0
for i,j in set(prim+sec):
ans += mat[j][i]
return ans | matrix-diagonal-sum | Understandable solution Py | StikS32 | 0 | 25 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,177 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1956273/Easy-Python-Solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
c = len(mat[0])-1
count_p = 0
count_s = 0
for i in range(len(mat)):
count_p += mat[i][i]
for j in range(len(mat)):
count_s += mat[i][c]
c -= 1
... | matrix-diagonal-sum | Easy Python Solution | itsmeparag14 | 0 | 40 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,178 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1889370/Ez-oror-2-sols-oror-O(n)-and-O(n2) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
r, s = len(mat), 0
for i in range(r):
for j in range(r):
if i==j:
s+=mat[i][j]
break
for i in range(r):
for j in range(r-1, -1, -1):
... | matrix-diagonal-sum | Ez || 2 sols || O(n) and O(n^2) | ashu_py22 | 0 | 25 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,179 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1889370/Ez-oror-2-sols-oror-O(n)-and-O(n2) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
r, s = len(mat), 0
for i in range(r):
for j in range(r):
if i==j:
s+=mat[i][j]+mat[i][r-j-1]
break
if r%2==0:
return s
return s - mat[r/... | matrix-diagonal-sum | Ez || 2 sols || O(n) and O(n^2) | ashu_py22 | 0 | 25 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,180 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1889370/Ez-oror-2-sols-oror-O(n)-and-O(n2) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
r, s = len(mat), 0
for i in range(r):
s+=mat[i][i]+mat[i][r-i-1]
if r%2==0:
return s
return s - mat[r//2][r//2] | matrix-diagonal-sum | Ez || 2 sols || O(n) and O(n^2) | ashu_py22 | 0 | 25 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,181 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1862065/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = len(mat)
mid = 0
odd = False
if n % 2 != 0:
odd = True
mid = n//2 + 1
count = 0
for r in range(0, len(mat)):
count+=mat[r][r]
j = 1
for r in ran... | matrix-diagonal-sum | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 48 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,182 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1854333/Python-easy-beginner's-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
sec_i = 0
sec_j = len(mat) - 1
s = 0
for i in range(len(mat)):
for j in range(len(mat)):
if i == j:
s = s + mat[i][j] + mat[sec_i][sec_j]
sec_i += 1... | matrix-diagonal-sum | Python easy beginner's solution | alishak1999 | 0 | 37 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,183 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1825042/2-Lines-Python-Solution-oror-70-Faster-oror-Memory-less-than-98 | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n=len(mat) ; sm=sum(mat[i][i]+mat[i][n-i-1] for i in range(n))
return sm-mat[n//2][n//2] if n%2==1 else sm | matrix-diagonal-sum | 2-Lines Python Solution || 70% Faster || Memory less than 98% | Taha-C | 0 | 66 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,184 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1814986/Python3-O(n)-solution | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
summ=0
n=len(mat)
r,c=0,0
while r<n and c<n:
summ+=mat[r][c]
if r!=n-c-1:
summ+=mat[r][n-c-1]
r+=1
c+=1
return summ | matrix-diagonal-sum | [Python3] O(n) solution | __PiYush__ | 0 | 41 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,185 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1786511/Python-(On)-simple-solution-easy-understanding | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
count = 0
start,end = 0, len(mat)-1
while start < len(mat):
if start == end:
count += mat[start][end]
else:
count += mat[start][end]
count += mat[start]... | matrix-diagonal-sum | Python, (On) simple solution, easy understanding | Nish786 | 0 | 72 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,186 |
https://leetcode.com/problems/matrix-diagonal-sum/discuss/1657403/Python-O(N)-Time-O(1)-Space-Better-than-83.69-and-99.41 | class Solution:
def diagonalSum(self, mat: List[List[int]]) -> int:
n = len(mat)
diagonal_sum = 0
for i in range(n):
diagonal_sum += mat[i][i]
# Before adding the element for the second diagonal,
# make sure we are not in the middle ... | matrix-diagonal-sum | Python O(N) Time, O(1) Space, Better than 83.69% and 99.41% | bobbyzia123 | 0 | 101 | matrix diagonal sum | 1,572 | 0.798 | Easy | 23,187 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/830700/Python-3-or-Math-(Pass)-Backtracking-(TLE)-or-Explanation | class Solution:
def numWays(self, s: str) -> int:
total = s.count('1')
if total % 3: return 0
n = len(s)
if not total: return (1+n-2) * (n-2) // 2 % 1000000007
avg, ans = total // 3, 0
cnt = first_part_right_zeros = last_part_left_zeros = 0
for i in range(n):
... | number-of-ways-to-split-a-string | Python 3 | Math (Pass), Backtracking (TLE) | Explanation | idontknoooo | 2 | 215 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,188 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/830700/Python-3-or-Math-(Pass)-Backtracking-(TLE)-or-Explanation | class Solution:
def numWays(self, s: str) -> int:
total = s.count('1')
if total % 3: return 0
avg = total // 3
n, ans = len(s), 0
@lru_cache(maxsize=None)
def dfs(s, idx, part):
nonlocal avg, n
if part == 4 and idx == n: return 1
cn... | number-of-ways-to-split-a-string | Python 3 | Math (Pass), Backtracking (TLE) | Explanation | idontknoooo | 2 | 215 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,189 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/2520656/Python-easy-to-read-and-understand-or-math | class Solution:
def numWays(self, s: str) -> int:
cnt, n = 0, len(s)
mod = 10**9 + 7
for i in range(n):
if s[i] == '1':
cnt += 1
if cnt%3 != 0:
return 0
if cnt == 0:
return (n-1)*(n-2)//2 % (mod)
... | number-of-ways-to-split-a-string | Python easy to read and understand | math | sanial2001 | 0 | 117 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,190 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/1657626/Python-O(n)-time-O(n)-space-solution | class Solution:
def numWays(self, s: str) -> int:
n = len(s)
ones = s.count('1')
if ones % 3 != 0:
return 0
if ones == 0:
return (n-1)*(n-2)//2 % (10**9+7)
# now ones % 3 ==0, ones >= 3
k = ones//3
idx... | number-of-ways-to-split-a-string | Python O(n) time, O(n) space solution | byuns9334 | 0 | 412 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,191 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/1358164/Python-Dynamic-Programming-%2B-Memoization | class Solution:
def numWays(self, s: str) -> int:
memo = {}
def backTracking(curr_path, s, count):
if (len(s), count) in memo:
return memo[(len(s), count)]
if not s:
if count == 3:
return 1
return 0... | number-of-ways-to-split-a-string | [Python] Dynamic Programming + Memoization | Sai-Adarsh | 0 | 294 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,192 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/830874/Python3-counting-zeros-between-groups | class Solution:
def numWays(self, s: str) -> int:
n = s.count("1")
if n % 3: return 0 # impossible
if not n: ans = ((len(s)-1)*(len(s)-2)//2) # len(s)-1 choose 2
else:
ans = 1
ones = zeros = 0
for c in s:
if c == "1":
... | number-of-ways-to-split-a-string | [Python3] counting zeros between groups | ye15 | 0 | 69 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,193 |
https://leetcode.com/problems/number-of-ways-to-split-a-string/discuss/830785/Naive-Python-Solution | class Solution:
def numWays(self, s: str) -> int:
n = len(s)
mod = 10**9+7
numOnes = [0]*n
numOnes[0] = 1 if s[0] == '1' else 0
for i, ch in enumerate(s[1:]):
numOnes[i+1] += numOnes[i]
if ch == '1':
numOnes[i+1] += 1
... | number-of-ways-to-split-a-string | Naive Python Solution | leetcodesujitraman | 0 | 60 | number of ways to split a string | 1,573 | 0.325 | Medium | 23,194 |
https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/835866/Python-Solution-Based-on-Binary-Search | class Solution:
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
def lowerbound(left, right, target):
while left < right:
mid = left + (right - left) // 2
if arr[mid] == target:
right = mid
... | shortest-subarray-to-be-removed-to-make-array-sorted | Python Solution Based on Binary Search | pochy | 4 | 694 | shortest subarray to be removed to make array sorted | 1,574 | 0.366 | Medium | 23,195 |
https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/865971/Python-Simple-and-concise-linear-time-Solution-beats-99.2 | class Solution:
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
st = 0
end = len(arr) - 1
prev = None
st = Solution.find_end_subarray(arr=arr, st=0, inc_flag=True)
end = Solution.find_end_subarray(arr=arr, st=len(arr)-1, inc_flag=False)
merge_... | shortest-subarray-to-be-removed-to-make-array-sorted | [Python] Simple and concise linear time Solution beats 99.2 % | vasu6 | 2 | 472 | shortest subarray to be removed to make array sorted | 1,574 | 0.366 | Medium | 23,196 |
https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/2470235/Python3-or-O(n)-Time-complexity-or-Two-pointers | class Solution:
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
# sentinel
arr.append(float("inf"))
arr.insert(0, 0)
left = 0
right = len(arr) - 1
shortest = float("inf")
# find longest ascending array at left side.
while left <... | shortest-subarray-to-be-removed-to-make-array-sorted | Python3 | O(n) Time complexity | Two pointers | shugokra | 1 | 197 | shortest subarray to be removed to make array sorted | 1,574 | 0.366 | Medium | 23,197 |
https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/2846886/Break-the-array-and-binary-search | class Solution:
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
left, right = 0, len(arr) - 1
while left < len(arr) - 1 and arr[left] <= arr[left + 1]:
left += 1
if left == right: return 0
while right > 0 and arr[right] >= arr[right - 1]:
righ... | shortest-subarray-to-be-removed-to-make-array-sorted | Break the array and binary search | michaelniki | 0 | 1 | shortest subarray to be removed to make array sorted | 1,574 | 0.366 | Medium | 23,198 |
https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/discuss/2316103/Python-easy-to-read-and-understand-or-two-pointers | class Solution:
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
n = len(arr)
i = 0
while i < n-1 and arr[i+1] >= arr[i]:
i += 1
if i == n-1:
return 0
j = n-1
while j >= 0 and arr[j-1] <= arr[j]:
j -=... | shortest-subarray-to-be-removed-to-make-array-sorted | Python easy to read and understand | two-pointers | sanial2001 | 0 | 192 | shortest subarray to be removed to make array sorted | 1,574 | 0.366 | Medium | 23,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.