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/reverse-prefix-of-word/discuss/1942319/Python-One-Liner!
|
class Solution:
def reversePrefix(self, word, ch):
i = word.find(ch)+1
return word[:i][::-1] + word[i:] if i > 0 else word
|
reverse-prefix-of-word
|
Python - One-Liner!
|
domthedeveloper
| 0
| 34
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,900
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1942319/Python-One-Liner!
|
class Solution:
def reversePrefix(self, word, ch):
return (lambda i : word[:i][::-1] + word[i:] if i > 0 else word)(word.find(ch)+1)
|
reverse-prefix-of-word
|
Python - One-Liner!
|
domthedeveloper
| 0
| 34
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,901
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1906789/Python-Solution-or-List-Slicing-and-Reversing-or-Over-90-Faster
|
class Solution:
def reversePrefix(self, s: str, ch: str) -> str:
pos = s.find(ch)
return s[:pos+1][::-1] + s[pos+1:]
|
reverse-prefix-of-word
|
Python Solution | List Slicing and Reversing | Over 90% Faster
|
Gautam_ProMax
| 0
| 29
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,902
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1881304/Python-dollarolution
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
if ch not in word:
return word
x = word.index(ch)+1
return (word[:x][::-1] + word[x:])
|
reverse-prefix-of-word
|
Python $olution
|
AakRay
| 0
| 25
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,903
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1860631/Python-solution-35-ms
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
try:
ind = word.index(ch)+1
except ValueError:
return word
sub = word[0:ind]
return sub[::-1] + word[ind:]
|
reverse-prefix-of-word
|
Python solution 35 ms
|
alishak1999
| 0
| 35
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,904
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1808637/Python-Easy-and-Fast-95
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
if ch not in word:
return word
i=word.index(ch)
a=word[:i+1]
b=word[i+1:]
a=a[::-1]
return a+b
|
reverse-prefix-of-word
|
Python Easy and Fast 95%
|
adityabaner
| 0
| 44
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,905
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1758348/Python-Easy-Sol-oror-Beats-90
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
try:
ri = word.index(ch)
except ValueError:
return word
if ri == len(word)-1:
return word[::-1]
else:
rr = word[:ri+1]
rem = word[ri+1:]
rev = rr[::-1] + rem
return rev
|
reverse-prefix-of-word
|
Python Easy Sol || Beats 90%
|
dos_77
| 0
| 47
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,906
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1700190/Python3-accepted-solution
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
if(ch not in word): return word
return word[:word.index(ch)+1][::-1] + word[word.index(ch)+1:]
|
reverse-prefix-of-word
|
Python3 accepted solution
|
sreeleetcode19
| 0
| 37
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,907
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1683421/Python-one-line-solution-logic-explained
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
return word[0:word.find(ch)+1][::-1] + word[word.find(ch) + 1:]
|
reverse-prefix-of-word
|
Python one line solution, logic explained
|
snagsbybalin
| 0
| 32
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,908
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1555618/Easy-Python3-solution
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
index: int = word.find(ch)
return word if index == -1 else (word[:index + 1][::-1] + word[index + 1:])
|
reverse-prefix-of-word
|
Easy Python3 solution
|
sirenescx
| 0
| 54
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,909
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1534163/Python-Runtime-97%2B-Faster(24ms)-Memory-98%2B-Solution
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
try:
index = word.index(ch)
except:
return word
word = list(word)
part = word[:index+1]
word[:index+1] = part[::-1]
return "".join(word)
|
reverse-prefix-of-word
|
Python Runtime 97%+ Faster(24ms) Memory 98%+ Solution
|
aaffriya
| 0
| 91
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,910
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1511325/Python-1-line
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
return word if ch not in word else ch + word.split(ch)[0][::-1] + word[word.find(ch)+1:]
|
reverse-prefix-of-word
|
Python 1-line
|
SmittyWerbenjagermanjensen
| 0
| 47
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,911
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1461241/Python3-2-line
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
k = word.find(ch)
return word[:k+1][::-1] + word[k+1:]
|
reverse-prefix-of-word
|
[Python3] 2-line
|
ye15
| 0
| 33
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,912
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1461135/Python3-Fastest-Solution-Faster-Than-100-Really-Easy-To-Understand
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
idx = -1
for i in range(len(word)):
if word[i] == ch:
idx = i
break
return word[:idx + 1][::-1] + word[idx + 1:] if idx != -1 else word
|
reverse-prefix-of-word
|
Python3 Fastest Solution, Faster Than 100%, Really Easy To Understand
|
Hejita
| 0
| 44
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,913
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1459918/Python-the-standard-algorithmic-O(N)-solution
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
for i, c in enumerate(word):
if c == ch:
w = list(word)
for j in range((i+1)//2):
w[j], w[i-j] = w[i-j], w[j]
return "".join(w)
return word
|
reverse-prefix-of-word
|
Python, the standard algorithmic O(N) solution
|
blue_sky5
| 0
| 36
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,914
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1459211/Intuitive-approach-by-index
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
try:
i = word.index(ch)
i += len(ch) - 1
return ''.join(word[:i+1][::-1]) + word[i+1:]
except:
return word
|
reverse-prefix-of-word
|
Intuitive approach by index
|
puremonkey2001
| 0
| 24
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,915
|
https://leetcode.com/problems/reverse-prefix-of-word/discuss/1487225/Python-3oror-Faster-than-91-oror-28ms-oror-simple-slicing
|
class Solution:
def reversePrefix(self, word: str, ch: str) -> str:
idx = word.find(ch)
if idx==-1:
return word
else:
res = list(word)
first = res[idx::-1]
last = res[idx+1:]
ans = first+last
return ''.join(ans)
|
reverse-prefix-of-word
|
Python 3|| Faster than 91% || 28ms || simple slicing
|
ana_2kacer
| -1
| 39
|
reverse prefix of word
| 2,000
| 0.778
|
Easy
| 27,916
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/2818774/O(n)-solution-of-Combined-Dictionary-and-Pre-sum-in-Python
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
preSum = []
for rec in rectangles:
preSum.append(rec[1]/rec[0])
dic1 = {}
for i in range(len(preSum)-1, -1, -1):
if preSum[i] not in dic1.keys():
dic1[preSum[i]] = [0,1]
else:
dic1[preSum[i]][0] = dic1[preSum[i]][0] + dic1[preSum[i]][1]
dic1[preSum[i]][1] += 1
return sum ([v[0] for v in dic1.values()])
|
number-of-pairs-of-interchangeable-rectangles
|
O(n) solution of Combined Dictionary and Pre sum in Python
|
DNST
| 0
| 3
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,917
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/2766675/Number-of-pairs-of-interchangeables-Rectangles
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
count=0
d=dict()
for i in range(len(rectangles)):
ratio=rectangles[i][0]/rectangles[i][1]
if ratio not in d:
d[ratio]=1
else:
count+=d[ratio]
d[ratio]+=1
return count
|
number-of-pairs-of-interchangeable-rectangles
|
Number of pairs of interchangeables Rectangles
|
shivansh2001sri
| 0
| 6
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,918
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/2705014/Python-Easy-O(N)-Solution-with-Counter
|
class Solution:
def interchangeableRectangles(self, rectangles: [[int]]) -> int:
counter = Counter([i / j for i, j in rectangles])
ans = 0
for i, j in rectangles:
ans += (counter.get(i / j) - 1)
counter.update({i / j: - 1})
return ans
|
number-of-pairs-of-interchangeable-rectangles
|
Python Easy O(N) Solution with Counter
|
alan_ahmet
| 0
| 13
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,919
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/2545410/4146-TEST-CASES-PASSED.
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
for i in range(0,len(rectangles)):
r=rectangles[i][1]/rectangles[i][0]
rectangles[i]=r
count=0
for j in range(0,len(rectangles)-1):
Slice=rectangles[j+1:]
c=Slice.count(rectangles[j])
count=count+c
return count
|
number-of-pairs-of-interchangeable-rectangles
|
41/46 TEST CASES PASSED.
|
keertika27
| 0
| 11
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,920
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/2452570/Python-3-clean-Solution
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
hashmap , pairs = {} , 0
for i in range(len(rectangles)):
key = rectangles[i][0] / rectangles[i][1]
if key in hashmap:
pairs += hashmap[key]
hashmap[key] += 1
else:
hashmap[key] = 1
return pairs
|
number-of-pairs-of-interchangeable-rectangles
|
Python 3 clean Solution
|
abhisheksanwal745
| 0
| 15
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,921
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1984358/Python-one-line
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
c = Counter(w / h for w, h in rectangles)
return sum(v*(v-1)//2 for v in c.values())
|
number-of-pairs-of-interchangeable-rectangles
|
Python one line
|
SmittyWerbenjagermanjensen
| 0
| 62
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,922
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1984358/Python-one-line
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
return sum(v*(v-1)//2 for v in Counter(w / h for w, h in rectangles).values())
|
number-of-pairs-of-interchangeable-rectangles
|
Python one line
|
SmittyWerbenjagermanjensen
| 0
| 62
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,923
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1461244/Python3-2-line
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
freq = Counter(w/h for w, h in rectangles)
return sum(v*(v-1)//2 for v in freq.values())
|
number-of-pairs-of-interchangeable-rectangles
|
[Python3] 2-line
|
ye15
| 0
| 55
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,924
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1461244/Python3-2-line
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
ans = 0
freq = defaultdict(int)
for w, h in rectangles:
ans += freq[w/h]
freq[w/h] += 1
return ans
|
number-of-pairs-of-interchangeable-rectangles
|
[Python3] 2-line
|
ye15
| 0
| 55
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,925
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1459988/Easy-Python-Solution-using-dictionary
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
diction = defaultdict(int)
n = len(rectangles)
count = 0
for i in range(0, n):
diction[rectangles[i][0]/rectangles[i][1]] += 1
x = list(diction.values())
m = len(x)
for i in range(0, m):
if x[i] > 1:
count += x[i]*(x[i]-1)/2
return int(count)
|
number-of-pairs-of-interchangeable-rectangles
|
Easy Python Solution using dictionary
|
Zikai_Lian
| 0
| 42
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,926
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1458677/JavaPython3-Concise-Solution
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
m = len(rectangles)
n = len(rectangles[0])
res = 0
d = {}
for i in rectangles:
ratio = i[0]/i[1]
if ratio in d:
res += d[ratio]
d[ratio] += 1
else:
d[ratio] = 1
return res
|
number-of-pairs-of-interchangeable-rectangles
|
[Java/Python3] Concise Solution
|
abhijeetmallick29
| 0
| 24
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,927
|
https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/discuss/1458322/Python3-solution-Faster-than-100-less-than-100
|
class Solution:
def interchangeableRectangles(self, rectangles: List[List[int]]) -> int:
counts = {}
for i in range(len(rectangles)):
rectangles[i] = rectangles[i][0]/rectangles[i][1]
if rectangles[i] in counts:
counts[rectangles[i]] += 1
else:
counts[rectangles[i]] = 1
tc = 0
for c in counts:
tc += (counts[c]*(counts[c]-1))//2
return tc
|
number-of-pairs-of-interchangeable-rectangles
|
Python3 solution - Faster than 100%, less than 100%
|
elainefaith0314
| 0
| 40
|
number of pairs of interchangeable rectangles
| 2,001
| 0.451
|
Medium
| 27,928
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/1458484/Python-Bruteforce
|
class Solution:
def maxProduct(self, s: str) -> int:
subs = []
n = len(s)
def dfs(curr, ind, inds):
if ind == n:
if curr == curr[::-1]:
subs.append((curr, inds))
return
dfs(curr+s[ind], ind+1, inds|{ind})
dfs(curr, ind+1, inds)
dfs('', 0, set())
res = 0
n = len(subs)
for i in range(n):
s1, i1 = subs[i]
for j in range(i+1, n):
s2, i2 = subs[j]
if len(i1 & i2) == 0:
res = max(res, len(s1)*len(s2))
return res
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
Python - Bruteforce
|
ajith6198
| 3
| 232
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,929
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/1461446/Python3-bitmask-dp
|
class Solution:
def maxProduct(self, s: str) -> int:
@cache
def lps(mask):
"""Return length of longest palindromic sequence."""
if not mask: return 0
if not mask & (mask-1): return 1
lo = int(log2(mask & ~(mask-1))) # least significant set bit
hi = int(log2(mask)) # most significant set bit
if s[lo] == s[hi]: return 2 + lps(mask^(1<<lo)^(1<<hi))
return max(lps(mask^(1<<lo)), lps(mask^(1<<hi)))
ans = 0
for mask in range(1 << len(s)):
comp = (1 << len(s)) - 1 ^ mask
ans = max(ans, lps(mask) * lps(comp))
return ans
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
[Python3] bitmask dp
|
ye15
| 1
| 62
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,930
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/1458482/PYTHON-Simple-solution-backtracking
|
class Solution:
def maxProduct(self, s: str) -> int:
self.answer = 0
def dfs(i, word, word2):
if i >= len(s):
if word == word[::-1] and word2 == word2[::-1]:
self.answer = max(len(word) * len(word2), self.answer)
return
dfs(i + 1, word + s[i], word2)
dfs(i + 1, word, word2 + s[i])
dfs(i + 1, word, word2)
dfs(0, '', '')
return self.answer
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
[PYTHON] - Simple solution, backtracking✅
|
just_4ina
| 1
| 282
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,931
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/2844906/python-3-Bitmask
|
class Solution:
def maxProduct(self, s: str) -> int:
def create_string(v):
res=[]
for i in range(n):
if 1<<i&v:
res.append(s[i])
if res==res[::-1]:
pal[v]=len(res)
pal=dict()
n=len(s)
for i in range(1,pow(2,n)):
create_string(i)
res=0
for x in pal:
for y in pal:
if not x&y:
res=max(res,pal[x]*pal[y])
return res
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
[python 3] Bitmask
|
gabhay
| 0
| 1
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,932
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/2313099/Python-3-Bitmask
|
class Solution:
def maxProduct(self, s: str) -> int:
def create_string(v):
res=[]
for i in range(n):
if 1<<i&v:
res.append(s[i])
if res==res[::-1]:
pal[v]=len(res)
pal=dict()
n=len(s)
for i in range(1,pow(2,n)):
create_string(i)
res=0
for x in pal:
for y in pal:
if not x&y:
res=max(res,pal[x]*pal[y])
return res
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
[Python 3] Bitmask
|
gabhay
| 0
| 28
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,933
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/1769323/Python3-Bitmask-%2B-DP
|
class Solution:
def maxProduct(self, s: str) -> int:
res, N = 0, len(s)
for i in range((1<<N)-1):
res = max(res, self.mpl(s,i) * self.mpl(s,(1<<N)-1-i))
return res
def mpl(self,s,i): # max palindrome subsequence length
t = ''
for j in range(len(s)):
if i&1:
t = s[len(s) - 1 - j] + t
i >>= 1
if t:
dp = [[0 for _ in range(len(t))] for _ in range(len(t))]
for i in range(len(t)):
dp[i][i] = 1
for l in range(2,len(t)+1):
for i in range(0,len(t) - l + 1):
j = i + l - 1
if t[i] == t[j]:
dp[i][j] = 2 + dp[i+1][j-1]
else:
dp[i][j] = max(dp[i+1][j],dp[i][j-1])
return dp[0][len(t)-1]
return 0
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
[Python3] Bitmask + DP
|
Rainyforest
| 0
| 104
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,934
|
https://leetcode.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/discuss/1467087/Python-dfs%2Bmemo-came-up-in-contest
|
class Solution:
def maxProduct(self, s: str) -> int:
if len(s) < 2:
return 0
if len(s) == 2:
return 1
self.ans = -1
visited = set()
def dfs(arr1, arr2, idx):
if tuple(sorted([arr1, arr2]) + [idx]) in visited:
return
visited.add(tuple(sorted([arr1, arr2]) + [idx]))
if arr1 == arr1[::-1] and arr2 == arr2[::-1]:
self.ans = max(self.ans, len(arr1) * len(arr2))
if idx == len(s):
return
dfs(arr1+s[idx], arr2, idx+1)
dfs(arr1, arr2+s[idx], idx+1)
dfs(arr1, arr2, idx+1)
dfs("", "", 0)
return self.ans
|
maximum-product-of-the-length-of-two-palindromic-subsequences
|
Python dfs+memo came up in contest
|
ScoutBoi
| 0
| 192
|
maximum product of the length of two palindromic subsequences
| 2,002
| 0.533
|
Medium
| 27,935
|
https://leetcode.com/problems/smallest-missing-genetic-value-in-each-subtree/discuss/1461767/Python3-dfs
|
class Solution:
def smallestMissingValueSubtree(self, parents: List[int], nums: List[int]) -> List[int]:
ans = [1] * len(parents)
if 1 in nums:
tree = {}
for i, x in enumerate(parents):
tree.setdefault(x, []).append(i)
k = nums.index(1)
val = 1
seen = set()
while k != -1:
stack = [k]
while stack:
x = stack.pop()
seen.add(nums[x])
for xx in tree.get(x, []):
if nums[xx] not in seen:
stack.append(xx)
seen.add(nums[xx])
while val in seen: val += 1
ans[k] = val
k = parents[k]
return ans
|
smallest-missing-genetic-value-in-each-subtree
|
[Python3] dfs
|
ye15
| 0
| 89
|
smallest missing genetic value in each subtree
| 2,003
| 0.443
|
Hard
| 27,936
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1471015/Python-Clean-and-concise.-Dictionary-T.C-O(N)
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
seen = defaultdict(int)
counter = 0
for num in nums:
tmp, tmp2 = num - k, num + k
if tmp in seen:
counter += seen[tmp]
if tmp2 in seen:
counter += seen[tmp2]
seen[num] += 1
return counter
|
count-number-of-pairs-with-absolute-difference-k
|
[Python] Clean & concise. Dictionary T.C O(N)
|
asbefu
| 36
| 3,700
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,937
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1471015/Python-Clean-and-concise.-Dictionary-T.C-O(N)
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
seen = defaultdict(int)
counter = 0
for num in nums:
counter += seen[num-k] + seen[num+k]
seen[num] += 1
return counter
|
count-number-of-pairs-with-absolute-difference-k
|
[Python] Clean & concise. Dictionary T.C O(N)
|
asbefu
| 36
| 3,700
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,938
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2101510/2-Approach-Python-Solution-O(n)-and-O(n2)
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
hash = {}
for i in nums:
if i in hash:
hash[i] +=1
else:
hash[i] = 1
for i in hash:
if i+k in hash:
count+=hash[i]*hash[i+k]
return count
|
count-number-of-pairs-with-absolute-difference-k
|
2 Approach Python Solution O(n) & O(n2)
|
Brillianttyagi
| 7
| 298
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,939
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2101510/2-Approach-Python-Solution-O(n)-and-O(n2)
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
i = 0
j = len(nums)-1
nums.sort()
while i<len(nums):
if abs(nums[j]-nums[i])==k:
count+=1
j-=1
elif abs(nums[j]-nums[i])<k:
i+=1
j=len(nums)-1
else:
j-=1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
2 Approach Python Solution O(n) & O(n2)
|
Brillianttyagi
| 7
| 298
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,940
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1470878/Python-3-Simple-solution-or-Explained!
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
for i, j in permutations(nums, 2):
if (i - j) == k: count += 1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
[Python 3] Simple solution | Explained!
|
JK0604
| 6
| 880
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,941
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1958493/Python-easy-to-read-and-understand-or-hashmap
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
d = collections.defaultdict(int)
ans = 0
for num in nums:
d[num] = d.get(num, 0) + 1
for key in d:
if (key + k) in d:
ans += d[key] * d[key + k]
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
Python easy to read and understand | hashmap
|
sanial2001
| 3
| 272
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,942
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1633099/Easy-O(n)-Python-Solution.
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
pairs = {}
for num in nums:
if num+k not in pairs:
pairs[num+k] = [0,0]
if num not in pairs:
pairs[num] = [0,0]
pairs[num][0] +=1
pairs[num+k][1] +=1
return sum([pairs[k][0]*pairs[k][1] for k in pairs])
|
count-number-of-pairs-with-absolute-difference-k
|
Easy O(n) Python Solution.
|
manassehkola
| 3
| 316
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,943
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1818715/Ez-to-Understand-oror-hashmap-and-brute-force
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
d={}
co=0
for i in nums:
if i in d:
d[i]+=1
else:
d[i]=1
for i in nums:
if k+i in d:
co=co+d[k+i]
return co
|
count-number-of-pairs-with-absolute-difference-k
|
Ez to Understand || hashmap and brute force
|
ashu_py22
| 2
| 101
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,944
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1818715/Ez-to-Understand-oror-hashmap-and-brute-force
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
co=0
for i in range(len(nums)):
for j in range(0, len(nums)):
if nums[i]+k==nums[j]:
co+=1
return co
|
count-number-of-pairs-with-absolute-difference-k
|
Ez to Understand || hashmap and brute force
|
ashu_py22
| 2
| 101
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,945
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1818715/Ez-to-Understand-oror-hashmap-and-brute-force
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
co=0
for i in range(len(nums)):
if nums[i]+k in nums:
co+=nums.count(nums[i]+k)
return co
|
count-number-of-pairs-with-absolute-difference-k
|
Ez to Understand || hashmap and brute force
|
ashu_py22
| 2
| 101
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,946
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1757985/PYTHON-ONE-LINER
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
return sum(nums.count(num+k) for num in nums if num+k in nums)
|
count-number-of-pairs-with-absolute-difference-k
|
PYTHON ONE-LINER
|
vijayvardhan6
| 2
| 170
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,947
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1574982/Using-counter-99.89-speed
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
cnt = Counter(nums)
return sum(cnt[key] * cnt[key + k]
for key in sorted(cnt.keys()) if key + k in cnt)
|
count-number-of-pairs-with-absolute-difference-k
|
Using counter, 99.89% speed
|
EvgenySH
| 2
| 293
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,948
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2538343/very-easy-python-solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count=0
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if abs(nums[i]-nums[j])==k:
count+=1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
very easy python solution
|
Sneh713
| 1
| 20
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,949
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2285518/Python-or-O(norn)-or-hashmapor-Similar-to-Two-sum
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
hashmap = {}
answer = 0
for i in range(len(nums)):
if nums[i]+k in hashmap.keys():
answer += hashmap[nums[i]+k]
if nums[i]-k in hashmap.keys():
answer += hashmap[nums[i]-k]
if nums[i] not in hashmap.keys():
hashmap[nums[i]] = 1
else:
hashmap[nums[i]] += 1
return answer
|
count-number-of-pairs-with-absolute-difference-k
|
Python | O(n|n) | hashmap| Similar to Two-sum
|
user7457RV
| 1
| 57
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,950
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1721809/Faster-than-99.6.-Defaultdict-O(n)
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
d = defaultdict(int)
for el in nums:
if el - k > 0:
d[el-k] += 1
s = 0
for el in nums:
s += d[el]
return s
|
count-number-of-pairs-with-absolute-difference-k
|
Faster than 99.6%. Defaultdict O(n)
|
mygurbanov
| 1
| 235
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,951
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1470954/Python3-solution-hash-map-102ms-O(N2)
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
nums = sorted(nums)
ans = 0
s = set(nums)
for n in s:
ans += nums.count(n)*nums.count(n+k)
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 solution - hash map, 102ms, O(N^2)
|
elainefaith0314
| 1
| 181
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,952
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2848044/python
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
cnt, ans = Counter(nums), 0
for key in cnt:
if key + k in cnt:
ans += cnt[key] * cnt[key + k]
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
python
|
xy01
| 0
| 1
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,953
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2816372/One-Liner-Python-Solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
return len([True for i in nums for j in nums if i < j and abs(i - j) == k])
|
count-number-of-pairs-with-absolute-difference-k
|
One-Liner Python Solution
|
PranavBhatt
| 0
| 2
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,954
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2815260/Easy-way
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
res=0
for i in nums:
for j in nums:
if i-j==k:
res=res+1
return res
|
count-number-of-pairs-with-absolute-difference-k
|
Easy way
|
nishithakonuganti
| 0
| 1
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,955
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2788652/Simple-Python-Solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
for i in nums:
for j in nums:
if i-j == k:
count = count + 1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
Simple Python Solution
|
dnvavinash
| 0
| 2
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,956
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2723599/Python3-Solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
ans = 0
for i, num in enumerate(nums):
ans += sum([True for n in nums[i:] if abs(num-n) == k])
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 Solution
|
sipi09
| 0
| 8
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,957
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2718428/Python3-O(n)-easy-solution-without-sorting
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
arr = [0] * 101
for num in nums:
arr[num] += 1
ans = 0
for i in range(101):
if i + k > 100:
break
ans += arr[i] * arr[i + k]
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
[Python3] O(n) easy solution without sorting
|
huangweijing
| 0
| 10
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,958
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2640142/Python3-Counter
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
c = Counter(nums)
tot = 0
for low in c:
tot += c[low] * c[low + k]
return tot
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 Counter
|
godshiva
| 0
| 4
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,959
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2559563/simple-python-solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
pairs = 0
for x in range(len(nums)):
for y in range(x+1,len(nums)):
if abs(nums[x]-nums[y]) == k:
pairs += 1
return pairs
|
count-number-of-pairs-with-absolute-difference-k
|
simple python solution
|
maschwartz5006
| 0
| 40
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,960
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2545142/EASY-PYTHON3-SOLUTION
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
for i in range(len(nums)):
for j in range(i,len(nums)):
if abs(nums[i] - nums[j])==k:
count+=1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
🔥 EASY PYTHON3 SOLUTION 🔥
|
rajukommula
| 0
| 34
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,961
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2484944/Easy-hashmap-solution-beats-93
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
dic = {}
count = 0
for i in range(len(nums)):
if nums[i] not in dic:
dic[nums[i]] = 1
else:
dic[nums[i]] += 1
for j in nums:
if j + k in dic:
count += dic[j+k]
return count
|
count-number-of-pairs-with-absolute-difference-k
|
Easy hashmap solution beats 93%
|
aruj900
| 0
| 84
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,962
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2404852/2006.-Count-Number-of-Pairs-With-Absolute-Difference-K%3A-One-liner
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
return sum([1 for number_a in nums for number_b in nums if number_a-number_b==k])
|
count-number-of-pairs-with-absolute-difference-k
|
2006. Count Number of Pairs With Absolute Difference K: One liner
|
rogerfvieira
| 0
| 17
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,963
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2384312/FASTER-Than-50-EASY-Python-Solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
c=0
for i in range(0,len(nums)-1):
for j in range(i+1,len(nums)):
if abs(nums[i]-nums[j])==k: c=c+1
return c
|
count-number-of-pairs-with-absolute-difference-k
|
FASTER Than 50% EASY Python Solution
|
keertika27
| 0
| 33
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,964
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2372950/Python-faster-than-98
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
diffList=[0]*101
maxNum = 0
for i in nums:
diffList[i]+=1
if i > maxNum:
maxNum=i
result = 0
for i in range(maxNum-k+1):
result+=diffList[i]*diffList[i+k]
return result
|
count-number-of-pairs-with-absolute-difference-k
|
Python faster than 98%
|
Shrey-Modi
| 0
| 60
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,965
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2326297/Python3-Solution-with-using-hashmap
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
d = collections.defaultdict(int)
res = 0
for num in nums:
res += d[num - k]
res += d[num + k]
d[num] += 1
return res
|
count-number-of-pairs-with-absolute-difference-k
|
[Python3] Solution with using hashmap
|
maosipov11
| 0
| 46
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,966
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2239513/Python3-brute-force
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
l=[]
for i in range(0, len(nums)):
for j in range(i+1, len(nums)):
if abs(nums[i]-nums[j]) == k:
l.append([nums[i],nums[j]])
return len(l)
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 brute force
|
psnakhwa
| 0
| 13
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,967
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2195256/222-ms-faster-than-62.05-of-Python3
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
for i in range(len(nums)):
for j in range(i,len(nums)):
if abs(nums[i]-nums[j])==k:
count+=1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
222 ms, faster than 62.05% of Python3
|
EbrahimMG
| 0
| 35
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,968
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2182498/Python3-Easy-Peasy
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
n= len(nums)
count=0
for i in range(0,n):
for j in range(i+1,n):
if(abs(nums[i]-nums[j])==k):
count+=1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 Easy Peasy
|
hvt1998
| 0
| 54
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,969
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2178830/Python-simple-solution
|
class Solution:
def countKDifference(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 abs(nums[i]-nums[j]) == k:
ans += 1
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
Python simple solution
|
StikS32
| 0
| 51
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,970
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/2043388/Python3-easy-to-understand
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
count = 0
for i,_ in enumerate(nums):
for j,_ in enumerate(nums):
if nums[i] - nums[j] == k:
count += 1
return count
|
count-number-of-pairs-with-absolute-difference-k
|
[Python3] easy to understand
|
Shiyinq
| 0
| 68
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,971
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1881238/Python3-filter-and-map-solution
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
res = 0
for i in range(len(nums)):
ft = filter(lambda j: j==k, map(lambda n: abs(n-nums[i]), nums[i:]))
res += len([x for x in ft])
else:
return res
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 filter and map solution
|
khRay13
| 0
| 83
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,972
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1851426/2-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-85
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
ans=0
for i in range(len(nums)-1):
for j in range(i+1,len(nums)):
if abs(nums[j]-nums[i])==k: ans+=1
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
2-Lines Python Solution || 50% Faster || Memory less than 85%
|
Taha-C
| 0
| 93
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,973
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1673682/Python3-oror-List-Comprehension
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
return len([(item1, nums[j]) for i, item1 in enumerate(nums) for j in range(i+1, len(nums)) if abs(item1 - nums[j]) == k])
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 || List Comprehension
|
user9295j
| 0
| 120
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,974
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1671829/Python3-O(n)-IF-you-can't-look-forward-just-walk-and-look-behind
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
m, cnt = {nums[0] : 1}, 0
for i in range(1, len(nums)):
# Solution for the equation |x - y| = k
# either x - y = k or x - y = -k
t1, t2 = nums[i] - k, nums[i] + k
if t1 in m and t1 > 0:
cnt += m[t1]
if t2 in m and t2 > 0:
cnt += m[t2]
if nums[i] not in m:
m[nums[i]] = 1
else:
m[nums[i]] += 1
return cnt
|
count-number-of-pairs-with-absolute-difference-k
|
Python3 O(n), IF you can't look forward just walk and look behind
|
Hejita
| 0
| 120
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,975
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1555575/Python3-faster-than-97.93-solutions
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
nums_dict: dict = {}
for num in nums:
if num not in nums_dict:
nums_dict[num] = 1
else:
nums_dict[num] += 1
count: int = 0
for key in nums_dict:
if key - k in nums_dict:
count += nums_dict[key] * nums_dict[key - k]
if k + key in nums_dict:
count += nums_dict[key] * nums_dict[k + key]
return count // 2
|
count-number-of-pairs-with-absolute-difference-k
|
Python3, faster than 97.93% solutions
|
sirenescx
| 0
| 190
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,976
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1521114/Python-from-brute-force-to-linear-time
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
return sum(abs(nums[j] - n) == k for i, n in enumerate(nums) for j in range(i, len(nums)))
|
count-number-of-pairs-with-absolute-difference-k
|
Python, from brute force to linear time
|
mousun224
| 0
| 116
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,977
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1521114/Python-from-brute-force-to-linear-time
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
d = collections.defaultdict(list)
cnt = 0
for i, n in enumerate(nums):
cnt += (len(d[n + k]))
if n > k:
cnt += len(d[n - k])
d[n] += i,
return cnt
|
count-number-of-pairs-with-absolute-difference-k
|
Python, from brute force to linear time
|
mousun224
| 0
| 116
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,978
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1502559/The-most-simple-python-solution%3A-O(n)-time-O(n)-space-with-hash
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
n = len(nums)
h = defaultdict(int)
h[nums[0]] += 1
res = 0
for i in range(1, n):
res += h[nums[i]+k] + h[nums[i]-k]
h[nums[i]] += 1
return res
|
count-number-of-pairs-with-absolute-difference-k
|
The most simple python solution: O(n) time, O(n) space with hash
|
byuns9334
| 0
| 230
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,979
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1471583/Python-O(N)O(N)-solution-with-a-defaultdict-for-indices
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
n_idxs = defaultdict(list)
for j, n in enumerate(nums):
n_idxs[n].append(j)
return sum(sum(j > i for j in n_idxs[n - k]) + sum(j > i for j in n_idxs[n + k])
for i, n in enumerate(nums))
|
count-number-of-pairs-with-absolute-difference-k
|
Python, O(N)/O(N) solution with a defaultdict for indices
|
blue_sky5
| 0
| 59
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,980
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1471581/Python-brute-force-one-liner
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
return sum(abs(nums[i] - nums[j]) == k for i in range(len(nums)) for j in range(i+1, len(nums)))
|
count-number-of-pairs-with-absolute-difference-k
|
Python, brute force one liner
|
blue_sky5
| 0
| 66
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,981
|
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/discuss/1471579/Python3-freq-table
|
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
ans = 0
freq = defaultdict(int)
for x in nums:
ans += freq[x - k] + freq[x + k]
freq[x] += 1
return ans
|
count-number-of-pairs-with-absolute-difference-k
|
[Python3] freq table
|
ye15
| 0
| 70
|
count number of pairs with absolute difference k
| 2,006
| 0.823
|
Easy
| 27,982
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/1470895/Python-Sorting.-Easy-to-understand-and-clean-T.C%3A-O(n-log-n)-S.C%3A-O(N)
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
"""
The idea is to:
1st sort the numbers
2nd Create a counter to save the frequency of each number
3nd iterate the array and for each number check if the double exists.
4rd after taking len(changed) // 2 numbers return the answer
Time complexity: O(nlog(n))
Space complexity: O(n)
"""
if len(changed) % 2 != 0: # If there are not even amount the numbers there is no solution.
return []
changed.sort()
c = Counter(changed) # The counter is needed because we have 0s
answer = []
for num in changed:
if num in c and c[num] >= 1: # Check if the number is available (we may have taken it before)
c[num] -= 1 # we mark the number as used by decreasing the counter (only needed for the zeros)
if (num * 2) in c and c[(num * 2)] >= 1: # Take the one that doubles it if exists
answer.append(num)
c[num*2] -= 1 # The number has been taken.
if len(answer) == len(changed) // 2:
return answer
return []
|
find-original-array-from-doubled-array
|
[Python] Sorting. Easy to understand and clean T.C: O(n log n) S.C: O(N)
|
asbefu
| 13
| 981
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,983
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2578083/python-easy-peasy-solution-using-queue-and-sorting
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
changed.sort()
stk,res=deque([]),[]
for i in changed:
if stk and stk[0]*2==i:
b=stk.popleft()
res.append(b)
else:
stk.append(i)
return res if not stk else []
|
find-original-array-from-doubled-array
|
python easy-peasy solution using queue and sorting
|
benon
| 8
| 589
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,984
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2577455/Python-Solution
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 == 1:
return []
data = Counter(changed)
result = []
for k in sorted(data):
if data[k] < 0:
return []
elif k == 0:
x, y = divmod(data[k], 2)
if y == 1:
return []
result += [0] * x
elif data[k] > 0:
value = k * 2
if data[value] == 0:
return []
min_value = min(value, data[k])
result += [k] * min_value
data[k] -= min_value
data[value] -= min_value
return result
|
find-original-array-from-doubled-array
|
Python Solution
|
hgalytoby
| 3
| 241
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,985
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2577455/Python-Solution
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 == 1:
return []
data = Counter(changed)
result = []
for k in sorted(data):
if data[k] < 0:
return []
value = k * 2
while data[k] > 0:
if data[value] == 0:
return []
result.append(k)
data[k] -= 1
data[value] -= 1
return result
|
find-original-array-from-doubled-array
|
Python Solution
|
hgalytoby
| 3
| 241
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,986
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2577373/Python-or-Math-%2B-Sort-%2B-Counter
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 != 0:
return []
changed.sort(reverse = True)
unpaired = Counter(); original = []
for i, num in enumerate(changed):
if num != 0:
if unpaired[2 * num] > 0:
unpaired[2 * num] -= 1
original.append(num)
elif num % 2 == 0:
unpaired[num] += 1
else:
return []
else:
unpaired[num] += 1
if unpaired[0] % 2 == 0:
original += [0] * (unpaired[0] // 2)
unpaired[0] = 0
return original if all(count == 0 for count in unpaired.values()) else []
|
find-original-array-from-doubled-array
|
Python | Math + Sort + Counter
|
sr_vrd
| 3
| 310
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,987
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2581541/Python3-or-Easy-solution-Explained-or-HashMap-or-97-Faster
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
ll = len(changed)
if ll % 2 != 0: return []
result, hashmap = [], {}
# Push numbers in hashmap {number: count}
for n in changed: hashmap[n] = 1 + hashmap.get(n, 0)
for num in sorted(hashmap.keys()):
if hashmap[num] > 0: # this condition means we already get all count of that number
# 0*2 = 0 so 0 is twice the value of 0 so if the count of so we insert 2 zeros in list as one zero
# if there was 3 zeros we only insert 2 to the result
if num == 0:
# we can put a condition here that if it found a odd count of zeros return emty list
if hashmap[num] % 2 != 0: return []
result += [0] * (hashmap[num] // 2)
hashmap[num] = 0
# if the number anything else
elif hashmap.get(num * 2, False):
if hashmap[num] > hashmap[num*2]: return []
count = hashmap[num]
hashmap[num * 2] -= count
# insert the number with all counts into the result
result += [num] * count
return result if len(result) == ll / 2 else []
|
find-original-array-from-doubled-array
|
Python3 | Easy solution Explained | HashMap | 97% Faster
|
abdoohossamm
| 2
| 53
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,988
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2580151/Python-or-Two-solutions-using-Stack-and-Dictionary-with-explanation-or-Faster-than-95
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 == 1: return []
changed.sort()
res, stack = deque(), deque()
for num in changed:
if stack and num == stack[0] * 2:
res.append(stack.popleft())
else:
stack.append(num)
return [] if stack else res
|
find-original-array-from-doubled-array
|
Python | Two solutions using Stack and Dictionary with explanation | Faster than 95%
|
ahmadheshamzaki
| 2
| 47
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,989
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2580151/Python-or-Two-solutions-using-Stack-and-Dictionary-with-explanation-or-Faster-than-95
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 == 1: return []
changed.sort()
count = Counter(changed)
res = deque()
for num in changed:
if count[num] == 0:
continue
elif count[num * 2] >= 1:
res.append(num)
count[num] -= 1
count[num * 2] -= 1
else:
return []
return res
|
find-original-array-from-doubled-array
|
Python | Two solutions using Stack and Dictionary with explanation | Faster than 95%
|
ahmadheshamzaki
| 2
| 47
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,990
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2595725/Very-Simple-Python-(Self-explanatory)%3A
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed)%2!=0:return []
changed.sort()
c=Counter(changed)
ans=[]
if c[0]%2==0:
ans+=[0]*(c[0]//2)
for i in c:
if i==0 or c[i]==0:
continue
elif (i*2 not in c) or c[i]>c[i*2]:
return []
c[i*2]-=c[i]
ans+=[i]*c[i]
return ans
|
find-original-array-from-doubled-array
|
Very Simple Python (Self explanatory):
|
goxy_coder
| 1
| 73
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,991
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2579404/Python-Simple-Python-Solution-Using-Dictionary-or-Sorting
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2 != 0:
return []
changed = sorted(changed)
result = []
d = {}
for i in changed:
if i not in d:
d[i] = 1
else:
d[i] = d[i] + 1
for num in changed:
double = num * 2
if num in d and double in d:
if num == 0 and d[num] >= 2:
d[num] = d[num] - 2
result.append(num)
elif num > 0 and d[num] and d[double]:
d[num] = d[num] - 1
d[double] = d[double] - 1
result.append(num)
if len(changed) // 2 == len(result):
return result
else:
return []
|
find-original-array-from-doubled-array
|
[ Python ] ✅✅ Simple Python Solution Using Dictionary | Sorting 🥳✌👍
|
ASHOK_KUMAR_MEGHVANSHI
| 1
| 33
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,992
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2578848/Simple-%22python%22-Solution
|
class Solution:
def findOriginalArray(self, changed):
c = Counter(changed)
# int = [0,0,0,0]
if c[0]%2:
return []
for x in sorted(c): # c = [1:1,2:1,3:1,4:1,6:1,8:1]
if c[x] > c[2*x]: # [6,3,4,1] = c [1:1,3:1,4:1,6:1]
return []
c[2*x] -=c[x] if x else c[x]//2
return list(c.elements())
|
find-original-array-from-doubled-array
|
Simple "python" Solution
|
anandchauhan8791
| 1
| 111
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,993
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2578000/simple-python-using-map
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if(len(changed) % 2):
return []
changed = sorted(changed)
h = {}
for i in changed:
if(i not in h):
h[i] = 0
h[i] += 1
ans = []
for i in changed:
if(i == 0):
if(h[i]%2):
return []
else:
count = h[0]//2
while(count):
count -= 1
ans.append(0)
h[0] = 0
elif(i * 2 in h):
while(h[i] > 0 and h[i*2] > 0):
ans.append(i)
h[i] -= 1
h[i*2] -= 1
if(h[i] > 0):
return []
if(len(ans) != len(changed)//2):
return []
return ans
|
find-original-array-from-doubled-array
|
simple python using map
|
jagdishpawar8105
| 1
| 17
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,994
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2577792/python3-or-dictionary-or-explained-or-easy-to-understand
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
n = len(changed)
if n%2==1: return [] # len of changed array = 2*x (x = len of original array)
d={}
for e in changed: d[e] = d.get(e, 0)+1 # using dictionary for optimization
arr = []
changed.sort() # sort the changed list
for e in changed:
if d.get(e, False): # if e in dictionary d
if d.get(e*2, False): # check if 2*e in dictionary also
arr.append(e)
d[e]-=1 # removing those instances from d
d[e*2]-=1
elif e%2==0 and d.get(e//2, False): # check if e//2 in d
arr.append(e//2)
d[e//2]-=1 # removing those instances from d
d[e]-=1
else:
return []
return arr if len(arr) == n//2 else [] # if len arr == len(changes) // 2 then return arr else returning empty list
|
find-original-array-from-doubled-array
|
python3 | dictionary | explained | easy to understand
|
H-R-S
| 1
| 32
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,995
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2101593/EXPLAINED-oror-O(1)-SPACE-oror-O(N-LOGN)-TIME-oror-PYTHON
|
class Solution:
def findOriginalArray(self, a: List[int]) -> List[int]:
if len(a)%2:
return []
a.sort()
w,s,d=0,0,1
while d<len(a):
if a[d]%2==0:
while s<d-1 and a[s]<(a[d]//2):
s+=1
if a[s]==(a[d]//2):
a[ w ] , a[ s ] = a[ s ] , a[ w ]
w+=1
s+=1
a[d]=-1
d+=1
if w==len(a)//2:
return a[:w]
return []
|
find-original-array-from-doubled-array
|
✔️EXPLAINED || ✔️O(1) SPACE || O(N LOGN) TIME || 🐍PYTHON🐍
|
karan_8082
| 1
| 153
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,996
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/1501076/Using-Counter-to-sort-the-keys-95-speed
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
if len(changed) % 2:
return []
ans = []
cnt = Counter(changed)
if 0 in cnt:
if cnt[0] % 2:
return []
else:
ans.extend([0] * (cnt[0] // 2))
cnt.pop(0)
sorted_keys = sorted(cnt.keys())
for key in sorted_keys:
if not cnt:
break
if key in cnt:
if 2 * key in cnt and cnt[key] <= cnt[2 * key]:
ans.extend([key] * cnt[key])
cnt[2 * key] -= cnt[key]
if cnt[2 * key] == 0:
cnt.pop(2 * key)
cnt.pop(key)
else:
return []
return ans
|
find-original-array-from-doubled-array
|
Using Counter to sort the keys, 95% speed
|
EvgenySH
| 1
| 238
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,997
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/1471585/Python3-freq-table
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
freq = Counter(changed)
ans = []
for x in sorted(freq):
if freq[x]:
if x and freq[x] <= freq[2*x]:
ans.extend([x] * freq[x])
freq[2*x] -= freq[x]
elif x == 0 and freq[x] % 2 == 0:
ans.extend([0] * (freq[x]//2))
else: return []
return ans
|
find-original-array-from-doubled-array
|
[Python3] freq table
|
ye15
| 1
| 45
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,998
|
https://leetcode.com/problems/find-original-array-from-doubled-array/discuss/2824173/Python3-or-Sorting-%2B-Hashmap
|
class Solution:
def findOriginalArray(self, changed: List[int]) -> List[int]:
n = len(changed)
if n % 2 != 0:
return []
freq = Counter(changed)
ans = []
changed.sort()
for i in range(n):
if changed[i] % 2 != 0:
if freq[changed[i] * 2] > 0:
freq[changed[i] * 2]-=1
ans.append(changed[i])
else:
return []
else:
if changed[i] == 0:
if freq[changed[i]] > 1:
freq[changed[i]] -= 2
ans.append(changed[i])
elif freq[changed[i]] == 0:
continue
else:
return []
else:
if freq[changed[i]] > 0 and freq[changed[i] * 2] > 0:
freq[changed[i]] -= 1
freq[changed[i] * 2] -= 1
ans.append(changed[i])
return [] if len(ans) != n//2 else ans
|
find-original-array-from-doubled-array
|
[Python3] | Sorting + Hashmap
|
swapnilsingh421
| 0
| 1
|
find original array from doubled array
| 2,007
| 0.409
|
Medium
| 27,999
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.