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/remove-letter-to-equalize-frequency/discuss/2661333/Counter-of-frequencies | class Solution:
def equalFrequency(self, word: str) -> bool:
cnt = Counter(Counter(word).values())
if len(cnt) == 1 and (1 in cnt or 1 in cnt.values()):
return True
elif len(cnt) != 2:
return False
lst_keys = list(cnt.keys())
lst_vals = [cnt[k] for k in lst_keys]
if lst_vals[0] == 1 and (lst_keys[0] - lst_keys[1] == 1
or lst_keys[0] == 1):
return True
elif lst_vals[1] == 1 and (lst_keys[1] - lst_keys[0] == 1
or lst_keys[1] == 1):
return True
return False | remove-letter-to-equalize-frequency | Counter of frequencies | EvgenySH | 0 | 55 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,100 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2652730/Python-Very-Simple-easy-to-understand-or-100 | class Solution:
def equalFrequency(self, word: str) -> bool:
for i in range(len(word)):
c = Counter(word[:i]) + Counter(word[i+1:])
if len(set(c.values())) == 1:
return True
return False | remove-letter-to-equalize-frequency | Python Very Simple easy to understand | 100% | pandish | 0 | 76 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,101 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2651956/remove-letter-to-equalize-frequency | class Solution:
def equalFrequency(self, word: str) -> bool:
d=Counter(word)
word=set(word)
word=list(word)
s=set()
print(word)
print(d)
for i in range(len(word)):
d[word[i]]=d[word[i]]-1
for j in range(len(word)):
if d[word[i]]==0:
if i!=j:
p=d[word[j]]
s.add(p)
else:
p=d[word[j]]
s.add(p)
if len(s)==1:
return True
else:
s.clear()
d[word[i]]=d[word[i]]+1
return False | remove-letter-to-equalize-frequency | remove-letter-to-equalize-frequency | meenu155 | 0 | 5 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,102 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2650304/Python-solution%3A-counter-of-counter | class Solution:
def equalFrequency(self, word: str) -> bool:
counter = Counter(word)
counter_counter = Counter(counter.values())
if len(counter_counter) == 1:
if 1 in counter_counter: # e.g., "abc"
return True
else:
for k, v in counter_counter.items():
if v == 1: # e.g., "zz"
return True
return False # e.g., "aabb"
if len(counter_counter) == 2:
max_, min_ = max(counter_counter), min(counter_counter)
if min_ == 1 and counter_counter[1] == 1: # e.g., "abbcc"
return True
if max_ == min_ + 1 and counter_counter[max_] == 1: # e.g., "aaabbcc"
return True
return False | remove-letter-to-equalize-frequency | Python solution: counter of counter | CuriousJianXu | 0 | 18 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,103 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2650253/Python-Solution-100-Faster | class Solution:
def equalFrequency(self, word: str) -> bool:
j = 0
word = list(word)
d = deque(word)
while j < len(word):
item = d.popleft()
c = list(Counter(d).values())
if len(set(c))==1:
return True
d.append(item)
j+=1
return False | remove-letter-to-equalize-frequency | Python Solution 100% Faster | Saqib_skipq_2022 | 0 | 5 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,104 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2649760/Python-3-bruteforce-easy-to-understand | class Solution:
def equalFrequency(self, word: str) -> bool:
dic = {}
r1=0
r2=0
for i in range(len(word)):
if word[i] not in dic.keys():
dic[word[i]] = 1
else:
dic[word[i]] = dic[word[i]]+1
val = dic.values()
freq = list(val)
f2 = list(val)
freq.sort()
f2.sort()
freq[len(freq)-1] = freq[len(freq)-1] -1
f2[0] = f2[0]-1
if f2[0]==0:
f2.pop(0)
if freq[len(freq)-1]==0:
freq.pop()
for i in range(len(f2)-1):
if f2[i]!= f2[i+1]:
r2 = 1
for i in range(len(freq)-1):
if freq[i]!=freq[i+1]:
r1 =1
print(f2,freq)
if r1==0 or r2==0:
return True
return False | remove-letter-to-equalize-frequency | Python 3 bruteforce easy to understand | Ashish_El | 0 | 6 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,105 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2649547/Easy-problem-No-sir!-Solution-faster-than-100-of-Python3-online-submissions-for-this-problem | class Solution:
def equalFrequency(self, word: str) -> bool:
for i in range(0, len(word)):
wordFreq = Counter(word[:i] + word[i + 1:])
d = defaultdict(int)
for V in wordFreq.values():
d[V] += 1
if len(d) == 1:
return True
return False | remove-letter-to-equalize-frequency | Easy problem? No sir! 🔥Solution faster than 100% of Python3 online submissions for this problem🔥 | mediocre-coder | 0 | 35 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,106 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2648286/Python3-O(n)-Solution-that-voids-checking-every-corner-case | class Solution:
def equalFrequency(self, word: str) -> bool:
counter = defaultdict(int)
for char in word:
counter[char] += 1
for key in counter.keys():
counter[key] -= 1
data = [x for x in counter.values() if x != 0 ]
if data.count(data[0]) == len(data): return True
counter[key] += 1
return False | remove-letter-to-equalize-frequency | Python3 O(n) Solution that voids checking every corner case | xxHRxx | 0 | 22 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,107 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2648285/Python-3-or-Remove-each-letter-in-Word-or-O(n) | class Solution:
def equalFrequency(self, word: str) -> bool:
counter = Counter(word)
for let in counter.keys():
copy = Counter(counter)
copy[let] -= 1
if copy[let] == 0: del copy[let]
if len(set(copy.values())) == 1: return True
return False | remove-letter-to-equalize-frequency | Python 3 | Remove each letter in Word | O(n) | leeteatsleep | 0 | 47 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,108 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2648000/Python%3A-100%2B100 | class Solution:
def equalFrequency(self, word: str) -> bool:
F=False
C=list(Counter(word).values())
if len(C)==1: F=True
if len(set(C))==1 and C[0]==1: F=True
if len(set(C))==2:
a=max(C)
if sum(C)==a+(a-1)*(len(C)-1):
F=True
b=min(C)
if sum(C)+1==(b+1)*len(C):
F=True
return F | remove-letter-to-equalize-frequency | Python: 100%+100% | Leox2022 | 0 | 7 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,109 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647818/Python-Time-O(N)-Space-O(1)-Easy-to-understand | class Solution:
def equalFrequency(self, word: str) -> bool:
N = len(word)
uniq_letters = set(word)
# a. Word of same letter: 'aaaaa'
# b. All frequency of letter is 1: 'abcde'
if len(uniq_letters) == 1 or len(uniq_letters) == N:
return True
freq = [0] * 26
for c in word:
freq[ord(c) - ord('a')] += 1
count = Counter(freq)
del count[0]
if len(count) != 2:
return False
k1, k2 = min(count.keys()), max(count.keys())
# c-i
if k1 == count[k1] == 1:
return True
# c-ii
if k2 - k1 == 1 and count[k2] == 1:
return True
return False | remove-letter-to-equalize-frequency | [Python] Time O(N) / Space O(1) Easy to understand | sam8899 | 0 | 137 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,110 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647763/Easy-to-understand-just-using-Counter | class Solution:
def equalFrequency(self, word: str) -> bool:
n=len(word)
for i in range(n):
c=Counter(word)
c[word[i]]-=1
if c[word[i]]==0:
del c[word[i]]
print(c) #use this statement for the better visualisation
if len(set(c.values()))==1:
return True
return False | remove-letter-to-equalize-frequency | Easy to understand just using Counter | gauravtiwari91 | 0 | 33 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,111 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647738/Python-100-beat | class Solution:
def equalFrequency(self, word: str) -> bool:
s = len(set(word))
lengh = len(word)
if (s == lengh):
return True
if ((lengh - 1) % s == 0) or ((lengh) % s == s-1):
return True
return False | remove-letter-to-equalize-frequency | Python 100% beat | 22025004 | 0 | 15 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,112 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647720/Python-or-Counter-of-Counters | class Solution:
def equalFrequency(self, word: str) -> bool:
count = Counter(word)
freq_of_count = Counter(count.values())
n = len(freq_of_count)
if n == 1:
return (1 in freq_of_count.keys()
or 1 in freq_of_count.values())
elif n == 2:
freq_list = list(freq_of_count.items())
freq_list.sort()
if freq_list[0][0] == 1 and freq_list[0][1] == 1:
return True
return ((freq_list[1][0] - freq_list[0][0]) == 1
and freq_list[1][1] == 1)
return False | remove-letter-to-equalize-frequency | Python | Counter of Counters | on_danse_encore_on_rit_encore | 0 | 7 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,113 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647698/Simple-Python-Solution | class Solution:
def equalFrequency(self, word: str) -> bool:
C=Counter(word)
for w in word:
C[w]-=1
arr=[]
for j in C.values():
if j !=0:
arr.append(j)
if len(set(arr))==1:
return True
C[w]+=1
return False | remove-letter-to-equalize-frequency | Simple Python Solution | 131901028 | 0 | 13 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,114 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647277/Python-Numpy-Easy-to-understand | class Solution:
def equalFrequency(self, word: str) -> bool:
import numpy as np
for i in range(len(word)):
s = ""
s = s + word[0:i]+word[i+1:]
a = Counter(s)
b = list(a.values())
if len(np.unique(b))==1:
return True
break
return False | remove-letter-to-equalize-frequency | Python-Numpy Easy to understand | spraj_123 | 0 | 8 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,115 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2647093/Python3-Solution-not-the-prettiest-code-but-still-34ms-beats-100 | class Solution:
def equalFrequency(self, word: str) -> bool:
count = {}
for letter in word:
count[letter] = count.get(letter,0) + 1
list_values = list(count.values())
if len(count) == 1:
return True
if len(set(list_values)) > 2:
return False
if max(count.values()) == min(count.values()):
if max(count.values()) == 1:
return True
else:
return False
if len(set(list_values)) == 1:
return True
if max(count.values()) - min(count.values()) == 1:
if list(count.values()).count(min(count.values())) == 1:
return True
elif list(count.values()).count(max(count.values())) == 1:
return True
else:
return False | remove-letter-to-equalize-frequency | Python3 Solution - not the prettiest code, but still 34ms, beats 100% | sipi09 | 0 | 12 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,116 |
https://leetcode.com/problems/remove-letter-to-equalize-frequency/discuss/2646781/Secret-Python-Answer-Counter-and-Set-Answer-O(N2) | class Solution:
def equalFrequency(self, word: str) -> bool:
c = Counter(word)
m = c.values()
for l in c:
c[l] -= 1
s = set(c.values())
if 0 in s:
s.remove(0)
if len(s) == 1:
return True
c[l] += 1
return False | remove-letter-to-equalize-frequency | [Secret Python Answer🤫🐍👌😍] Counter and Set Answer O(N^2) | xmky | 0 | 56 | remove letter to equalize frequency | 2,423 | 0.193 | Easy | 33,117 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2646655/JavaPython-3-Bit-manipulations-analysis. | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
m, n = map(len, (nums1, nums2))
return (m % 2 * reduce(xor, nums2)) ^ (n % 2 * reduce(xor, nums1)) | bitwise-xor-of-all-pairings | [Java/Python 3] Bit manipulations analysis. | rock | 10 | 221 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,118 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2649369/python3-math-sol-for-reference | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
n1 = len(nums1)%2
n2 = len(nums2)%2
n1xor = 0
n2xor = 0
if n2 == 1:
for n in nums1:
n1xor ^= n
if n1 == 1:
for n in nums2:
n2xor ^= n
return n1xor ^ n2xor | bitwise-xor-of-all-pairings | [python3] math sol for reference | vadhri_venkat | 1 | 13 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,119 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2647402/O(n)-using-checking-odd-and-even-attributes-of-length-array-(Examples) | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
# nums3 = []
# for n1 in nums1:
# for n2 in nums2:
# nums3.append(n1 ^ n2)
# ans1 = 0
# for i in range(len(nums3)):
# ans1 = ans1 ^ nums3[i]
n1, n2 = len(nums1), len(nums2)
ans = 0
if n2%2==1:
for i in range(n1):
ans = ans ^ nums1[i]
if n1%2==1:
for i in range(n2):
ans = ans ^ nums2[i]
# print(ans1, ans)
return ans | bitwise-xor-of-all-pairings | O(n) using checking odd and even attributes of length array (Examples) | dntai | 1 | 15 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,120 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2646616/Simple-Python3-Explained-or-O(n1-%2B-n2) | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
n1, n2 = len(nums1), len(nums2)
res = 0
for num in nums1:
if n2 % 2:
res ^= num
for num in nums2:
if n1 % 2:
res ^= num
return res | bitwise-xor-of-all-pairings | Simple Python3 Explained | O(n1 + n2) | ryangrayson | 1 | 18 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,121 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2845428/python-solution | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
res_a = 0
res_b = 0
for j in nums2:
res_b^=j
for i in nums1:
res_a^=i
if len(nums2) %2 ==0 :
if len(nums1) %2 == 0 :
return 0
return res_b
else :
if len(nums1) %2 == 0 :
return res_a
return res_a ^ res_b | bitwise-xor-of-all-pairings | python solution | Cosmodude | 0 | 1 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,122 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2842133/Python3-Unhole-On-Liner | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
return (reduce(lambda x, y: x^y, nums1) if len(nums2) % 2 else 0)^(reduce(lambda x, y: x^y, nums2) if len(nums1) % 2 else 0) | bitwise-xor-of-all-pairings | [Python3] - Unhole On-Liner | Lucew | 0 | 1 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,123 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2743962/FASTER-THAN-83-PYTHON-3-SULUTIONS | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
ans = 0
if(len(nums1)%2) :
for i in nums2:
ans=ans^i
if(len(nums2)%2) :
for i in nums1:
ans=ans^i
return ans | bitwise-xor-of-all-pairings | FASTER THAN 83% PYTHON 3 SULUTIONS | mishrakripanshu303 | 0 | 2 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,124 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2722297/Simple-python-code-with-explanation | class Solution:
def xorAllNums(self, nums1, nums2):
if (len(nums1)&1) == 0 and (len(nums2)&1) == 0 :
return 0
elif (len(nums1)&1) != 0 and (len(nums2)&1) == 0 :
x = 0
for i in range(len(nums2)):
x = x ^(nums2[i])
return x
elif (len(nums1)&1) == 0 and (len(nums2)&1) != 0 :
x = 0
for i in range(len(nums1)):
x = x ^(nums1[i])
return x
elif (len(nums1)&1) == 1 and (len(nums2)&1) == 1:
x = 0
nums3 = nums1 + nums2
for i in range(len(nums3)):
x = x ^ nums3[i]
return x | bitwise-xor-of-all-pairings | Simple python code with explanation | thomanani | 0 | 2 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,125 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2706008/Python-(Faster-than-94) | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
res = 0
if len(nums1) % 2 != 0:
for n in nums2:
res ^= n
if len(nums2) % 2 != 0:
for n in nums1:
res ^= n
if len(nums1) == len(nums2):
return res
return res | bitwise-xor-of-all-pairings | Python (Faster than 94%) | KevinJM17 | 0 | 5 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,126 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2684438/Python-or-XOR-or-Greedy | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
s=0
for i in nums1:
s^=i
g=0
for i in nums2:
g^=i
# print(g,s)
si=s
gi=g
xe=0
for i in range(len(nums2)):
s=xe^si
xe=s
xe=0
for i in range(len(nums1)):
g=xe^gi
xe=g
# print(g,s)
# print(g)
# print(s,g)
return s^g | bitwise-xor-of-all-pairings | Python | XOR | Greedy | Prithiviraj1927 | 0 | 2 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,127 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2665716/Python3-or-easy-solution | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
if(len(nums1)%2==0 and len(nums2)%2==0):
return 0
elif(len(nums1)%2==0 and len(nums2)%2==1):
ans = 0
for number in nums1:
ans = ans^number
elif(len(nums1)%2==1 and len(nums2)%2==0):
ans = 0
for number in nums2:
ans = ans^number
else:
ans = 0
for number in nums1+nums2:
ans = ans^number
return ans | bitwise-xor-of-all-pairings | Python3 | easy solution | ty2134029 | 0 | 2 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,128 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2658167/Python-Using-algebraic-solver-to-find-fast-5-line-solution | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
match len(nums1) % 2, len(nums2) % 2:
case 0, 0: return 0
case 0, 1: return reduce(xor, nums1)
case 1, 0: return reduce(xor, nums2)
return reduce(xor, nums1) ^ reduce(xor, nums2) | bitwise-xor-of-all-pairings | [Python] Using algebraic solver to find fast 5 line solution 🤔💭🔢✖️🧮 | NotTheSwimmer | 0 | 19 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,129 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2648325/Python3-O(n)-Math-Solution | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
len1, len2 = len(nums1) % 2, len(nums2) % 2
data = [0]*32
for element in nums1:
bin_rep = bin(element)[2:]
start_index = 32 - len(bin_rep)
for t in range(start_index, 32):
data[t] += (int(bin_rep[t - start_index])) * len2
for element in nums2:
bin_rep = bin(element)[2:]
start_index = 32 - len(bin_rep)
for t in range(start_index, 32):
data[t] += (int(bin_rep[t - start_index])) * len1
return int(''.join(['1' if x % 2 == 1 else '0' for x in data]), 2) | bitwise-xor-of-all-pairings | Python3 O(n) Math Solution | xxHRxx | 0 | 18 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,130 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2648175/Python3-or-Explained-or-Easy-to-understand-or-Simple-or-O(n) | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
ret = 0
if len(nums2) % 2 == 1:
for i in nums1:
ret ^= i
if len(nums1) % 2 == 1:
for i in nums2:
ret ^= i
return ret | bitwise-xor-of-all-pairings | Python3 | Explained | Easy-to-understand | Simple | O(n) | DheerajGadwala | 0 | 2 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,131 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2647103/Python-solution | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
c1 = len(nums1) % 2
c2 = len(nums2) % 2
n1 = 0
n2 = 0
if c1:
for i in nums2:
n2 = n2 ^ i
if c2:
for i in nums1:
n1 = n1 ^ i
if c1 and c2:
return n1 ^ n2
elif c1:
return n2
elif c2:
return n1
else:
return 0 | bitwise-xor-of-all-pairings | Python solution | cstoku | 0 | 4 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,132 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2646839/Python3-One-Line-Reduce | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
return reduce(lambda x,y:x^y,len(nums1)%2 * nums2 + len(nums2)%2 * nums1,0) | bitwise-xor-of-all-pairings | Python3, One Line, Reduce | Silvia42 | 0 | 5 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,133 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2646839/Python3-One-Line-Reduce | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
n=len(nums1)
m=len(nums2)
if n%2 and m%2:
q=nums1+nums2
elif n%2:
q=nums2
elif m%2:
q=nums1
else:
return 0
return reduce(lambda x,y:x^y,q[1:],q[0]) | bitwise-xor-of-all-pairings | Python3, One Line, Reduce | Silvia42 | 0 | 5 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,134 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2646632/Tricky-Python-Answer-8-Lines-of-code-O(n) | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
res = 0
for i in nums1:
if len(nums2) %2 == 1:
res ^= i
for j in nums2:
if len(nums1) %2 == 1:
res ^= j
return res | bitwise-xor-of-all-pairings | [Tricky Python Answer🤫🐍👌😍] 8 Lines of code- O(n) | xmky | 0 | 15 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,135 |
https://leetcode.com/problems/bitwise-xor-of-all-pairings/discuss/2647616/Python-XOR-properties-oror-Time%3A-1418-ms-Space%3A-31.4-MB | class Solution:
def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int:
n=len(nums1)
m=len(nums2)
ans=0
if(m%2==0):
if(n%2==0):
return 0
else:
ans=nums2[0]
for i in range(1,m):
ans^=nums2[i]
return ans
else:
if(n%2==0):
ans=nums1[0]
for i in range(1,n):
ans^=nums1[i]
return ans
else:
ans=nums1[0]
for i in range(1,n):
ans^=nums1[i]
for i in range(m):
ans^=nums2[i]
return ans | bitwise-xor-of-all-pairings | Python XOR properties || Time: 1418 ms , Space: 31.4 MB | koder_786 | -1 | 11 | bitwise xor of all pairings | 2,425 | 0.586 | Medium | 33,136 |
https://leetcode.com/problems/number-of-pairs-satisfying-inequality/discuss/2647569/Python-Binary-Search-oror-Time%3A-2698-ms-(50)-Space%3A-32.5-MB-(100) | class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
n=len(nums1)
for i in range(n):
nums1[i]=nums1[i]-nums2[i]
ans=0
arr=[]
for i in range(n):
x=bisect_right(arr,nums1[i]+diff)
ans+=x
insort(arr,nums1[i])
return ans | number-of-pairs-satisfying-inequality | Python Binary Search || Time: 2698 ms (50%), Space: 32.5 MB (100%) | koder_786 | 0 | 15 | number of pairs satisfying inequality | 2,426 | 0.422 | Hard | 33,137 |
https://leetcode.com/problems/number-of-pairs-satisfying-inequality/discuss/2647262/Python-3Binary-search | class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
q = []
n = len(nums1)
ans = 0
for i in reversed(range(n)):
cur = nums1[i] - nums2[i] - diff
loc = bisect.bisect_left(q, cur)
ans += len(q) - loc
bisect.insort(q, cur + diff)
return ans | number-of-pairs-satisfying-inequality | [Python 3]Binary search | chestnut890123 | 0 | 62 | number of pairs satisfying inequality | 2,426 | 0.422 | Hard | 33,138 |
https://leetcode.com/problems/number-of-pairs-satisfying-inequality/discuss/2646882/Python-Binary-indexed-tree-O(nlogn) | class Solution:
def numberOfPairs(self, a: List[int], b: List[int], diff: int) -> int:
l = len(a)
c = [0] * l
for i in range(l):
c[i] = a[i] - b[i]
minc = min(c)
if minc <= 0:
for i in range(l):
c[i] = c[i] - minc + 1 # shift up to make sure all numbers are positive
BIG = max(c)+abs(diff)+1
t = [0] * (BIG+1)
def update(c):
while c <= BIG:
t[c] += 1
c += c & -c
def count(c):
r = 0
while c > 0:
r += t[c]
c -= c & -c
return r
r = 0
for i in range(l):
r += count(c[i]+diff)
update(c[i])
return r
``` | number-of-pairs-satisfying-inequality | [Python] Binary indexed tree O(nlogn) | hieuvpm | 0 | 35 | number of pairs satisfying inequality | 2,426 | 0.422 | Hard | 33,139 |
https://leetcode.com/problems/number-of-pairs-satisfying-inequality/discuss/2646806/python-simple-solution-with-binary-search-(2434-ms) | class Solution:
def numberOfPairs(self, nums1: List[int], nums2: List[int], diff: int) -> int:
ans = 0
n = len(nums1)
nums = [nums1[i] - nums2[i] for i in range(n)]
vis = []
for i in range(n - 1, -1, -1):
if not vis:
vis.append(nums[i] + diff)
else:
pos = bisect.bisect_left(vis, nums[i])
ans += len(vis) - pos
bisect.insort(vis, nums[i] + diff)
return ans | number-of-pairs-satisfying-inequality | python simple solution with binary search (2434 ms) | EthanYangCX | 0 | 21 | number of pairs satisfying inequality | 2,426 | 0.422 | Hard | 33,140 |
https://leetcode.com/problems/number-of-common-factors/discuss/2817862/Easy-Python-Solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
c=0
mi=min(a,b)
for i in range(1,mi+1):
if a%i==0 and b%i==0:
c+=1
return c | number-of-common-factors | Easy Python Solution | Vistrit | 3 | 62 | number of common factors | 2,427 | 0.801 | Easy | 33,141 |
https://leetcode.com/problems/number-of-common-factors/discuss/2683395/PYTHON-TRASH-SOLUTION-or-CLICK-IF-YOU-WANT-TO-LOWER-YOUR-IQ | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
c, d = max(a,b), min(a,b)
for x in range(1,c):
if c % x == 0:
if d % x == 0:
count += 1
if a == b:
count += 1
return count | number-of-common-factors | [PYTHON] TRASH SOLUTION | CLICK IF YOU WANT TO LOWER YOUR IQ | omkarxpatel | 3 | 65 | number of common factors | 2,427 | 0.801 | Easy | 33,142 |
https://leetcode.com/problems/number-of-common-factors/discuss/2651457/Python-JS-one-liners | class Solution:
def commonFactors(self, a: int, b: int) -> int:
return sum(a % n == 0 and b % n == 0 for n in range(1, min(a, b) + 1)) | number-of-common-factors | Python / JS one-liners | SmittyWerbenjagermanjensen | 2 | 69 | number of common factors | 2,427 | 0.801 | Easy | 33,143 |
https://leetcode.com/problems/number-of-common-factors/discuss/2648886/Python3-brute-force | class Solution:
def commonFactors(self, a: int, b: int) -> int:
ans = 0
for x in range(1, min(a, b)+1):
if a % x == b % x == 0: ans += 1
return ans | number-of-common-factors | [Python3] brute-force | ye15 | 2 | 27 | number of common factors | 2,427 | 0.801 | Easy | 33,144 |
https://leetcode.com/problems/number-of-common-factors/discuss/2759289/Python-or-Easy | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
temp = min(a,b)
for x in range(1,temp + 1):
if(a % x == 0 and b % x == 0):
count += 1
return count | number-of-common-factors | Python | Easy | LittleMonster23 | 1 | 7 | number of common factors | 2,427 | 0.801 | Easy | 33,145 |
https://leetcode.com/problems/number-of-common-factors/discuss/2759289/Python-or-Easy | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
temp = gcd(a,b)
for x in range(1,temp + 1):
if(a % x == 0 and b % x == 0):
count += 1
return count | number-of-common-factors | Python | Easy | LittleMonster23 | 1 | 7 | number of common factors | 2,427 | 0.801 | Easy | 33,146 |
https://leetcode.com/problems/number-of-common-factors/discuss/2656870/Python-100-run-time | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
if max(a,b) % min(a,b) == 0:
count += 1
for i in range(1,(min(a,b)//2) + 1):
if a % i == 0 and b % i == 0:
count += 1
return count | number-of-common-factors | Python 100% run time | aruj900 | 1 | 74 | number of common factors | 2,427 | 0.801 | Easy | 33,147 |
https://leetcode.com/problems/number-of-common-factors/discuss/2651164/Python-one-liner | class Solution:
def commonFactors(self, a: int, b: int) -> int:
return sum(a % x == b % x == 0 for x in range(1, min(a, b) + 1)) | number-of-common-factors | Python, one liner | blue_sky5 | 1 | 35 | number of common factors | 2,427 | 0.801 | Easy | 33,148 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649240/Python3-Math-solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
def factors(n):
return set(factor for i in range(1, int(n ** 0.5) + 1) if n % i == 0 \
for factor in (i, n / i))
return len(set(factors(a) & factors(b))) | number-of-common-factors | Python3 Math solution | frolovdmn | 1 | 40 | number of common factors | 2,427 | 0.801 | Easy | 33,149 |
https://leetcode.com/problems/number-of-common-factors/discuss/2823176/EASY-TO-UNDERSTAND-SOLUTION-step-by-step-explanation-for-beginners | class Solution:
def commonFactors(self, a: int, b: int) -> int:
t = 0 # define or initialize variable 't' as integer
s = min(a,b) # picks the minimum value in a and b, to execute the condition below that many times
for i in range(1,s+1): #start with 1, nothing is divisible by 0
if (a%i == 0) & (b%i == 0): #this IF condition loop gets executed only when the boolean logic is True, i.e. where int(a) is the minimum input value and any int(i) in the range(1,a+1) is perfect common factor for both int(a) & int(b)
t+=1 #initiates counter and adds1 everytime this if condition is true
return t | number-of-common-factors | EASY TO UNDERSTAND SOLUTION - step by step explanation for beginners | jaiswaldevansh27 | 0 | 5 | number of common factors | 2,427 | 0.801 | Easy | 33,150 |
https://leetcode.com/problems/number-of-common-factors/discuss/2820036/Python-Easy-Solution-(Brute-Force) | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
mini = min(a,b)
for i in range(1, mini+1):
if a % i == 0 and b % i == 0:
count = count + 1
return count | number-of-common-factors | Python Easy Solution (Brute Force) | Asmogaur | 0 | 2 | number of common factors | 2,427 | 0.801 | Easy | 33,151 |
https://leetcode.com/problems/number-of-common-factors/discuss/2801976/Python | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count=0
for i in range(1,min(a,b)+1):
if a%i==0 and b%i==0:
count+=1
return count | number-of-common-factors | Python | user1633vy | 0 | 2 | number of common factors | 2,427 | 0.801 | Easy | 33,152 |
https://leetcode.com/problems/number-of-common-factors/discuss/2790477/Python3-Solution-one-liner | class Solution:
def commonFactors(self, a: int, b: int) -> int:
return sum([(a % i == 0 and b % i == 0) for i in range(1,max(a,b)+1)]) | number-of-common-factors | Python3 Solution one-liner | sipi09 | 0 | 1 | number of common factors | 2,427 | 0.801 | Easy | 33,153 |
https://leetcode.com/problems/number-of-common-factors/discuss/2784727/gcd | class Solution:
def commonFactors(self, a: int, b: int) -> int:
g, res = gcd(a, b), 0
for i in range(1, g + 1):
if g % i == 0:
res += 1
return res | number-of-common-factors | gcd | JasonDecode | 0 | 2 | number of common factors | 2,427 | 0.801 | Easy | 33,154 |
https://leetcode.com/problems/number-of-common-factors/discuss/2764886/Python-solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count_common_factors = 0
for n in range(1, min(a, b) + 1):
if a % n == 0 and b % n == 0:
count_common_factors += 1
return count_common_factors | number-of-common-factors | Python solution | samanehghafouri | 0 | 5 | number of common factors | 2,427 | 0.801 | Easy | 33,155 |
https://leetcode.com/problems/number-of-common-factors/discuss/2755591/Python-solution-2-lines | class Solution:
def commonFactors(self, a: int, b: int) -> int:
#Sets of commaon factors
common_factors_a_set,common_factors_b_set = set(i for i in range(1,a+1) if a%i==0),set(i for i in range(1,b+1) if b%i==0)
#Intersection of the two sets
return len(common_factors_a_set.intersection(common_factors_b_set)) | number-of-common-factors | Python solution, 2 lines | nathanel80 | 0 | 4 | number of common factors | 2,427 | 0.801 | Easy | 33,156 |
https://leetcode.com/problems/number-of-common-factors/discuss/2750994/Python-Solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
common_1 = []
common_2 = []
for i in range(1,a+1):
if a % i == 0:
common_1.append(i)
for i in range(1,b+1):
if b % i == 0:
common_2.append(i)
result = [i for i in common_1 if i in common_2]
return len(result) | number-of-common-factors | Python Solution | danishs | 0 | 5 | number of common factors | 2,427 | 0.801 | Easy | 33,157 |
https://leetcode.com/problems/number-of-common-factors/discuss/2750813/Python3-O(sqrt(min(ab)))-GCD | class Solution:
def commonFactors(self, a: int, b: int) -> int:
ans = 0
small, big = min(a,b), max(a,b)
c = min(int(sqrt(a)), int(sqrt(b)))
for d in range(1, c + 1):
q1 = small // d
ans += (small % d == 0 and big % d == 0) + ((not d == q1) and small % q1 == 0 and big % q1 == 0)
return ans | number-of-common-factors | [Python3] O(sqrt(min(a,b))) GCD | DG_stamper | 0 | 1 | number of common factors | 2,427 | 0.801 | Easy | 33,158 |
https://leetcode.com/problems/number-of-common-factors/discuss/2706876/Python-easy-solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count=0
for i in range(1,a+1):
if a%i==0 and b%i==0:
count+=1
return count | number-of-common-factors | Python easy solution | AviSrivastava | 0 | 2 | number of common factors | 2,427 | 0.801 | Easy | 33,159 |
https://leetcode.com/problems/number-of-common-factors/discuss/2694282/Fast-Run-Time-Solution.-Easy | class Solution:
def commonFactors(self, a: int, b: int) -> int:
res = []
tempt = 1
for i in range(1,1000):
if a%i == 0:
if b%i == 0:
res.append(i)
if a == b == 1000:
res.append(tempt)
return len(res) | number-of-common-factors | Fast Run-Time Solution. Easy | zombieattak2009 | 0 | 3 | number of common factors | 2,427 | 0.801 | Easy | 33,160 |
https://leetcode.com/problems/number-of-common-factors/discuss/2686936/very-obvious-solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
f1 = self.factors(a)
f2 = self.factors(b)
result = [i for i in f1 if i in f2]
return len(result)
def factors(self,n):
i =1
res1 = []
while i<= n//2:
if (n%i == 0):
res1.append(i)
i += 1
res1.append(n)
return res1 | number-of-common-factors | very obvious solution | sahilchoudhary | 0 | 6 | number of common factors | 2,427 | 0.801 | Easy | 33,161 |
https://leetcode.com/problems/number-of-common-factors/discuss/2668890/Loop-until-half-min(a-b)-89-speed | class Solution:
def commonFactors(self, a: int, b: int) -> int:
return (sum(not a % i and not b % i
for i in range(2, min(a, b) // 2 + 1))
+ 1 + (int((not a % b) if a >= b else (not b % a))
if min(a, b) > 1 else 0)) | number-of-common-factors | Loop until half min(a, b), 89% speed | EvgenySH | 0 | 17 | number of common factors | 2,427 | 0.801 | Easy | 33,162 |
https://leetcode.com/problems/number-of-common-factors/discuss/2659561/Python-Easy-Solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
counter=0
num=[]
for i in range(1,max(a,b)+1):
if a%i==0 and b%i==0:
num.append(i)
counter+=1
print(num)
return counter | number-of-common-factors | Python Easy Solution | prashantdahiya711 | 0 | 8 | number of common factors | 2,427 | 0.801 | Easy | 33,163 |
https://leetcode.com/problems/number-of-common-factors/discuss/2659207/Simple-Python-Code. | class Solution:
def commonFactors(self, a: int, b: int) -> int:
c=0
for i in range(1,min(a,b)+1):
if a%i==0 and b%i==0:
c+=1
return c | number-of-common-factors | Simple Python Code. | isimran18 | 0 | 4 | number of common factors | 2,427 | 0.801 | Easy | 33,164 |
https://leetcode.com/problems/number-of-common-factors/discuss/2652299/Python-easy-solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
ans = 0
for i in range(1, min(a,b)+1):
if a%i == 0 and b%i == 0:
ans += 1
return ans | number-of-common-factors | Python easy solution | StikS32 | 0 | 11 | number of common factors | 2,427 | 0.801 | Easy | 33,165 |
https://leetcode.com/problems/number-of-common-factors/discuss/2651235/Why-be-simple-when-you-can-be-Frustrating-%3A | class Solution:
def commonFactors(self, a: int, b: int) -> int:
c = 0
for i in range(1,min(a,b)+1):
if not ((a / i) - (a//i) ):
if not ((b / i) - (b//i) ):
c += 1
return c | number-of-common-factors | Why be simple when you can be Frustrating :} | hk_davy | 0 | 7 | number of common factors | 2,427 | 0.801 | Easy | 33,166 |
https://leetcode.com/problems/number-of-common-factors/discuss/2650950/Python-Simple-Faster-than-100 | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
x = min(a,b)
for i in range(1, x+1, 1):
if a % i == 0 and b % i == 0:
count += 1
return count | number-of-common-factors | Python Simple Faster than 100% | theReal007 | 0 | 22 | number of common factors | 2,427 | 0.801 | Easy | 33,167 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649898/Python-or-Greatest-Common-Divisor | class Solution:
def commonFactors(self, a: int, b: int) -> int:
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
d = gcd(a, b)
# If they are relatively prime, there is only one common factor (i.e., 1).
if d == 1:
return 1
# Otherwise, there are at least two divisors (i.e., itself and 1).
divisors = 2
root_d = int(sqrt(d))
perfect_square = root_d * root_d == d
for i in range (2, root_d):
if d % i == 0:
divisors += 2
if d % root_d == 0 and root_d != 1:
divisors += 1 if perfect_square else 2
return divisors | number-of-common-factors | Python | Greatest Common Divisor | on_danse_encore_on_rit_encore | 0 | 5 | number of common factors | 2,427 | 0.801 | Easy | 33,168 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649850/Simple-solution-or-Easy-understanding | class Solution:
def commonFactors(self, a: int, b: int) -> int:
temp=min(a,b)
res=1
for i in range(2,temp+1):
if a%i==0 and b%i==0:
res+=1
return res | number-of-common-factors | Simple solution | Easy understanding | sundram_somnath | 0 | 16 | number of common factors | 2,427 | 0.801 | Easy | 33,169 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649836/Number-of-Common-Factors-oror-Python3 | class Solution:
def commonFactors(self, a: int, b: int) -> int:
c=0
m=min(a,b)
for i in range(1,m+1):
if a%i==0 and b%i==0:
c+=1
return c | number-of-common-factors | Number of Common Factors || Python3 | shagun_pandey | 0 | 5 | number of common factors | 2,427 | 0.801 | Easy | 33,170 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649754/Python3-One-Liner-With-List-Comprehension | class Solution:
def commonFactors(self, a: int, b: int) -> int:
return sum([1 for i in range(1, min(a,b)+1) if a%i==0 and b%i==0]) | number-of-common-factors | Python3 One Liner With List Comprehension | godshiva | 0 | 4 | number of common factors | 2,427 | 0.801 | Easy | 33,171 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649732/Numbers-Of-Common-Factor | class Solution:
def commonFactors(self, a: int, b: int) -> int:
count = 0
for i in range(1,min(a,b)+1):
if a % i == b % i == 0:
count += 1
return count | number-of-common-factors | Numbers Of Common Factor | jashii96 | 0 | 4 | number of common factors | 2,427 | 0.801 | Easy | 33,172 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649664/Python%2BSymPy | class Solution:
def commonFactors(self, a: int, b: int) -> int:
from sympy import divisors
min,max=min(a,b),max(a,b)
k=0
for div in divisors(min):
if max%div==0:
k=k+1
return k | number-of-common-factors | Python+SymPy | Leox2022 | 0 | 3 | number of common factors | 2,427 | 0.801 | Easy | 33,173 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649579/Easy-Solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
factors = 0
for i in range(1, min(a, b) + 1):
if a % i == 0 and b % i == 0:
factors += 1
return factors | number-of-common-factors | Easy Solution | mediocre-coder | 0 | 9 | number of common factors | 2,427 | 0.801 | Easy | 33,174 |
https://leetcode.com/problems/number-of-common-factors/discuss/2649112/Python-Answer-Simple-O(n)-modulo-answer | class Solution:
def commonFactors(self, a: int, b: int) -> int:
if a > b:
a,b = b,a
res = 0
for i in range(1,a+1):
if ( b / i ) % 1 == 0 and (a/i) % 1 == 0:
res +=1
return res | number-of-common-factors | [Python Answer🤫🐍🐍🐍] Simple O(n) modulo answer | xmky | 0 | 23 | number of common factors | 2,427 | 0.801 | Easy | 33,175 |
https://leetcode.com/problems/number-of-common-factors/discuss/2648751/Easy-Python-Solution | class Solution:
def commonFactors(self, a: int, b: int) -> int:
c = 0
for i in range(1,min(a,b) + 1):
if a%i == 0 and b%i == 0:
c += 1
return c | number-of-common-factors | Easy Python Solution | a_dityamishra | 0 | 32 | number of common factors | 2,427 | 0.801 | Easy | 33,176 |
https://leetcode.com/problems/number-of-common-factors/discuss/2648687/Math%3A-Common-factors-are-factors-of-the-GCD | class Solution:
def commonFactors(self, a: int, b: int) -> int:
GCD = gcd(a, b)
commonFactors = 1
for i in range(2, floor(GCD) + 1):
if GCD % i == 0:
commonFactors += 1
return commonFactors | number-of-common-factors | Math: Common factors are factors of the GCD | sr_vrd | 0 | 24 | number of common factors | 2,427 | 0.801 | Easy | 33,177 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2677741/Simple-Python-Solution-oror-O(MxN)-beats-83.88 | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
res=0
cur=0
for i in range(len(grid)-2):
for j in range(1,len(grid[0])-1):
cur=sum(grid[i][j-1:j+2]) +grid[i+1][j] + sum(grid[i+2][j-1:j+2])
res = max(res,cur)
return res | maximum-sum-of-an-hourglass | Simple Python Solution || O(MxN) beats 83.88% | Graviel77 | 1 | 42 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,178 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2655852/Python-Solution-with-2D-prefix-sum | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
# ps is our 2D array to store prefix sum
ps = [[0] * (n + 1) for _ in range(m + 1)]
ps[1][1] = grid[0][0]
# prepare prefix sum
for i in range(2, n + 1):
ps[1][i] = grid[0][i - 1] + ps[1][i - 1]
for i in range(2, m + 1):
ps[i][1] = grid[i - 1][0] + ps[i - 1][1]
for i in range(2, m + 1):
for j in range(2, n + 1):
ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + grid[i - 1][j - 1]
# go through each square of (9 cells)
res = 0
for i in range(3, m + 1):
for j in range(3, n + 1):
curr = ps[i][j] - grid[i - 2][j - 3] - grid[i - 2][j - 1]
curr = curr - ps[i][j - 3] - ps[i - 3][j] + ps[i - 3][j - 3]
res = max(res, curr)
# print(ps[i][j])
return res | maximum-sum-of-an-hourglass | [Python] Solution with 2D prefix sum | eaglediao | 1 | 13 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,179 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2649227/O(n2)-using-sliding-window-for-finding-houg-glass-matrix-3x3 | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m-2):
for j in range(n-2):
vsum = 0
for k in range(3):
for t in range(3):
# print(grid[i+k][j+t], end = " ")
if k==0 or k==2 or (k==1 and t==1):
vsum = vsum + grid[i+k][j+t]
# print()
ans = max(ans, vsum)
# print("--")
# print("=" * 20)
return ans | maximum-sum-of-an-hourglass | O(n^2) using sliding window for finding houg-glass matrix 3x3 | dntai | 1 | 35 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,180 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2648937/Python-EXPLAINED | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
max_sum = float("-inf")
for row in range(rows-3+1): # +1 step for edge case
for col in range(1, cols-2+1): # +1 step for edge case
if row+2 <= rows and col+1 <= cols:
ur = grid[row][col-1] + grid[row][col] + grid[row][col+1] #upper row
mc = grid[row+1][col] #mid column
lr = grid[row+2][col-1] + grid[row+2][col] + grid[row+2][col+1] #lower row
curr_sum = ur+mc+lr
max_sum = max(max_sum, curr_sum)
return max_sum | maximum-sum-of-an-hourglass | Python [EXPLAINED] | diwakar_4 | 1 | 28 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,181 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2822574/Python-3-Iterative-approach | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
mx = 0
i = 1
j = 1
for i in range(1,len(grid)-1):
for j in range(1,len(grid[0])-1):
s = grid[i-1][j-1] + grid[i-1][j] + grid[i-1][j+1] + grid[i][j] + grid[i+1][j-1] + grid[i+1][j] + grid[i+1][j+1]
mx = max(mx,s)
return mx | maximum-sum-of-an-hourglass | Python 3 - Iterative approach | 0xack13 | 0 | 2 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,182 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2796517/Efficient-and-Simple-Python-Solution-oror-Easy-Understanding | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
def traverse(grid, i, j, n, m):
summ = 0
summ += (grid[i][j] + grid[i][j+1] + grid[i][j+2]); #######
summ += grid[i+1][j+1]; #
summ += (grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2]); #######
return summ;
n = len(grid); m = len(grid[0]); maxsum = 0;
for i in range(n-2):
for j in range(m-2):
maxsum = max(maxsum, traverse(grid, i, j, n, m));
return maxsum | maximum-sum-of-an-hourglass | Efficient and Simple Python Solution || Easy Understanding | avinashdoddi2001 | 0 | 2 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,183 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2771572/Python-Easy-and-Intuitive-Solution | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
indices = [[0, 0], [0, 1], [0, 2], [1, 1], [2, 0], [2, 1], [2, 2]] #Hourglass indices
res = 0
for i in range(m - 2):
for j in range(n - 2):
curr = sum(grid[i + x][j + y] for x, y in indices)
res = max(curr, res)
return res | maximum-sum-of-an-hourglass | Python Easy & Intuitive Solution | MaverickEyedea | 0 | 4 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,184 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2768151/Precompute-triples-in-rows-85-speed | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
rows_2, cols_2 = rows - 2, cols - 2
triples = [[sum(grid[r][c: c + 3]) for c in range(cols_2)]
for r in range(rows)]
return max(triples[r][c] + triples[r + 2][c] + grid[r + 1][c + 1]
for r in range(rows_2) for c in range(cols_2)) | maximum-sum-of-an-hourglass | Precompute triples in rows, 85% speed | EvgenySH | 0 | 3 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,185 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2700737/Best-and-Easy-Solution-for-Understanding-Python-3 | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
n = len(grid); m = len(grid[0])
result = 0
for i in range(n-2):
for j in range(m-2):
upperThree = 0; lowerThree = 0; pivot=0
upperThree = sum(grid[i][j:j+3])
lowerThree = sum(grid[i+2][j:j+3])
pivot = grid[i+1][j+1]
curr = upperThree + lowerThree + pivot
result = max(curr, result)
return result | maximum-sum-of-an-hourglass | Best and Easy Solution for Understanding Python 3 | deepcoder1 | 0 | 6 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,186 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2688061/Python-solution-with-explanation | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
res = 0
N = len(grid)
for idx,row in enumerate(grid):
#check wheather it has 3 rows starting from the current idx
#if total length - the current_idx if it's less than 2 that means there are 2 rows that we can do the math on
if N - (idx + 1) < 2:
break
#calc the 1 and 3 upto 3 element
#eg: [6,2,1,3] 1. [6,2,1] -> 2. [2,1,3]
# [4,2,1,5] mid: [4,{2},5] -> mid: [2,{1},5]
#eg: [9,2,8,7] 1. [9,2,8] -> 2. [2,8,7]
l = 0
csums = 0
for r in range(len(row)):
if (r - l + 1) > 3:
csums -= grid[idx+2][l]
csums -= grid[idx][l]
l += 1
csums += grid[idx+2][r]
csums += grid[idx][r]
if (r - l + 1) == 3:
mid = grid[idx+1][l:r+1][1]
res = max(res, csums+mid)
print(res)
return res | maximum-sum-of-an-hourglass | Python solution with explanation | pandish | 0 | 9 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,187 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2671463/Python-3-oror-easy-to-understand | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
n, m = len(grid),len(grid[0])
hourglass = [[-1,-1],[-1,0],[-1,1],[0,0],[1,-1],[1,0],[1,1]]
result = 0
for i in range(1,n-1):
for j in range(1,m-1):
sum_local = 0
for h in hourglass:
sum_local += grid[i+h[0]][j+h[1]]
result = max(result, sum_local)
return result | maximum-sum-of-an-hourglass | Python 3 || easy to understand | KateIV | 0 | 20 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,188 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2653021/Python-Solution | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
def hourGlassSum(grid,i,j):
total = 0
for x in range(i,i+3):
for y in range(j,j+3):
if (x == i + 1 and y == j) or (x == i + 1 and y == j + 2):
continue
total += grid[x][y]
return total
result = 0
for i in range(len(grid)-2):
for j in range(len(grid[0])-2):
result = max(result,hourGlassSum(grid,i,j))
return result | maximum-sum-of-an-hourglass | Python Solution | parthberk | 0 | 9 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,189 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2652208/simple-python-solution | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
ans=0
m,n=len(grid),len(grid[0])
for i in range(m-2):
for j in range(n-2):
temp=grid[i][j+0]+grid[i][j+1]+grid[i][j+2]+grid[i+1][j+1]+grid[i+2][j+0]+grid[i+2][j+1]+grid[i+2][j+2]
ans=max(ans,temp)
return ans | maximum-sum-of-an-hourglass | simple python solution | 131901028 | 0 | 12 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,190 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2651591/Python-I-traversal | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
ans = 0
for i in range(n-2):
for j in range(m-2):
ans = max(ans,
grid[i][j]+grid[i][j+1]+grid[i][j+2]
+grid[i+1][j+1]+
grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]
)
return ans | maximum-sum-of-an-hourglass | Python - I traversal | chandu71202 | 0 | 24 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,191 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2651544/Python-3-lines | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
R, C = len(grid), len(grid[0])
def getHourglassSum(r, c):
return sum(grid[r][c:c+3]) + grid[r+1][c+1] + sum(grid[r+2][c:c+3])
return max(getHourglassSum(r, c) for r in range(R-2) for c in range(C-2)) | maximum-sum-of-an-hourglass | Python 3 lines | SmittyWerbenjagermanjensen | 0 | 32 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,192 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2651544/Python-3-lines | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
R, C = len(grid), len(grid[0])
getHourglassSum = lambda r, c: sum(grid[r][c:c+3]) + grid[r+1][c+1] + sum(grid[r+2][c:c+3])
return max(getHourglassSum(r, c) for r in range(R-2) for c in range(C-2)) | maximum-sum-of-an-hourglass | Python 3 lines | SmittyWerbenjagermanjensen | 0 | 32 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,193 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2651093/Python%2BNumPy | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
import numpy as np
grid=np.array(grid)
m,n=grid.shape
pattern=np.array([[1,1,1],[0,1,0],[1,1,1]])
return np.max([np.sum(grid[i-1:i+2,j-1:j+2]*pattern) for i in range(1,m-1) for j in range(1,n-1)]) | maximum-sum-of-an-hourglass | Python+NumPy | Leox2022 | 0 | 4 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,194 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2650995/Python-or-Straight-or-Easy-or-Noob | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
ans = 0
for i in range(len(grid)-2):
for j in range(len(grid[0])-2):
m = grid[i][j]+ grid[i][j+1]+ grid[i][j+2]+grid[i+1][j+1]+grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]
ans = max(m,ans)
return ans | maximum-sum-of-an-hourglass | Python | Straight | Easy | Noob | Brillianttyagi | 0 | 33 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,195 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2650413/beginners-friendly | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
r, c = len(grid), len(grid[0])
a = 0
for i in range(1, r-1):
for j in range(1, c-1):
a = max(a, grid[i][j]+grid[i-1][j-1]+grid[i-1][j]+grid[i-1][j+1]+grid[i+1][j-1]+grid[i+1][j]+grid[i+1][j+1])
return a | maximum-sum-of-an-hourglass | ⬆️ ✅ beginners friendly | ManojKumarPatnaik | 0 | 5 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,196 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2650283/Python-Very-Easy-Brute-Force-Solution | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
def getHourGlassSum(i, j):
res = grid[i+1][j+1]
for k in range(j, j+3):
res += grid[i][k]
res += grid[i+2][k]
return res
res = 0
for i in range(len(grid)-2):
for j in range(len(grid[0])-2):
res = max(res, getHourGlassSum(i, j))
return res | maximum-sum-of-an-hourglass | [Python] Very Easy Brute Force Solution | Saksham003 | 0 | 22 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,197 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2649834/Simple-Solution-or-Python | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
row=len(grid)
col=len(grid[0])
res=0
for i in range(row-2):
for j in range(col-2):
temp=grid[i][j]+grid[i][j+1]+grid[i][j+2]+grid[i+1][j+1]+grid[i+2][j]+grid[i+2][j+1]+grid[i+2][j+2]
res=max(temp,res)
return res | maximum-sum-of-an-hourglass | Simple Solution | Python | sundram_somnath | 0 | 18 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,198 |
https://leetcode.com/problems/maximum-sum-of-an-hourglass/discuss/2649103/Python-Answer-Douple-Loop-O(m*n) | class Solution:
def maxSum(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
ma = 0
def total(i,j):
dirs = [[0,0],[1,0],[-1,0],[-1,-1],[1,1],[-1,1],[1,-1]]
t = 0
for d in dirs:
t += grid[i+d[0]][j+d[1]]
return t
for i in range(1,m-1):
for j in range(1,n-1):
ma = max(ma, total(i,j))
return ma | maximum-sum-of-an-hourglass | [Python Answer🤫🐍🐍🐍] Douple Loop O(m*n) | xmky | 0 | 18 | maximum sum of an hourglass | 2,428 | 0.739 | Medium | 33,199 |
Subsets and Splits