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/count-operations-to-obtain-zero/discuss/1911332/Python3-Math-Solution-Faster-Than-99.35-With-Explanation
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
# this important here in case one of them equal 0 or both equal 0
# in this case we will not enter while loop
cnt = 0
while min(num1, num2) > 0:
if num1 >= num2:
cnt += num1 // num2
num1 = num1 % num2
else:
cnt += num2 // num1
num2 = num2 % num1
return cnt
|
count-operations-to-obtain-zero
|
Python3, Math Solution Faster Than 99.35% With Explanation
|
Hejita
| 1
| 44
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,100
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1771675/Python-Using-while-loop
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count = 0
while num1 != 0 and num2 != 0:
if num1 >= num2:
num1 = num1 - num2
else:
num2 = num2 - num1
count += 1
return count
|
count-operations-to-obtain-zero
|
[Python] Using while loop
|
tejeshreddy111
| 1
| 45
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,101
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2847910/Python-solution
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count = 0
for i in range(max(num1, num2)):
if num1 == 0 or num2 == 0:
return count
if num1 >= num2:
num1 -= num2
count += 1
else:
num2 -= num1
count += 1
return count
|
count-operations-to-obtain-zero
|
Python solution
|
samanehghafouri
| 0
| 1
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,102
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2778178/Python3-Explained-Switching-Divmod-(Euclidean-Algorithm)
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
# sort the numbers
num1, num2 = (num1, num2) if num1 < num2 else (num2, num1)
# ho through the switching divmod
result = 0
while num1 > 0:
# make divmod and switch numbers
divi, res = divmod(num2, num1)
num2 = num1
num1 = res
# update the result
result += divi
return result
|
count-operations-to-obtain-zero
|
[Python3] - Explained Switching Divmod (Euclidean Algorithm)
|
Lucew
| 0
| 3
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,103
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2611324/Simple-solution-easy-to-understand
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
c=0
while num1!=0 and num2!=0:
c=c+1
if num1>=num2:
num1=num1-num2
else:
num2=num2-num1
return c
|
count-operations-to-obtain-zero
|
Simple solution easy to understand
|
Prabal_Nair
| 0
| 22
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,104
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2607353/solution-using-heap
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
l=[num1,num2]
heapq.heapify(l)
c=0
while(True):
a =heapq.heappop(l)
b =heapq.heappop(l)
if a==0 or b==0:
return c
heapq.heappush(l,b-a)
heapq.heappush(l,a)
c+=1
return c
|
count-operations-to-obtain-zero
|
solution using heap
|
abhayCodes
| 0
| 7
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,105
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2447129/Python-oror-Faster-more-than-70
|
class Solution:
def countOperations(self, x: int, y: int) -> int:
count = 0
if x==0:
return x
if y == 0:
return y
while(True):
if x < y:
r = y - x
y = r
count += 1
else:
r = x -y
x = r
count += 1
if(r==0):
break
return count
```
Cheers! ----Upvote-----
|
count-operations-to-obtain-zero
|
Python || Faster more than 70%
|
saranshthukral231
| 0
| 19
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,106
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2176182/Python-simple-solution
|
class Solution:
def countOperations(self, n1: int, n2: int) -> int:
ans = 0
while True:
if n1 == 0 or n2 == 0:
return ans
elif n1 > n2:
n1 -= n2
ans += 1
elif n1 < n2:
n2 -= n1
ans += 1
else:
ans += 1
return ans
|
count-operations-to-obtain-zero
|
Python simple solution
|
StikS32
| 0
| 63
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,107
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2119766/8-Lines-of-Python-code-with-if-else
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
c = 0
while num1 != 0 and num2 != 0:
if num1>=num2:
num1 = num1 - num2
else:
num2 = num2 - num1
c += 1
return c
|
count-operations-to-obtain-zero
|
8 Lines of Python code with if else
|
prernaarora221
| 0
| 60
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,108
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2079083/Python-Solution-or-Simple-Logic-or-Simulation
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
ans = 0
while num1 > 0 and num2 > 0:
if num1 == num2:
return ans + 1
if num1 > num2:
num1 -= num2
else:
num2 -= num1
ans += 1
return ans
|
count-operations-to-obtain-zero
|
Python Solution | Simple Logic | Simulation
|
Gautam_ProMax
| 0
| 59
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,109
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/2062523/Python-Solution-2169
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
res=0
while num1 and num2:
if num1>=num2:
num1-=num2
res+=1
else :
num2-=num1
res+=1
return res
|
count-operations-to-obtain-zero
|
Python Solution #2169
|
MinAtoNAmikAze
| 0
| 31
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,110
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1941321/Python-dollarolution-(98-faster-97-better-memory)
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
s = 0
if num1 < num2:
num1, num2 = num2, num1
while num2 != 0:
s += num1//num2
num2, num1 = num1%num2 , num2
return s
|
count-operations-to-obtain-zero
|
Python $olution (98% faster, 97% better memory)
|
AakRay
| 0
| 57
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,111
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1901508/python-3-oror-simple-iterative-solution
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
res = 0
while num1 and num2:
if num1 >= num2:
num1 -= num2
else:
num2 -= num1
res += 1
return res
|
count-operations-to-obtain-zero
|
python 3 || simple iterative solution
|
dereky4
| 0
| 30
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,112
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1885814/Python3-Solution-using-Conditional-Statements
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count = 0
while num1 != 0 and num2 != 0:
if num1 > num2:
num1 -= num2
count += 1
elif num2 > num1:
num2 -= num1
count += 1
elif num2 == num1:
count += 1
break
return count
|
count-operations-to-obtain-zero
|
[Python3] Solution using Conditional Statements
|
natscripts
| 0
| 42
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,113
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1848664/python-arithmetic-operations
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
res = 0
while num1 > 0 and num2 > 0:
if num1 >= num2:
res += (num1//num2)
num1 = num1%num2
else:
res += (num2//num1)
num2 = num2%num1
return res
|
count-operations-to-obtain-zero
|
python arithmetic operations
|
abkc1221
| 0
| 37
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,114
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1842348/5-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-70
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
mx=max(num1,num2) ; mn=min(num1,num2) ; ans=0
while mx and mn:
mx=mx-mn ; ans+=1
if mx<mn: mx,mn=mn,mx
return ans
|
count-operations-to-obtain-zero
|
5-Lines Python Solution || 80% Faster || Memory less than 70%
|
Taha-C
| 0
| 38
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,115
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1771181/C%2B%2B-and-Python3-solution-using-division-and-modulus-operations
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
numberOfOperations = 0
while True:
if (num1 == 0 or num2 == 0):
break
elif (num1 == num2):
numberOfOperations += 1
break
elif (num1 > num2 and num1 % num2 == 0):
numberOfOperations += (num1 // num2)
break
elif (num2 > num1 and num2 % num1 == 0):
numberOfOperations += (num2 // num1)
break
else:
if (num1 >= num2):
num1 -= num2 # same as writing num1 = num1 - num2
else:
num2 -= num1 # same as writing num2 = num2 - num1
numberOfOperations += 1
return numberOfOperations
|
count-operations-to-obtain-zero
|
C++ and Python3 solution using division and modulus operations
|
vetikke
| 0
| 24
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,116
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1770310/Using-divmod-function-31ms-100-speed
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count = 0
while num1 > 0 and num2 > 0:
if num1 == num2:
return count + 1
elif num1 > num2:
n, num1 = divmod(num1, num2)
count += n
else:
n, num2 = divmod(num2, num1)
count += n
return count
|
count-operations-to-obtain-zero
|
Using divmod function, 31ms 100% speed
|
EvgenySH
| 0
| 19
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,117
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1769156/Python-naive-and-optimized-simulations-100-FAST!
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
result = 0
while num1 and num2:
q, r = divmod(num2, num1)
result += q
num1, num2 = r, num1
return result
|
count-operations-to-obtain-zero
|
Python, naive and optimized simulations, 100% FAST!
|
blue_sky5
| 0
| 17
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,118
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1769156/Python-naive-and-optimized-simulations-100-FAST!
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
result = 0
while num1 and num2:
result += 1
if num1 >= num2:
num1 -= num2
else:
num2 -= num1
return result
|
count-operations-to-obtain-zero
|
Python, naive and optimized simulations, 100% FAST!
|
blue_sky5
| 0
| 17
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,119
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1768228/Python3-quick-solution
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
res = 0
while num1 != 0 and num2 != 0:
if num1 >= num2:
res += num1 // num2
num1 = num1 % num2
else:
res += num2 // num1
num2 = num2 % num1
return res
|
count-operations-to-obtain-zero
|
Python3 quick solution
|
bianrui0315
| 0
| 15
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,120
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1767293/Python3-Faster-than-100
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
cnt = 0
while (num1 and num2):
if num1 >= num2:
cnt += num1//num2
num1 = num1%num2
else:
cnt += num2//num1
num2 = num2%num1
return cnt
|
count-operations-to-obtain-zero
|
Python3 Faster than 100%
|
howler
| 0
| 16
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,121
|
https://leetcode.com/problems/count-operations-to-obtain-zero/discuss/1766769/Easy-Python-3-Solution
|
class Solution:
def countOperations(self, num1: int, num2: int) -> int:
count = 0
while num1 > 0 and num2 > 0:
if num1 >= num2:
num1 -= num2
else:
num2 -= num1
count += 1
return count
|
count-operations-to-obtain-zero
|
Easy Python 3 Solution
|
sdasstriver9
| 0
| 39
|
count operations to obtain zero
| 2,169
| 0.755
|
Easy
| 30,122
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1766909/Python3-4-line
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
pad = lambda x: x + [(None, 0)]*(2-len(x))
even = pad(Counter(nums[::2]).most_common(2))
odd = pad(Counter(nums[1::2]).most_common(2))
return len(nums) - (max(even[0][1] + odd[1][1], even[1][1] + odd[0][1]) if even[0][0] == odd[0][0] else even[0][1] + odd[0][1])
|
minimum-operations-to-make-the-array-alternating
|
[Python3] 4-line
|
ye15
| 19
| 1,600
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,123
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1766909/Python3-4-line
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
odd, even = Counter(), Counter()
for i, x in enumerate(nums):
if i&1: odd[x] += 1
else: even[x] += 1
def fn(freq):
key = None
m0 = m1 = 0
for k, v in freq.items():
if v > m0: key, m0, m1 = k, v, m0
elif v > m1: m1 = v
return key, m0, m1
k0, m00, m01 = fn(even)
k1, m10, m11 = fn(odd)
return len(nums) - max(m00 + m11, m01 + m10) if k0 == k1 else len(nums) - m00 - m10
|
minimum-operations-to-make-the-array-alternating
|
[Python3] 4-line
|
ye15
| 19
| 1,600
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,124
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1767132/Python-3-hashmap-solution-O(N)-no-sort-needed
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
n = len(nums)
odd, even = defaultdict(int), defaultdict(int)
for i in range(n):
if i % 2 == 0:
even[nums[i]] += 1
else:
odd[nums[i]] += 1
topEven, secondEven = (None, 0), (None, 0)
for num in even:
if even[num] > topEven[1]:
topEven, secondEven = (num, even[num]), topEven
elif even[num] > secondEven[1]:
secondEven = (num, even[num])
topOdd, secondOdd = (None, 0), (None, 0)
for num in odd:
if odd[num] > topOdd[1]:
topOdd, secondOdd = (num, odd[num]), topOdd
elif odd[num] > secondOdd[1]:
secondOdd = (num, odd[num])
if topOdd[0] != topEven[0]:
return n - topOdd[1] - topEven[1]
else:
return n - max(secondOdd[1] + topEven[1], secondEven[1] + topOdd[1])
|
minimum-operations-to-make-the-array-alternating
|
Python 3 hashmap solution O(N), no sort needed
|
xil899
| 12
| 654
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,125
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/2778316/Python3-Commented-and-Easy-Solution-O(N)
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
# trivial case
if len(nums) < 2:
return 0
# count the even and odd indexed numbers
cn_even = collections.Counter(nums[::2])
cn_odd = collections.Counter(nums[1::2])
# use the most common even and odd index numbers if they are not equal
most_even = cn_even.most_common(2)
most_odd = cn_odd.most_common(2)
# check whether most common elements are equal and if so
# check out which combination is better
if most_even[0][0] == most_odd[0][0]:
# if both have two most common elements
if len(most_even) == 2 and len(most_odd) == 2:
# get the subtractor for the case where both have multiple different
# elements and find the combination that has the most characters
# that need no changing
subtractor = max(most_even[0][1] + most_odd[1][1], most_even[1][1] + most_odd[0][1])
elif len(most_even) == 2:
# we need to take
subtractor = most_odd[0][1] + most_even[1][1]
elif len(most_odd) == 2:
subtractor = most_odd[1][1] + most_even[0][1]
else:
subtractor = len(nums) - len(nums)//2
else:
subtractor = most_even[0][1] + most_odd[0][1]
return len(nums) - subtractor
|
minimum-operations-to-make-the-array-alternating
|
[Python3] - Commented and Easy Solution - O(N)
|
Lucew
| 0
| 7
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,126
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/2715024/Python3-or-Find-Max-and-Second-max
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
if len(nums)==1:
return 0
n=len(nums)
ans=float('inf')
oddFreq=Counter([nums[i] for i in range(n) if i%2!=0])
evenFreq=Counter([nums[i] for i in range(n) if i%2==0])
evenPos,oddPos=(n+1)//2,n//2
evenArray=sorted([[key,value] for key,value in evenFreq.items()],key=lambda x:x[1],reverse=True)
oddArray=sorted([[key,value] for key,value in oddFreq.items()],key=lambda x:x[1],reverse=True)
if evenArray[0][0]==oddArray[0][0]:
if len(oddArray)>1:
ans=min(ans,evenPos-evenArray[0][1]+oddPos-oddArray[1][1])
else:
ans=min(ans,evenPos-evenArray[0][1]+oddArray[0][1])
if len(evenArray)>1:
ans=min(ans,oddPos-oddArray[0][1]+evenPos-evenArray[1][1])
else:
ans=min(ans,oddPos-oddArray[0][1]+evenArray[0][1])
else:
ans=min(ans,evenPos-evenArray[0][1]+oddPos-oddArray[0][1])
return ans
|
minimum-operations-to-make-the-array-alternating
|
[Python3] | Find Max and Second max
|
swapnilsingh421
| 0
| 5
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,127
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/2324119/Python3-or-hashmap-and-priority-queueor-Easy-to-understand
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
if len(nums) < 2:
return 0
even = []
even_table = defaultdict(int)
for count, num in enumerate(nums):
if not count&1:
even_table[num] += 1
for num, occurance in even_table.items():
heapq.heappush(even, [-occurance, num])
odd_table = defaultdict(int)
odd = []
for count, num in enumerate(nums):
if count&1:
odd_table[num] += 1
for num, occurance in odd_table.items():
heapq.heappush(odd, [-occurance, num])
even_frequent = heapq.heappop(even)
odd_frequent = heapq.heappop(odd)
while (even_frequent[1] == odd_frequent[1]):
if not even and not odd:
return int(len(nums)/2)
elif not even:
return self.count(nums, even_frequent, heapq.heappop(odd))
elif not odd:
return self.count(nums, heapq.heappop(even), odd_frequent)
else:
return min([self.count(nums, even_frequent, heapq.heappop(odd)),
self.count(nums, heapq.heappop(even), odd_frequent)])
return self.count(nums, even_frequent, odd_frequent)
def count(self, nums: List[int], even_frequent: List[int], odd_frequent: List[int]) -> int:
res = 0
if not even_frequent or not odd_frequent:
return float('inf')
for count, num in enumerate(nums):
if not count&1 and num != even_frequent[1]:
res += 1
elif count&1 and num != odd_frequent[1]:
res += 1
return res
|
minimum-operations-to-make-the-array-alternating
|
Python3 | hashmap and priority queue| Easy to understand
|
shawn15
| 0
| 57
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,128
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1958008/6-Lines-Python-Solution-oror-99-Faster-oror-Memory-less-than-98
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
n=len(nums) ; odd_len=n//2 ; even_len=n-odd_len
if n <= 1: return 0
c1=Counter(nums[i] for i in range(0,n,2)).most_common(2)+[(-1,0)]
c2=Counter(nums[i] for i in range(1,n,2)).most_common(2)+[(-1,0)]
if c1[0][0]!=c2[0][0]: return (even_len-c1[0][1])+(odd_len-c2[0][1])
else: return min((even_len-c1[0][1])+(odd_len-c2[1][1]), (even_len-c1[1][1])+(odd_len-c2[0][1]))
|
minimum-operations-to-make-the-array-alternating
|
6-Lines Python Solution || 99% Faster || Memory less than 98%
|
Taha-C
| 0
| 125
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,129
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1771570/Python3-or-Easy-to-understand
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
freqEven = [0]*100001
freqOdd = [0]*100001
maxi = 0
n = len(nums)
for i in range(n):
if i%2 == 0:
freqEven[nums[i]] += 1
else:
freqOdd[nums[i]] += 1
maxi = max(nums[i], maxi)
firstMaxEven, freqFirstMaxEven = 0, 0
firstMaxOdd, freqFirstMaxOdd = 0, 0
secondMaxEven, freqSecondMaxEven = 0, 0
secondMaxOdd, freqSecondMaxOdd = 0, 0
for i in range(maxi+1):
if freqEven[i] >= freqFirstMaxEven:
freqSecondMaxEven = freqFirstMaxEven
secondMaxEven = firstMaxEven
freqFirstMaxEven = freqEven[i]
firstMaxEven = i
elif freqEven[i] > freqSecondMaxEven:
freqSecondMaxEven = freqEven[i]
secondMaxEven = i
if freqOdd[i] >= freqFirstMaxOdd:
freqSecondMaxOdd = freqFirstMaxOdd
secondMaxOdd = firstMaxOdd
freqFirstMaxOdd = freqOdd[i]
firstMaxOdd = i
elif freqOdd[i] > freqSecondMaxOdd:
freqSecondMaxOdd = freqOdd[i]
secondMaxOdd = i
if (firstMaxEven != firstMaxOdd):
return n-freqFirstMaxEven-freqFirstMaxOdd
else:
return min(n-freqFirstMaxOdd-freqSecondMaxEven, n-freqFirstMaxEven-freqSecondMaxOdd)
|
minimum-operations-to-make-the-array-alternating
|
Python3 | Easy to understand
|
prankurgupta18
| 0
| 67
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,130
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1770198/Frequency-dictionaries-75-speed
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
len_nums = len(nums)
if len_nums < 2:
return 0
dict_even_freq, dict_odd_freq = defaultdict(int), defaultdict(int)
for i, n in enumerate(nums):
if i % 2:
dict_odd_freq[n] += 1
else:
dict_even_freq[n] += 1
even_freq_key, odd_freq_key = defaultdict(set), defaultdict(set)
for k, f in dict_even_freq.items():
even_freq_key[f].add(k)
for k, f in dict_odd_freq.items():
odd_freq_key[f].add(k)
even_freq = sorted(even_freq_key.keys(), reverse=True)
odd_freq = sorted(odd_freq_key.keys(), reverse=True)
if even_freq_key[even_freq[0]] != odd_freq_key[odd_freq[0]]:
return len_nums - even_freq[0] - odd_freq[0]
ans = len_nums // 2
if (len(odd_freq) > 1 and
even_freq_key[even_freq[0]] != odd_freq_key[odd_freq[1]]):
ans = min(ans, len_nums - even_freq[0] - odd_freq[1])
if (len(even_freq) > 1 and
even_freq_key[even_freq[1]] != odd_freq_key[odd_freq[0]]):
ans = min(ans, len_nums - even_freq[1] - odd_freq[0])
return ans
|
minimum-operations-to-make-the-array-alternating
|
Frequency dictionaries, 75% speed
|
EvgenySH
| 0
| 59
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,131
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1767309/Python3-Two-Dictionaries-or-O(n)
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
n=len(nums)
evenCount, oddCount = defaultdict(int), defaultdict(int)
for i in range(0,n, 2):
evenCount[nums[i]]+=1
if i<n-1:
oddCount[nums[i+1]]+=1
max_even, second_max_even = (None, 0), (None, 0)
for num, cnt in evenCount.items():
if cnt>max_even[1]:
max_even, second_max_even = (num, cnt), max_even
elif cnt>second_max_even[1]:
second_max_even = (num, cnt)
max_odd, second_max_odd = (None, 0), (None, 0)
for num, cnt in oddCount.items():
if cnt>max_odd[1]:
max_odd, second_max_odd = (num, cnt), max_odd
elif cnt>second_max_odd[1]:
second_max_odd=(num, cnt)
if max_even[0] == max_odd[0]:
return min(n-max_even[1]-second_max_odd[1], n-second_max_even[1]-max_odd[1])
return n-max_even[1]-max_odd[1]
|
minimum-operations-to-make-the-array-alternating
|
[Python3] Two Dictionaries | O(n)
|
__PiYush__
| 0
| 61
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,132
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1767288/Python-answer-using-counter-easy-solution
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
a0 = Counter(nums[0::2]).most_common(2)
a1 = Counter(nums[1::2]).most_common(2)
if len(nums) == 1:
return 0
elif a0[0][0] != a1[0][0]:
return len(nums) - (a0[0][1] + a1[0][1])
elif len(a0) == 1 and len(a1) != 1:
return len(nums) - (a0[0][1] + a1[1][1])
elif len(a1) == 1 and len(a0) != 1:
return len(nums) - (a0[1][1] + a1[0][1])
elif len(a1) == 1 and len(a0) == 1:
return len(nums) // 2
else:
return len(nums) - max([a0[1][1] + a1[0][1], a0[0][1] + a1[1][1]])
|
minimum-operations-to-make-the-array-alternating
|
Python answer using counter, easy solution
|
chazeon
| 0
| 81
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,133
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1767033/Python3-Count-Sort-Simple-Comparison
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
n = len(nums)
if n == 1: return 0
even = odd = n//2
if n%2:
even += 1
mfe = Counter()
mfo = Counter()
for i in range(n):
if i%2:
mfe[nums[i]] += 1
else:
mfo[nums[i]] += 1
nums1 = sorted(mfe.items(), key=lambda x:x[1], reverse=True)
nums2 = sorted(mfo.items(), key=lambda x:x[1], reverse=True)
if nums1[0][0] != nums2[0][0]:
return (even-nums1[0][1]) + (odd-nums2[0][1])
else:
a = nums1[0][1]
b = nums1[1][1] if len(nums1) > 1 else 0
c = nums2[0][1]
d = nums2[1][1] if len(nums2) > 1 else 0
return min(((even-a)+(odd-d)),((even-b)+(odd-c)))
|
minimum-operations-to-make-the-array-alternating
|
Python3 - Count - Sort - Simple Comparison
|
sankitshane
| 0
| 59
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,134
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1767026/Python-3-or-Find-max-and-second-max-frequency-for-odd-and-even-place-elements-or-O(n)-solution
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
#hashtable with two keys
d={}
#0 for count of even placed elements
d[0]=defaultdict(lambda:0)
#1 for count of odd placed elements
d[1]=defaultdict(lambda:0)
#count each element
for i,num in enumerate(nums):
d[i%2][num]+=1
#zero1 for highest occuring element
zero1=None
#zero2 for second highest occuring element
zero2=None
mc=0
#find highest and second highest occuring elements
for key in d[0].keys():
if d[0][key]>=mc:
mc=d[0][key]
zero2=zero1
zero1=key
elif zero2 and d[0][key]>d[0][zero2]:
zero2=key
elif not zero2:
zero2=key
one1=None
one2=None
mc=0
for key in d[1].keys():
if d[1][key]>=mc:
mc=d[1][key]
one2=one1
one1=key
elif one2 and d[1][key]>d[1][one2]:
one2=key
elif not one2:
one2=key
#if both highest element in even and odd place are same
if one1==zero1:
#if the element occurs more in odd place than in even places
if d[1][one1]>d[0][zero1]:
one=one1
zero=zero2
#if the element occurs more in even place than in odd places
elif d[1][one1]<d[0][zero1]:
one=one2
zero=zero1
#if the element occurs same number of times in both even and odd places
elif d[1][one1]==d[0][zero1]:
#priotrize as per second highest occuring element
if d[1][one2]>d[0][zero2]:
one=one2
zero=zero1
else:
one=one1
zero=zero2
else:
one=one1
zero=zero1
#We don't need to change selected elements in array but rest of them
#so we'll substract them from length of array
#Note: default dict give value 0 in case key is None
x=len(nums)-d[1][one]-d[0][zero]
return x
|
minimum-operations-to-make-the-array-alternating
|
Python 3 | Find max and second max frequency for odd and even place elements | O(n) solution
|
alokmalik
| 0
| 58
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,135
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1766854/2170-%3A-Python-O(NlogN)-solution-with-Heaps
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
n = len(nums)
if n == 1 :
return 0
c1 = Counter()
c2 = Counter()
for i,item in enumerate(nums) :
if i % 2 == 0 :
c1[item] += 1
else :
c2[item] += 1
heap1 = []
for item,count in c1.items() :
heappush(heap1, (-count,item) )
heap2 = []
for item,count in c2.items() :
heappush(heap2, (-count,item) )
max1_c,max1_item = heappop(heap1)
max2_c,max2_item = heappop(heap2)
max1_c,max2_c = -max1_c,-max2_c
while heap1 and heap2 and max1_item == max2_item :
if max1_c < max2_c :
max1_c,max1_item = heappop(heap1)
max1_c = -max1_c
else :
max2_c,max2_item = heappop(heap2)
max2_c = -max2_c
if max1_item != max2_item :
return ((n + 1) // 2 - max1_c) +( n//2 - max2_c)
return min( max1_c, max2_c )
|
minimum-operations-to-make-the-array-alternating
|
2170 : Python O(NlogN) solution with Heaps
|
Manideep8
| 0
| 99
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,136
|
https://leetcode.com/problems/minimum-operations-to-make-the-array-alternating/discuss/1766785/Python-oror-Easy-oror-simple-solution-oror-Explanation
|
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
x,y={},{}
for i in range(0,len(nums),2): # count the occurence of number
x[nums[i]]=x.get(nums[i],0)+1
for i in range(1,len(nums),2): # count the occurence of number
y[nums[i]]=y.get(nums[i],0)+1
sum1=sum(x.values()) # count the total occurence of number
sum2=sum(y.values()) # count the occurence of number
l1=[(k,x[k]) for k in x.keys()] # make list
l2=[(k,y[k]) for k in y.keys()] # make list
l1=sorted(l1,key=lambda x:x[1],reverse=True) # sort in reverse order
l2=sorted(l2,key=lambda x:x[1],reverse=True)
for i in l1:
for j in l2:
if i[0]!=j[0]: # check the given condition
return sum1-i[1]+sum2-j[1] # convert list into nums[i-1] and nums[i]
return min(sum1,sum2) # return minimum occurance
|
minimum-operations-to-make-the-array-alternating
|
Python || Easy || simple solution || Explanation
|
rushi_javiya
| 0
| 184
|
minimum operations to make the array alternating
| 2,170
| 0.332
|
Medium
| 30,137
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1766873/Python3-2-line
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans.sort()
return sum(beans) - max((len(beans)-i)*x for i, x in enumerate(beans))
|
removing-minimum-number-of-magic-beans
|
[Python3] 2-line
|
ye15
| 4
| 163
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,138
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/2123863/python-3-oror-short-and-simple-sorting-solution-with-explaination
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans.sort()
n, s = len(beans), sum(beans)
return min(s - bag*(n - i) for i, bag in enumerate(beans))
|
removing-minimum-number-of-magic-beans
|
python 3 || short and simple sorting solution with explaination
|
dereky4
| 0
| 95
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,139
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1953401/3-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-60
|
class Solution:
def minimumRemoval(self, B: List[int]) -> int:
B=sorted(B) ; s=sum(B) ; ans=[]
for i in range(len(B)): ans.append(s-B[i]*(len(B)-i))
return min(ans)
|
removing-minimum-number-of-magic-beans
|
3-Lines Python Solution || 95% Faster || Memory less than 60%
|
Taha-C
| 0
| 99
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,140
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1953401/3-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-60
|
class Solution:
def minimumRemoval(self, B: List[int]) -> int:
B=sorted(B) ; S=sum(B) ; ans=S
for i in range(len(B)):
currS=S-B[i]*(len(B)-i)
if currS<ans: ans=currS
return ans
|
removing-minimum-number-of-magic-beans
|
3-Lines Python Solution || 95% Faster || Memory less than 60%
|
Taha-C
| 0
| 99
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,141
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1773781/faster-than-95.13-of-Python3-online-submissions-or-python-3
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans.sort()
n = len(beans)
minSteps = float('inf')
totalSum = 0
for i in range(n):
totalSum += beans[i]
for i in range(n):
newSum = totalSum-(n-i)*beans[i]
minSteps = min(minSteps, newSum)
return minSteps
|
removing-minimum-number-of-magic-beans
|
faster than 95.13% of Python3 online submissions | python 3
|
prankurgupta18
| 0
| 36
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,142
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1768341/Python-3-Linear-scan-with-comments
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
c = Counter(beans)
n = len(beans)
tot = sum(beans)
left_tot = 0 # number of beans need to remove to empty the bag
ans= float('inf')
for k in sorted(c):
n -= c[k] # remaining bags
tot -= k * c[k] # remaining beans
ans = min(ans, tot - n * k + left_tot) # beans to remove if number of beans are "k" in each bag or zero
left_tot += k * c[k] # be careful to add the current number of beans after updating the answer
return ans
|
removing-minimum-number-of-magic-beans
|
[Python 3] Linear scan with comments
|
chestnut890123
| 0
| 32
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,143
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1768321/Best-Explanation-with-Diagram-oror-For-Beginners
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans.sort()
n = len(beans)
res = total = sum(beans)
pre = [0 for _ in range(n)]
for i,b in enumerate(beans):
pre[i] = pre[i-1] + b
for i in range(n):
if i==0:
tmp = (total-pre[i]) - ((n-i-1)*beans[i])
else:
tmp = pre[i-1]+(total-pre[i]) - ((n-i-1)*beans[i])
res = min(res, tmp)
return res
|
removing-minimum-number-of-magic-beans
|
📌📌 Best Explanation with Diagram || For Beginners 🐍
|
abhi9Rai
| 0
| 39
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,144
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1767040/Very-simple-Python-solution-Find-biggest-rectangle
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans.sort(reverse = True)
area = 0
for i,v in enumerate(beans):
area = max(area, v*(i+1))
return sum(beans) - area
|
removing-minimum-number-of-magic-beans
|
Very simple Python solution - Find biggest rectangle
|
8by8
| 0
| 27
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,145
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1766997/Python-Top-87.5-fastest-or-Time-(N-Log-N)-or-Beginner-friendly
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans = sorted(beans)
minimal = sys.maxsize
bot_removed = 0
top_removed = sum(beans)
if len(beans)==1:
return 0
prev=0
for i in range(len(beans)):
top_removed -= (beans[i]-prev)*(len(beans)-1-i)
minimal = min(minimal,top_removed+bot_removed-beans[i])
bot_removed += beans[i]
prev = beans[i]
return minimal
|
removing-minimum-number-of-magic-beans
|
Python Top 87.5% fastest | Time (N Log N) | Beginner friendly
|
yourongl
| 0
| 14
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,146
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1766843/Python-or-Easy-to-understand
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
beans.sort()
min_operations = float('inf')
n = len(beans)
left_sum = 0
right_sum = sum(beans)
for i in range(n):
right_sum -= beans[i]
cur_operations = 0
cur_operations += left_sum
cur_operations += right_sum - (n-i-1) * beans[i]
left_sum += beans[i]
min_operations = min(min_operations, cur_operations)
return min_operations
|
removing-minimum-number-of-magic-beans
|
Python | Easy to understand
|
Mikey98
| 0
| 45
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,147
|
https://leetcode.com/problems/removing-minimum-number-of-magic-beans/discuss/1766790/Python3-DP-%2B-Sorting-%2B-Prefix-Sum-solution-with-comments-O(nlogn)
|
class Solution:
def minimumRemoval(self, beans: List[int]) -> int:
#sort the array
beans = sorted(beans)
#create array for dynamic programming
dp = [0] * len(beans)
#prefix sum
for i in range(len(beans)):
dp[i] = dp[i-1] + beans[i]
#find the minimum number looping through the dp array
res = (dp[-1] - dp[0]) - beans[0] * (len(beans) - 1)
for i in range(1, len(beans)):
res = min(res, (dp[-1] - dp[i]) - beans[i] * (len(beans) - i - 1) + dp[i-1] )
return res
|
removing-minimum-number-of-magic-beans
|
[Python3] DP + Sorting + Prefix Sum solution with comments O(nlogn)
|
afcarreral
| 0
| 46
|
removing minimum number of magic beans
| 2,171
| 0.42
|
Medium
| 30,148
|
https://leetcode.com/problems/maximum-and-sum-of-array/discuss/1766984/Python3-dp-(top-down)
|
class Solution:
def maximumANDSum(self, nums: List[int], numSlots: int) -> int:
@cache
def fn(k, m):
"""Return max AND sum."""
if k == len(nums): return 0
ans = 0
for i in range(numSlots):
if m & 1<<2*i == 0 or m & 1<<2*i+1 == 0:
if m & 1<<2*i == 0: mm = m ^ 1<<2*i
else: mm = m ^ 1<<2*i+1
ans = max(ans, (nums[k] & i+1) + fn(k+1, mm))
return ans
return fn(0, 0)
|
maximum-and-sum-of-array
|
[Python3] dp (top-down)
|
ye15
| 13
| 591
|
maximum and sum of array
| 2,172
| 0.475
|
Hard
| 30,149
|
https://leetcode.com/problems/maximum-and-sum-of-array/discuss/1768324/Python-3-Two-masks
|
class Solution:
def maximumANDSum(self, nums: List[int], numSlots: int) -> int:
@lru_cache(None)
def dp(i, m1, m2):
if i == len(nums):
return 0
ans = float('-inf')
for j in range(numSlots):
if m1 & (1 << j):
ans = max(ans, (nums[i] & (j + 1)) + dp(i + 1, m1 ^ (1 << j), m2))
elif m2 & (1 << j):
ans = max(ans, (nums[i] & (j + 1)) + dp(i + 1, m1, m2 ^ (1 << j)))
return ans
return dp(0, (1<<numSlots)-1, (1<<numSlots)-1)
|
maximum-and-sum-of-array
|
[Python 3] Two masks
|
chestnut890123
| 1
| 73
|
maximum and sum of array
| 2,172
| 0.475
|
Hard
| 30,150
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1783447/Python3-Solution-fastest
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n=len(nums)
c=0
for i in range(0,n):
for j in range(i+1,n):
if nums[i]==nums[j] and ((i*j)%k==0):
c+=1
return c
|
count-equal-and-divisible-pairs-in-an-array
|
Python3 Solution fastest
|
Anilchouhan181
| 6
| 812
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,151
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1783476/Python-3-(80ms)-or-Easy-Brute-Force-N2-Approach
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n=len(nums)
c=0
for i in range(0,n):
for j in range(i+1,n):
if nums[i]==nums[j] and ((i*j)%k==0):
c+=1
return c
|
count-equal-and-divisible-pairs-in-an-array
|
Python 3 (80ms) | Easy Brute Force N^2 Approach
|
MrShobhit
| 3
| 391
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,152
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2291829/PYTHON-3-EASY-or-2-FOR-LOOPS-or-LOW-MEMORY
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
c = 0
for i in range(len(nums)):
for e in range(i+1, len(nums)):
if nums[i] == nums[e] and i*e % k == 0:
c += 1
return c
|
count-equal-and-divisible-pairs-in-an-array
|
[PYTHON 3] EASY | 2 FOR LOOPS | LOW MEMORY
|
omkarxpatel
| 2
| 87
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,153
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1783422/Python3-brute-force
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
ans = 0
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] == nums[j] and i*j % k == 0: ans += 1
return ans
|
count-equal-and-divisible-pairs-in-an-array
|
[Python3] brute-force
|
ye15
| 2
| 136
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,154
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2043440/Python3-easy-to-understand
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
for i,_ in enumerate(nums):
for j,_ in enumerate(nums):
if i < j < len(nums) and nums[i] == nums[j]:
if (i * j) % k == 0:
count += 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
[Python3] easy to understand
|
Shiyinq
| 1
| 76
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,155
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1993328/Python3-Easy-to-understand
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
dic = defaultdict(list)
for i, n in enumerate(nums):
dic[n].append(i)
res = 0
for v in dic.values():
if len(v) > 1:
res += len([ 1 for x, y in itertools.combinations(v, 2) if x * y % k == 0])
return res
|
count-equal-and-divisible-pairs-in-an-array
|
Python3 Easy to understand
|
anels
| 1
| 71
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,156
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1936078/Python3-One-Line-Solution
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
return sum([1 for i in range(len(nums)) for j in range(i + 1, len(nums)) if nums[i] == nums[j] and not i * j % k])
|
count-equal-and-divisible-pairs-in-an-array
|
[Python3] One-Line Solution
|
terrencetang
| 1
| 163
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,157
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1915313/python-3-oror-easy-solution
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count=0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]==nums[j]:
if (i*j)%k==0:
count+=1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
python 3 || easy solution
|
nileshporwal
| 1
| 109
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,158
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2849990/python
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
ans = 0
dic = defaultdict(list)
for idx, num in enumerate(nums):
dic[num].append(idx)
for key in dic:
n = len(dic[key])
if n > 1:
for i in range(n - 1):
for j in range(i + 1, n):
if not dic[key][i] * dic[key][j] % k:
ans += 1
return ans
|
count-equal-and-divisible-pairs-in-an-array
|
python
|
xy01
| 0
| 1
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,159
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2830460/Easy-python-code-beats-99
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count=0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]==nums[j] and (i*j)%k==0:
count+=1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Easy python code beats 99%
|
kmpravin5
| 0
| 3
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,160
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2787233/Simple-5-Line-Code-Using-Python
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
n = len(nums)
for i in range(0,n):
for j in range(i+1,n):
if nums[i] == nums[j] and i*j % k == 0:
count = count + 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Simple 5 Line Code Using Python
|
dnvavinash
| 0
| 2
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,161
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2786380/Python3-solution-nested-loop
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
ans = 0
for i, num in enumerate(nums):
j = i + 1
while j < len(nums):
if nums[i] == nums[j] and i * j % k == 0:
ans += 1
j += 1
return ans
|
count-equal-and-divisible-pairs-in-an-array
|
Python3 solution - nested loop
|
sipi09
| 0
| 3
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,162
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2785713/Python-or-LeetCode-or-2176.-Count-Equal-and-Divisible-Pairs-in-an-Array
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
n = len(nums)
count = 0
for i in range(n):
for j in range(i + 1, n):
if nums[i] == nums[j] and (i*j) % k == 0:
count += 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python | LeetCode | 2176. Count Equal and Divisible Pairs in an Array
|
UzbekDasturchisiman
| 0
| 4
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,163
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2779544/Python-99-faster-solution.
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
pair = 0
nums_dict = {}
for i in range(len(nums)):
if nums[i] in nums_dict:
for j in nums_dict[nums[i]]:
if i * j % k == 0:
pair+=1
nums_dict[nums[i]].append(i)
else:
nums_dict[nums[i]] = [i]
return pair
|
count-equal-and-divisible-pairs-in-an-array
|
[Python] 99% faster solution.
|
kawamataryo
| 0
| 4
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,164
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2772058/Python-HashMap
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
d = {}
count = 0
for i , val in enumerate(nums):
if val not in d:
d[val] = [i]
else:
for num in d[val]:
if (num * i ) % k == 0:
count += 1
d[val].append(i)
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python HashMap
|
theReal007
| 0
| 5
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,165
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2661641/easy-soln
|
class Solution:
def countPairs(self, c: List[int], k: int) -> int:
res=0
for i in range(len(c)):
for m in range(i+1,len(c)):
if c[i]==c[m] and (i*m)%k==0:
res+=1
return(res)
|
count-equal-and-divisible-pairs-in-an-array
|
easy soln
|
AMBER_FATIMA
| 0
| 1
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,166
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2588562/Python3-Solution-oror-Nested-for-Loop-oror-Simple
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if ((i * j) % k == 0):
if (nums[i] == nums[j]):
count += 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python3 Solution || Nested for Loop || Simple
|
shashank_shashi
| 0
| 17
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,167
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2558050/EASY-PYTHON3-SOLUTION-simple
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] == nums[j] and (i*j)%k==0:
count += 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
✅✔🔥 EASY PYTHON3 SOLUTION 🔥✅✔ simple
|
rajukommula
| 0
| 22
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,168
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2479961/Python-beats-97
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
seen=defaultdict(list)
pairs=[]
count=0
for i in range(len(nums)):
if nums[i] not in seen:
seen[nums[i]].append(i)
else:
for j in seen[nums[i]]:
pairs.append((i,j))
seen[nums[i]].append(i)
for (i,j) in pairs:
if (i*j)%k == 0:
count+=1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python beats 97%
|
aruj900
| 0
| 54
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,169
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2178838/Python-simple-solution
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
ans = 0
for i in range(len(nums)):
for j in range(i, len(nums)):
if i == j: continue
if (i*j)%k == 0 and nums[i] == nums[j]:
ans += 1
return ans
|
count-equal-and-divisible-pairs-in-an-array
|
Python simple solution
|
StikS32
| 0
| 66
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,170
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2175785/Python-Solution-using-dict-or-90-faster
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
mp = defaultdict(list)
count = 0
for i in range(len(nums)):
for j in mp[nums[i]]:
if (i*j)%k==0:
count += 1
mp[nums[i]].append(i)
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python Solution using dict | 90% faster
|
numbcoder619
| 0
| 72
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,171
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2160550/Simple-Logic-with-python
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
for i, j in combinations(range(len(nums)), 2):
if (nums[i] == nums[j]) and ((i*j) % k == 0):
count += 1
return count
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
return sum(nums[i] == nums[j] and (i*j) % k == 0 for i, j in combinations(range(len(nums)),2))
|
count-equal-and-divisible-pairs-in-an-array
|
Simple Logic with python
|
writemeom
| 0
| 37
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,172
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2159476/Python-or-90-faster-or-Easy-Solution-using-Brute-Force-technique
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
# ////// Brute Force approach TC: O(N*N) //////
count = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] == nums[j] and (i * j) % k == 0:
count += 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python | 90% faster | Easy Solution using Brute Force technique
|
__Asrar
| 0
| 44
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,173
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/2113758/Python-Solutions
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
result = 0
data = defaultdict(list)
for index, num in enumerate(nums):
data[num].append(index)
for v in data.values():
for i in range(len(v)):
result += reduce(lambda x, y: x + (v[i] * v[y] % k == 0), range(i + 1, len(v)), 0)
return result
|
count-equal-and-divisible-pairs-in-an-array
|
Python Solutions
|
hgalytoby
| 0
| 64
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,174
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1926036/Python3-simple-solution
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] == nums[j] and i * j % k == 0:
count += 1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 57
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,175
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1862061/Python-(Simple-Approach-and-Beginner-Friendly)
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count = 0
for i in range(0,len(nums)):
for j in range(i, len(nums)):
if nums[i] == nums[j] and (i*j)%k == 0 and i!=j:
print(i,j)
count+=1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
Python (Simple Approach and Beginner-Friendly)
|
vishvavariya
| 0
| 44
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,176
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1832585/Python-Simple-Python-Solution-With-Two-Approach-oror-94-Faster
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
result = 0
for i in range(len(nums)-1):
for j in range(i+1,len(nums)):
if nums[i] == nums[j] and (i*j) % k == 0:
result = result + 1
return result
|
count-equal-and-divisible-pairs-in-an-array
|
[ Python ] ✔✔ Simple Python Solution With Two Approach || 94 % Faster 🔥✌
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 88
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,177
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1832585/Python-Simple-Python-Solution-With-Two-Approach-oror-94-Faster
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
frequency = {}
result = 0
for i in range(len(nums)):
if nums[i] not in frequency:
frequency[nums[i]] = [i]
else:
frequency[nums[i]].append(i)
for i in frequency:
if len(frequency[i]) >= 2:
for index1 in range( len(frequency[i])-1 ):
for index2 in range( index1 + 1,len(frequency[i]) ):
if ( frequency[i][index1] * frequency[i][index2] ) % k == 0 :
result = result + 1
return result
|
count-equal-and-divisible-pairs-in-an-array
|
[ Python ] ✔✔ Simple Python Solution With Two Approach || 94 % Faster 🔥✌
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 88
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,178
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1819129/Ez-sol-oror-Brute-Force-oror-Hahsmap-oror-Explained
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
co=0
for i in range(0, len(nums)):
for j in range(i+1, len(nums)):
if((nums[i]==nums[j]) and (i*j)%k==0):
co+=1
return co
|
count-equal-and-divisible-pairs-in-an-array
|
Ez sol || Brute Force || Hahsmap || Explained
|
ashu_py22
| 0
| 36
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,179
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1819129/Ez-sol-oror-Brute-Force-oror-Hahsmap-oror-Explained
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
freq={}
co=0
for i in range(len(nums)):
if nums[i] not in freq:
freq[nums[i]]=[]
freq[nums[i]].append(i)
for values in freq.values():
for pairs in itertools.combinations(values, 2):
if pairs[0]*pairs[1]%k==0:
co+=1
return co
|
count-equal-and-divisible-pairs-in-an-array
|
Ez sol || Brute Force || Hahsmap || Explained
|
ashu_py22
| 0
| 36
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,180
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1799590/O(N1.5)
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
if k==1 :
mp=Counter(nums)
return sum((mp[k]*(mp[k]-1))//2 for k in mp.keys())
ans=nums.count(nums[0])-1
n=len(nums)
for i in range(k,n**2,k):
x=1
while x*x <= i :
ans+=(i%x==0 and i//x < n and x<n and x*x!=i and nums[x]==nums[i//x])
x+=1
return ans
|
count-equal-and-divisible-pairs-in-an-array
|
O(N^1.5)
|
P3rf3ct0
| 0
| 51
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,181
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1786576/Index-combinations-over-equal-values-72ms-100-speed
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
val_idx = defaultdict(list)
for i, n in enumerate(nums):
val_idx[n].append(i)
return sum(sum(i * j % k == 0 for i, j in combinations(lst, 2))
for lst in val_idx.values() if len(lst) > 1)
|
count-equal-and-divisible-pairs-in-an-array
|
Index combinations over equal values, 72ms, 100% speed
|
EvgenySH
| 0
| 42
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,182
|
https://leetcode.com/problems/count-equal-and-divisible-pairs-in-an-array/discuss/1785038/python3-brute-force-solution
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
count=0
n=len(nums)
for i in range(n):
for j in range(i+1,n):
if nums[i]==nums[j] and (i*j)%k==0:
count+=1
return count
|
count-equal-and-divisible-pairs-in-an-array
|
python3 brute force solution
|
Karna61814
| 0
| 10
|
count equal and divisible pairs in an array
| 2,176
| 0.803
|
Easy
| 30,183
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1783425/Python3-1-line
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
return [] if num % 3 else [num//3-1, num//3, num//3+1]
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
[Python3] 1-line
|
ye15
| 5
| 258
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,184
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1783457/Python-3-(40ms)-or-Easy-to-Understand-4-Lines
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
r=[]
if num%3==0:
r.append((num//3)-1)
r.append((num//3))
r.append((num//3)+1)
return r
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Python 3 (40ms) | Easy to Understand 4 Lines
|
MrShobhit
| 2
| 88
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,185
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1783430/Python3-solution-fastest
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num%3==0:
c=num//3
return [c-1,c,c+1]
else:
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Python3 solution fastest
|
Anilchouhan181
| 2
| 60
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,186
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2239767/Python-4-line-solution.
|
class Solution(object):
def sumOfThree(self, num):
"""
:type num: int
:rtype: List[int]
"""
if num%3==0:
l=num//3
return [l-1,l,l+1]
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Python 4 line solution.
|
babashankarsn
| 1
| 18
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,187
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2033835/Very-easy-very-small-python
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num%3!=0:
return
return [(num//3)-1,num//3,(num//3)+1]
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Very easy, very small [python]
|
pbhuvaneshwar
| 1
| 30
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,188
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/1954951/SImple-5-lines-of-code-divide-by-3
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
a = b = c = 0
if num % 3 == 0:
val = num // 3
a = val - 1
b = val
c = val + 1
return [a, b, c] if a + b + c == num else []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
SImple 5 lines of code divide by 3
|
ankurbhambri
| 1
| 33
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,189
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2789943/Simple-Python-Solution
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num % 3 == 0:
n = num // 3
return [n - 1, n, n + 1]
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Simple Python Solution
|
mansoorafzal
| 0
| 2
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,190
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2754507/Simple-Solution
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num%3==0:
x = num//3
return [x-1,x,x+1]
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Simple Solution
|
manishx112
| 0
| 1
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,191
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2651915/Whether-can-be-divided-by-3
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num%3 != 0:
return([])
else:
return([num//3-1,num//3,num//3+1])
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Whether can be divided by 3
|
EdenXiao
| 0
| 3
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,192
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2640637/Simple-Python-Solution%3A-4-Lines
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num == 0: return [-1,0,1]
if num % 3 != 0: return []
x = num // 3
return [x-1, x, x + 1]
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Simple Python Solution: 4 Lines
|
vijay_2022
| 0
| 1
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,193
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2613838/2-liner-Python-Solution-%3A)
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if(num%3!=0):return []
return [num//3-1,num//3,num//3+1]
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
2 liner Python Solution :)
|
vikrxnt
| 0
| 10
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,194
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2466623/Python-for-beginners
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
#Simple Logic :> If num can divisible by 3 than there will be a consecutive three integers else there is no consecutive three integers
#Runtime: 63ms
if(num/3 == num//3):
return [num//3-1,num//3,num//3+1]
else:
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Python for beginners
|
mehtay037
| 0
| 19
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,195
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2380849/easy-python-solution
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num%3 == 0 :
return [(num//3)-1, num//3, (num//3)+1]
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
easy python solution
|
sghorai
| 0
| 15
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,196
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2317978/Python3-Easy-solution-divisible-by-3
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num % 3 == 0:
n = num // 3
return [n-1, n , n+1]
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
[Python3] Easy solution - divisible by 3
|
Gp05
| 0
| 16
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,197
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2244446/Python-oror-Easy-understanding-oror-Simple
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if (num/3-3).is_integer():
n = int(num/3)-1
return [n,n+1,n+2]
return []
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Python || Easy understanding || Simple
|
dyforge
| 0
| 8
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,198
|
https://leetcode.com/problems/find-three-consecutive-integers-that-sum-to-a-given-number/discuss/2242502/Probably-the-easiest-problems-in-Medium-level-right
|
class Solution:
def sumOfThree(self, num: int) -> List[int]:
if num % 3 != 0:
return []
x = num // 3
return [x-1, x, x+1]
|
find-three-consecutive-integers-that-sum-to-a-given-number
|
Probably the easiest problems in Medium level right?
|
MD-ARMAN-Shanto
| 0
| 10
|
find three consecutive integers that sum to a given number
| 2,177
| 0.637
|
Medium
| 30,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.