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/best-poker-hand/discuss/2322339/Simple-Solution-Simulation
class Solution: def bestHand(self, ranks: List[int], suits: List[str]) -> str: r = collections.Counter(ranks) s = collections.Counter(suits) for i in s: if s[i] >= 5: return "Flush" for i in r: if r[i] >= 3: return "Three of a Kind" for i in r: if r[i] >= 2: return "Pair" return "High Card"
best-poker-hand
Simple Solution Simulation
abhijeetmallick29
0
2
best poker hand
2,347
0.606
Easy
32,200
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2322455/Python-oror-Two-pointers-oror-Easy-Approach
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: n = len(nums) ans = 0 i, j = 0, 0 while i <= n - 1: j = 0 if nums[i] == 0: while i + j <= n - 1 and nums[i + j] == 0: j += 1 ans += (j + 1) * j // 2 i = i + j + 1 return ans
number-of-zero-filled-subarrays
✅Python || Two-pointers || Easy Approach
chuhonghao01
2
72
number of zero filled subarrays
2,348
0.571
Medium
32,201
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2333158/Python3-one-liner
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: return reduce(lambda acc,y: (acc[0],0) if y else (sum(acc)+1,acc[1]+1), nums, (0,0))[0]
number-of-zero-filled-subarrays
Python3 one-liner
vsavkin
1
8
number of zero filled subarrays
2,348
0.571
Medium
32,202
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2323148/Python-Solution
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: ans = 0 l = 0 for i in range(len(nums)): if nums[i] == 0: ans += i - l + 1 else: l = i + 1 return ans
number-of-zero-filled-subarrays
Python Solution
user6397p
1
23
number of zero filled subarrays
2,348
0.571
Medium
32,203
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2751973/Three-different-Solution-or-Easy-to-Understand-or-DP-Solution-or-Python
class Solution(object): def zeroFilledSubarray(self, nums): ans, count = 0, 1 for i in range(len(nums)): if nums[i] == 0: ans += count count += 1 else: count = 1 return ans
number-of-zero-filled-subarrays
Three different Solution | Easy to Understand | DP Solution | Python
its_krish_here
0
2
number of zero filled subarrays
2,348
0.571
Medium
32,204
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2751973/Three-different-Solution-or-Easy-to-Understand-or-DP-Solution-or-Python
class Solution(object): def zeroFilledSubarray(self, nums): arr, count = [0] * len(nums), 1 for i in range(len(nums)): if nums[i] == 0: arr[i] = count count += 1 else: count = 1 return sum(arr)
number-of-zero-filled-subarrays
Three different Solution | Easy to Understand | DP Solution | Python
its_krish_here
0
2
number of zero filled subarrays
2,348
0.571
Medium
32,205
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2751973/Three-different-Solution-or-Easy-to-Understand-or-DP-Solution-or-Python
class Solution(object): def zeroFilledSubarray(self, nums): dp = [0] * len(nums) if nums[0] == 0: dp[0] = 1 for i in range(1, len(nums)): if nums[i] == 0: dp[i] = dp[i - 1] + 1 return sum(dp)
number-of-zero-filled-subarrays
Three different Solution | Easy to Understand | DP Solution | Python
its_krish_here
0
2
number of zero filled subarrays
2,348
0.571
Medium
32,206
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2666341/Python-3-O(n)-using-a-Sliding-Window-(faster-than-95-)
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: total_amount = 0 left = 0 for right in range(len(nums)): if nums[right] != 0: left = right + 1 continue if nums[right] == 0: total_amount += right - left + 1 return total_amount
number-of-zero-filled-subarrays
Python 3 O(n) using a Sliding Window (faster than 95 %)
lksmrqrdt
0
15
number of zero filled subarrays
2,348
0.571
Medium
32,207
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2643334/Fast-Python3-or-Single-Iteration-w-Math
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: chain0 = sub0 = 0 for num in nums: if num == 0: chain0 += 1 elif chain0 > 0: sub0 += chain0 * (chain0 + 1) // 2 chain0 = 0 return sub0 + chain0 * (chain0 + 1) // 2
number-of-zero-filled-subarrays
Fast Python3 | Single Iteration w/ Math
ryangrayson
0
9
number of zero filled subarrays
2,348
0.571
Medium
32,208
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2332036/Groupby-zeros-80-speed
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: zeros_groups = [len(list(g)) + 1 for k, g in groupby(nums) if k == 0] return (sum(sum(n - i for n in zeros_groups if n > i) for i in range(1, max(zeros_groups) + 1)) if zeros_groups else 0)
number-of-zero-filled-subarrays
Groupby zeros, 80% speed
EvgenySH
0
10
number of zero filled subarrays
2,348
0.571
Medium
32,209
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2324368/Python3-dp
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: ans = cnt = 0 for x in nums: if x: cnt = 0 else: cnt += 1 ans += cnt return ans
number-of-zero-filled-subarrays
[Python3] dp
ye15
0
8
number of zero filled subarrays
2,348
0.571
Medium
32,210
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2323542/Python3-Groupby-to-count-continuous-0's
class Solution: def zeroFilledSubarray(self, A: List[int]) -> int: ans = 0 for k, v in groupby(A): if k==0: cnt = len(list(v)) ans += cnt*(cnt+1)//2 return ans
number-of-zero-filled-subarrays
[Python3] Groupby to count continuous 0's
zhuzhengyuan824
0
6
number of zero filled subarrays
2,348
0.571
Medium
32,211
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2323390/Python-or-Easy-and-Understanding-Solution-or-One-Pass-Solution
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: n=len(nums) c=0 ans=0 for i in range(n): if(nums[i]!=0): c=0 else: c+=1 ans+=c return ans
number-of-zero-filled-subarrays
Python | Easy & Understanding Solution | One Pass Solution
backpropagator
0
12
number of zero filled subarrays
2,348
0.571
Medium
32,212
https://leetcode.com/problems/number-of-zero-filled-subarrays/discuss/2322335/Python-DP
class Solution: def zeroFilledSubarray(self, nums: List[int]) -> int: temp = [0] * (len(nums) + 1) for index, num in enumerate(nums, start=1): if num == 0: temp[index] = 1 + temp[index - 1] return sum(temp)
number-of-zero-filled-subarrays
[Python] DP
zonda_yang
0
20
number of zero filled subarrays
2,348
0.571
Medium
32,213
https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/discuss/2351553/100-faster-at-my-time-or-easy-python3-solution
class Solution: def shortestSequence(self, rolls: List[int], k: int) -> int: ans = 0 seen = set() for x in rolls: seen.add(x) if len(seen) == k: ans += 1 seen.clear() return ans+1
shortest-impossible-sequence-of-rolls
100% faster at my time | easy python3 solution
vimla_kushwaha
1
40
shortest impossible sequence of rolls
2,350
0.684
Hard
32,214
https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/discuss/2322381/Python-easy-understanding-solution
class Solution: def shortestSequence(self, rolls: List[int], k: int) -> int: s = set() res = 0 for r in rolls: s.add(r) if len(s) == k: # All possible number has appeared once. res += 1 # So you must "at least" use one more place to store it. s.clear() # Clear the set. return res + 1
shortest-impossible-sequence-of-rolls
Python easy understanding solution
byroncharly3
1
26
shortest impossible sequence of rolls
2,350
0.684
Hard
32,215
https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/discuss/2757347/Python-3-Solution
class Solution: def shortestSequence(self, rolls: List[int], k: int) -> int: z = set() res = 0 for x in rolls: z.add(x) if len(z) == k: res += 1 z.clear() return res + 1
shortest-impossible-sequence-of-rolls
Python 3 Solution
mati44
0
1
shortest impossible sequence of rolls
2,350
0.684
Hard
32,216
https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/discuss/2324377/Python3-hash-set
class Solution: def shortestSequence(self, rolls: List[int], k: int) -> int: ans = 1 seen = set() for x in rolls: seen.add(x) if len(seen) == k: ans += 1 seen.clear() return ans
shortest-impossible-sequence-of-rolls
[Python3] hash set
ye15
0
8
shortest impossible sequence of rolls
2,350
0.684
Hard
32,217
https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/discuss/2323029/Python3-One-Pass-oror-Set
class Solution: def shortestSequence(self, rolls: List[int], k: int) -> int: res = 0 s = set() for i in range(len(rolls)): num = rolls[i] if num not in s: s.add(num) if len(s) == k: res+=1 s = set() return res+1
shortest-impossible-sequence-of-rolls
[Python3] One Pass || Set
abhijeetmallick29
0
3
shortest impossible sequence of rolls
2,350
0.684
Hard
32,218
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2324754/Python-oror-Map-oror-Easy-Approach
class Solution: def repeatedCharacter(self, s: str) -> str: setS = set() for x in s: if x in setS: return x else: setS.add(x)
first-letter-to-appear-twice
✅Python || Map || Easy Approach
chuhonghao01
17
1,100
first letter to appear twice
2,351
0.76
Easy
32,219
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2334483/Python3-O(n)-oror-O(1)-Runtime%3A-30ms-90.00-oror-Memory%3A-13.9mb-10.00
class Solution: # O(n) || O(1) # Runtime: 30ms 90.00% || Memory: 13.9mb 10.00% def repeatedCharacter(self, string: str) -> str: strAlphaFreq = [0] * 26 for char in string: index = ord(char) - ord('a') strAlphaFreq[index] += 1 if strAlphaFreq[index] > 1: return char
first-letter-to-appear-twice
Python3 O(n) || O(1) # Runtime: 30ms 90.00% || Memory: 13.9mb 10.00%
arshergon
3
229
first letter to appear twice
2,351
0.76
Easy
32,220
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2324736/Python3-mask
class Solution: def repeatedCharacter(self, s: str) -> str: mask = 0 for ch in s: if mask &amp; 1<<ord(ch)-97: return ch mask ^= 1<<ord(ch)-97
first-letter-to-appear-twice
[Python3] mask
ye15
3
163
first letter to appear twice
2,351
0.76
Easy
32,221
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2671372/Beats-99.86-Python-3
class Solution: def repeatedCharacter(self, s: str) -> str: dicta={} for i in s: if i in dicta: return i else: dicta[i]=1
first-letter-to-appear-twice
Beats 99.86% [Python 3]
Sneh713
2
123
first letter to appear twice
2,351
0.76
Easy
32,222
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2843730/Very-easy
class Solution: def repeatedCharacter(self, s: str) -> str: res =[] for i in s: if i in res: return i break else: res.append(i)
first-letter-to-appear-twice
Very easy
khanismail_1
0
1
first letter to appear twice
2,351
0.76
Easy
32,223
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2833647/Python-solution-using-dictionary.-Return-once-you-see-a-character-that-already-exist-in-the-dict.
class Solution: def repeatedCharacter(self, s: str) -> str: dict_s = {} for ch in s: if ch not in dict_s: dict_s[ch] = 1 else: return ch
first-letter-to-appear-twice
Python solution using dictionary. Return once you see a character that already exist in the dict.
samanehghafouri
0
1
first letter to appear twice
2,351
0.76
Easy
32,224
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2821468/Python-easy-solution
class Solution: def repeatedCharacter(self, s: str) -> str: rech = {} for i in s: if i not in rech: rech[i]=1 else: return i break
first-letter-to-appear-twice
Python easy solution
ameenusyed09
0
1
first letter to appear twice
2,351
0.76
Easy
32,225
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2786230/pyton-easy-solution
class Solution: def repeatedCharacter(self, s: str) -> str: s = list(s); x=[]; for i in s : if i not in x: x.append(i); else: return i[0]
first-letter-to-appear-twice
pyton easy solution
seifsoliman
0
1
first letter to appear twice
2,351
0.76
Easy
32,226
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2767063/Python3-or-Easy-to-understand-or-With-explanation
class Solution: # Time complexity O(n) # Space complexity O(n) def repeatedCharacter(self, s: str) -> str: charSet = set() # Check if each character in input string exists in set, if yes, return the char. # If not, add the char to the set so that you can lookup later if it reoccures. for char in s: if char in charSet: return char charSet.add(char)
first-letter-to-appear-twice
Python3 | Easy to understand | With explanation
sj92
0
3
first letter to appear twice
2,351
0.76
Easy
32,227
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2738456/BEATS-99-OF-THE-SOLUTIONS-PYTHON-EASY-HASHMAP-SOLUTION
class Solution: def repeatedCharacter(self, s: str) -> str: dic = {} ss = '' for i, w in enumerate(s): if w in dic: dic[w].append(i) else: dic[w] = [i] mi = 10000 for key, value in dic.items(): if len(value)>=2: if value[1]<mi: mi = value[1] ss = key return ss
first-letter-to-appear-twice
BEATS 99% OF THE SOLUTIONS - PYTHON EASY HASHMAP SOLUTION
jacobsimonareickal
0
1
first letter to appear twice
2,351
0.76
Easy
32,228
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2726745/Python3-Solution-with-using-hashset
class Solution: def repeatedCharacter(self, s: str) -> str: seen = set() for char in s: if char in seen: return char seen.add(char) return None
first-letter-to-appear-twice
[Python3] Solution with using hashset
maosipov11
0
3
first letter to appear twice
2,351
0.76
Easy
32,229
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2711325/Python-solution-clean-code-with-comments.
class Solution: def repeatedCharacter(self, s: str) -> str: return str_to_dict(s) def str_to_dict(s: str): dic = {} for i in s: if i in dic: dic[i] += 1 # Return the first character that appear twice. if dic[i] == 2: return str(i) else: dic[i] = 1 return None # Runtime: 49 ms, faster than 57.39% of Python3 online submissions for First Letter to Appear Twice. # Memory Usage: 13.9 MB, less than 50.91% of Python3 online submissions for First Letter to Appear Twice. # If you like my work and found it helpful, then I'll appreciate a like. Thanks!
first-letter-to-appear-twice
Python solution, clean code with comments.
375d
0
10
first letter to appear twice
2,351
0.76
Easy
32,230
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2683239/100-EASY-TO-UNDERSTANDSIMPLECLEAN
class Solution: def repeatedCharacter(self, s: str) -> str: seenAlready = [] for c in s: if c not in seenAlready: seenAlready.append(c) else: return c
first-letter-to-appear-twice
🔥100% EASY TO UNDERSTAND/SIMPLE/CLEAN🔥
YuviGill
0
52
first letter to appear twice
2,351
0.76
Easy
32,231
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2644635/First-Letter-to-Appear-Twice-oror-Python3-oror-Dictionary
class Solution: def repeatedCharacter(self, s: str) -> str: d={} for i in s: if i in d: d[i]+=1 if d[i]==2: return i else: d[i]=1
first-letter-to-appear-twice
First Letter to Appear Twice || Python3 || Dictionary
shagun_pandey
0
5
first letter to appear twice
2,351
0.76
Easy
32,232
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2643076/Python-Code-oror-Simplest-solution
class Solution: def repeatedCharacter(self, s: str) -> str: stack=[] for i in range(len(s)): if s[i] in stack: return s[i] else: stack.append(s[i])
first-letter-to-appear-twice
Python Code || Simplest solution
sinjan_singh
0
1
first letter to appear twice
2,351
0.76
Easy
32,233
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2625098/dictionary-solution-very-easy!!
class Solution: def repeatedCharacter(self, s: str) -> str: dict={} for i in s: if i not in dict: dict[i]=1 else: return i
first-letter-to-appear-twice
dictionary solution very easy!!
lin11116459
0
9
first letter to appear twice
2,351
0.76
Easy
32,234
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2571956/Python-Simple-Python-Solution-Using-Dictionary
class Solution: def repeatedCharacter(self, s: str) -> str: d = {} result = '' min_index = 1000000 for index in range(len(s)): if s[index] not in d: d[s[index]] = [index] else: d[s[index]].append(index) for key in d: if len(d[key]) > 1: second_index = d[key][1] if second_index < min_index: result = key min_index = min(min_index, second_index) return result
first-letter-to-appear-twice
[ Python ] ✅✅ Simple Python Solution Using Dictionary 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
35
first letter to appear twice
2,351
0.76
Easy
32,235
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2555561/Python-95.9-Faster.-Easy-to-Follow-Comments
class Solution: def repeatedCharacter(self, s: str) -> str: #var to hold the output value ovalue = 100 #create a unique list of values ul = set(list(s)) #iterate throught the unique letters for letter in ul: #check if letter count is greater than 2 in the string if s.count(letter) >=2: #if more than 2, check second position, set ovalue if lower than previous value if ovalue > (s.find(letter) + s[s.find(letter)+1::].find(letter) + 1): ovalue = (s.find(letter) + s[s.find(letter)+1::].find(letter) + 1) #return index position within the string return(s[ovalue])
first-letter-to-appear-twice
Python 95.9% Faster. Easy to Follow Comments
ovidaure
0
30
first letter to appear twice
2,351
0.76
Easy
32,236
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2534660/simple-clean
class Solution: def repeatedCharacter(self, s: str) -> str: # use a hashmap to store the seen characters # if the char is in the hashmap, we've seen it once so... # return that character # Time O(N) Space O(N) seen = dict() for char in s: if char in seen: return char else: seen[char] = None
first-letter-to-appear-twice
simple clean
andrewnerdimo
0
26
first letter to appear twice
2,351
0.76
Easy
32,237
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2490955/Faster-than-90.08-of-Python3
class Solution: def repeatedCharacter(self, s: str) -> str: s=tuple(s) s2=tuple() for letter in s: if letter in s2: return letter s2=s2+tuple(letter)
first-letter-to-appear-twice
Faster than 90.08% of Python3
1_d99
0
17
first letter to appear twice
2,351
0.76
Easy
32,238
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2489304/Simple-solution-using-set
class Solution: def repeatedCharacter(self, s: str) -> str: seen = set() for i in s: if i in seen: return i else: seen.add(i)
first-letter-to-appear-twice
Simple solution using set
aruj900
0
18
first letter to appear twice
2,351
0.76
Easy
32,239
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2479030/Python-code-faster-than-92.23-of-online-submissions
class Solution: def repeatedCharacter(self, s: str) -> str: a=[] for i in s: if(i in a): return i break else: a.append(i)
first-letter-to-appear-twice
Python code faster than 92.23% of online submissions
Durgavamsi
0
23
first letter to appear twice
2,351
0.76
Easy
32,240
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2468907/Python-O(n)-time-complexity-O(1)-space-complexity-using-hashMap.
class Solution: def repeatedCharacter(self, s): set1 = set() # O(1) space complexity for i in range(len(s)): # O(n) if s[i] in set1: # O(1) return s[i] set1.add(s[i]) # O(1)
first-letter-to-appear-twice
Python O(n) time complexity, O(1) space complexity using hashMap.
OsamaRakanAlMraikhat
0
24
first letter to appear twice
2,351
0.76
Easy
32,241
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2436060/Simple-array-append-Python-80-faster
class Solution: def repeatedCharacter(self, s: str) -> str: a = [] for i in s: if i not in a: a.append(i) else: return i
first-letter-to-appear-twice
Simple array append Python 80% faster
amit0693
0
12
first letter to appear twice
2,351
0.76
Easy
32,242
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2435539/Solution-Using-List-or-Python
class Solution: def repeatedCharacter(self, s: str) -> str: for i in range(len(s)): if s[i] in s[:i]: return s[i]
first-letter-to-appear-twice
Solution Using List | Python
Abhi_-_-
0
19
first letter to appear twice
2,351
0.76
Easy
32,243
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2422049/Python-Bitmask
class Solution: def repeatedCharacter(self, s: str) -> str: bitmask = 0 i = 0 while i < 0 + len(s): offset = ord(s[i]) - ord('a') seen = bitmask &amp; (1 << offset) now = 1 << offset if seen and now: return s[i] else: bitmask |= now i += 1
first-letter-to-appear-twice
[Python] Bitmask
DG_stamper
0
9
first letter to appear twice
2,351
0.76
Easy
32,244
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2367254/Simple-hashset-python-fast-solution
class Solution: def repeatedCharacter(self, s: str) -> str: h=set() for i in s: if i in h: return i else: h.add(i)
first-letter-to-appear-twice
Simple hashset python fast solution
sunakshi132
0
36
first letter to appear twice
2,351
0.76
Easy
32,245
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2352976/easy-python-solution
class Solution: def repeatedCharacter(self, s: str) -> str: unique_letter = [] for i in range(len(s)) : if s[i] not in unique_letter : unique_letter.append(s[i]) else : return s[i]
first-letter-to-appear-twice
easy python solution
sghorai
0
33
first letter to appear twice
2,351
0.76
Easy
32,246
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2347771/python-Solution-O(n)
class Solution: def repeatedCharacter(self, s: str) -> str: d = {} for i in s: if i not in d: d[i] = d.get(i, 0) +1 else: return i
first-letter-to-appear-twice
python Solution O(n)
Tobi_Akin
0
24
first letter to appear twice
2,351
0.76
Easy
32,247
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2344721/Python-simple-solution
class Solution: def repeatedCharacter(self, s: str) -> str: sett = set() for i in s: if i in sett: return i else: sett.add(i)
first-letter-to-appear-twice
Python simple solution
StikS32
0
30
first letter to appear twice
2,351
0.76
Easy
32,248
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2335499/Python-or-Simple-Solution-or-Hashmap
class Solution: def repeatedCharacter(self, s: str) -> str: hash_map = {} for char in s: hash_map[char] = hash_map.get(char, 0) + 1 # when the freq of any char becomes greater than 1 simply return that character if hash_map[char] > 1: return i
first-letter-to-appear-twice
✅ Python | Simple Solution | Hashmap
Nk0311
0
24
first letter to appear twice
2,351
0.76
Easy
32,249
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2332101/Python-easy-solution-for-beginners-using-Sets
class Solution: def repeatedCharacter(self, s: str) -> str: seen = set() for i in s: if i in seen: return i else: seen.add(i)
first-letter-to-appear-twice
Python easy solution for beginners using Sets
alishak1999
0
13
first letter to appear twice
2,351
0.76
Easy
32,250
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2329012/Python-100-Memory
class Solution: def repeatedCharacter(self, s: str) -> str: for i in range(len(s)): if s[i] in s[:i]: # O(n) worse case, O(1) average return s[i]
first-letter-to-appear-twice
Python 100% Memory
heyitsmass
0
8
first letter to appear twice
2,351
0.76
Easy
32,251
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2327695/One-loop-one-set-80-speed
class Solution: def repeatedCharacter(self, s: str) -> str: seen = set() for c in s: if c in seen: return c seen.add(c) return ""
first-letter-to-appear-twice
One loop, one set, 80% speed
EvgenySH
0
22
first letter to appear twice
2,351
0.76
Easy
32,252
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2327077/Python-one-for-loop
class Solution: def repeatedCharacter(self, s: str) -> str: letters = [0] * 26 for c in s: idx = ord(c) - ord('a') if letters[idx]: return c letters[idx] = 1
first-letter-to-appear-twice
Python, one for loop
blue_sky5
0
12
first letter to appear twice
2,351
0.76
Easy
32,253
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2326389/SIMPLE-PYTHON-SOLUTION
class Solution: def repeatedCharacter(self, s: str) -> str: arr=[] for i in s: if i not in arr: arr.append(i) else: return i
first-letter-to-appear-twice
SIMPLE PYTHON SOLUTION
vijayvardhan6
0
14
first letter to appear twice
2,351
0.76
Easy
32,254
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2325687/PYTHON-EASY-IMPLEMENTATION-oror-DICTIONARY
class Solution: def repeatedCharacter(self, s: str) -> str: d = {} for idx, ch in enumerate(s): if ch not in d: d[ch] = [idx] else: d[ch].append(idx) curr = float("inf") for k,v in d.items(): if len(v) > 1: curr = min(curr, min(v[1:])) return s[curr]
first-letter-to-appear-twice
PYTHON EASY IMPLEMENTATION || DICTIONARY
varunshrivastava2706
0
5
first letter to appear twice
2,351
0.76
Easy
32,255
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2325586/Python-3-or-Hash_Map
class Solution: def repeatedCharacter(self, s: str) -> str: hash_map = {s[0]:1} for i in range(1,len(s)): if s[i] not in hash_map.keys(): hash_map[s[i]] = 1 else: return s[i]
first-letter-to-appear-twice
Python 3 | Hash_Map
user7457RV
0
9
first letter to appear twice
2,351
0.76
Easy
32,256
https://leetcode.com/problems/first-letter-to-appear-twice/discuss/2383591/For-beginners-99.98-faster-in-python-O(n)-time
class Solution: def repeatedCharacter(self, s: str) -> str: l = [] for i in s: if i in l: return i else: l.append(i)
first-letter-to-appear-twice
For beginners 99.98% faster in python O(n) time
yashsanghani310
-1
37
first letter to appear twice
2,351
0.76
Easy
32,257
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2328910/Python3-oror-3-lines-transpose-Ctr-w-explanation-oror-TM%3A-100100
class Solution: # Consider this grid for an example: # grid = [[1,2,1,9] # [2,8,9,2] # [1,2,1,9] # [9,2,6,3]] # Here's the plan: # • Determine tpse, the transpose of grid (using zip(*grid)): # tspe = [[1,2,1,9] # [2,8,2,2] # [1,9,1,6] # [9,2,9,3]] # The problem now is to determine the pairs of # identical rows, one row in tpse and the other in grid. # • We hash grid and tspe: # # Counter(tuple(grid)): # {(1,2,1,9): 2, (2,8,9,2): 1, (9,2,6,3): 1} # # Counter(zip(*grid)): # {(1,2,1,9): 1, (2,8,2,2): 1, (1,9,1,6): 1, (9,2,9,3): 1} # # • We determine the number of pairs: # (1,2,1,9): 2 and (1,2,1,9): 1 => 2x1 = 2 def equalPairs(self, grid: List[List[int]]) -> int: tpse = Counter(zip(*grid)) # <-- determine the transpose # and hash the rows grid = Counter(map(tuple,grid)) # <-- hash the rows of grid. (Note the tuple-map, so # we can compare apples w/ apples in next step.) return sum(tpse[t]*grid[t] for t in tpse) # <-- compute the number of identical pairs # https://leetcode.com/submissions/detail/755717162/
equal-row-and-column-pairs
Python3 || 3 lines, transpose, Ctr w/ explanation || T/M: 100%/100%
warrenruud
12
443
equal row and column pairs
2,352
0.71
Medium
32,258
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2643204/Python-Elegant-and-Short-or-O(n3)-vs-O(n2)-or-Hashing
class Solution: """ Time: O(n^3) Memory: O(1) """ def equalPairs(self, grid: List[List[int]]) -> int: n = len(grid) pairs = 0 for i in range(n): for j in range(n): for k in range(n): if grid[i][k] != grid[k][j]: break else: pairs += 1 return pairs
equal-row-and-column-pairs
Python Elegant & Short | O(n^3) vs O(n^2) | Hashing
Kyrylo-Ktl
4
94
equal row and column pairs
2,352
0.71
Medium
32,259
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2643204/Python-Elegant-and-Short-or-O(n3)-vs-O(n2)-or-Hashing
class Solution: """ Time: O(n^2) Memory: O(n) """ def equalPairs(self, grid: List[List[int]]) -> int: mp = defaultdict(int) for col in zip(*grid): mp[self.serialize(col)] += 1 return sum(mp[self.serialize(row)] for row in grid) @staticmethod def serialize(nums: Generator) -> str: return ','.join(map(str, nums))
equal-row-and-column-pairs
Python Elegant & Short | O(n^3) vs O(n^2) | Hashing
Kyrylo-Ktl
4
94
equal row and column pairs
2,352
0.71
Medium
32,260
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2324772/Python3-freq-table-O(N2)
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: freq = Counter(tuple(row) for row in grid) return sum(freq[tuple(col)] for col in zip(*grid))
equal-row-and-column-pairs
[Python3] freq table O(N^2)
ye15
2
129
equal row and column pairs
2,352
0.71
Medium
32,261
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2324775/Python-oror-Easy-Approach
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: n = len(grid) ans = 0 for i in range(n): res = [] for j in range(n): res = [] for k in range(n): res.append(grid[k][j]) if res == grid[i]: ans += 1 return ans
equal-row-and-column-pairs
✅Python || Easy Approach
chuhonghao01
1
70
equal row and column pairs
2,352
0.71
Medium
32,262
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2847926/Py-Hash-O(N2)
class Solution: def equalPairs(self, arr: List[List[int]]) -> int: def convert_key(arr): return tuple(arr) dic = defaultdict(int) for row in arr: dic[convert_key(row)] += 1 dic2 = defaultdict(int) for column in range(len(arr)): temp = [] for row in arr: temp.append(row[column]) dic2[convert_key(temp)] += 1 ans = 0 for item in dic: ans += dic[item] * dic2[item] return ans
equal-row-and-column-pairs
Py Hash O(N^2)
haly-leshchuk
0
1
equal row and column pairs
2,352
0.71
Medium
32,263
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2834658/solution-using-4-dictionaries
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: from collections import defaultdict mydict1 = defaultdict(list) #O(N) mydict2 = defaultdict(list) #O(N) for i in range(len(grid)): for j in range(len(grid[i])): mydict1[i].append(grid[i][j]) mydict2[j].append(grid[i][j]) d1 = defaultdict(int) d2 = defaultdict(int) for k1,v1 in mydict1.items(): d1[tuple(v1)]+=1 for k2,v2 in mydict2.items(): d2[tuple(v2)]+=1 nIdentical =0 for k in d1: nIdentical += d1[k]*d2[k] return nIdentical
equal-row-and-column-pairs
solution using 4 dictionaries
user5580aS
0
2
equal row and column pairs
2,352
0.71
Medium
32,264
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2760969/python-O(n*n)-T.C
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: #row and col hash-maps row = {} col = {} n = len(grid) #extracting the rows for i in grid: if tuple(i) in row: row[tuple(i)] += 1 else: row[tuple(i)] = 1 #extracting the coloumns for j in range(0 , n): temp = [] for i in range(0 , n): temp.append(grid[i][j]) if tuple(temp) in col: col[tuple(temp)] += 1 else: col[tuple(temp)] = 1 pairs = 0 #finding out the no of pairs for i in row: if i in col: pairs += row[i]*col[i] return pairs
equal-row-and-column-pairs
python O(n*n) T.C
akashp2001
0
2
equal row and column pairs
2,352
0.71
Medium
32,265
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2758735/Python-easy-to-read-and-understand
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: n = len(grid) rows = grid cols = [] for i in range(n): temp = [] for j in range(n): temp.append(grid[j][i]) cols.append(temp) res = 0 for row in rows: for col in cols: #print(row, col) if row == col: res += 1 return res
equal-row-and-column-pairs
Python easy to read and understand
sanial2001
0
4
equal row and column pairs
2,352
0.71
Medium
32,266
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2756177/Python3-Easy-O(n2)-Solution-using-Hashing
class Solution: MOD = 10 ** 9 + 7 BASE = 10 ** 5 + 1 def equalPairs(self, grid: List[List[int]]) -> int: row_arr, col_arr = [0] * len(grid), [0] * len(grid) for i, row in enumerate(grid): for j, col in enumerate(row): row_arr[i] = (row_arr[i] * Solution.BASE + col) % Solution.MOD col_arr[j] = (col_arr[j] * Solution.BASE + col) % Solution.MOD ans = 0 for row_sum in row_arr: for col_sum in col_arr: if row_sum == col_sum: ans += 1 return ans
equal-row-and-column-pairs
[Python3] Easy O(n^2) Solution using Hashing
huangweijing
0
4
equal row and column pairs
2,352
0.71
Medium
32,267
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2438979/Python3-Solution-with-using-hashmap
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: d = collections.defaultdict(int) for row in grid: d[tuple(row)] += 1 res = 0 for j in range(len(grid[0])): res += d[tuple([grid[i][j] for i in range(len(grid[0]))])] return res
equal-row-and-column-pairs
[Python3] Solution with using hashmap
maosipov11
0
23
equal row and column pairs
2,352
0.71
Medium
32,268
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2429015/easy-python-solution
class Solution: def makeColumns(self, grid) : output = [] for i in range(len(grid)) : col = [] for j in range(len(grid)) : col.append(grid[j][i]) output.append(col) return output def equalPairs(self, grid: List[List[int]]) -> int: cols = self.makeColumns(grid) count = 0 for col in cols : if col in grid : count += grid.count(col) return count
equal-row-and-column-pairs
easy python solution
sghorai
0
15
equal row and column pairs
2,352
0.71
Medium
32,269
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2409504/Easy-python-solution
class Solution: def equalPairs(self, rows: List[List[int]]) -> int: columns = [] rows_length = len(rows) cols_length = len(rows[0]) for i in range(0, rows_length): res = [] for j in range(0, cols_length): res.append(rows[j][i]) columns.append(res) #print(columns) ans = 0 for i in columns: for j in rows: if i == j: ans+=1 return ans
equal-row-and-column-pairs
Easy python solution 💯
EbrahimMG
0
23
equal row and column pairs
2,352
0.71
Medium
32,270
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2351472/Python-HashMap-or-beats-90-by-runtime-or-100-by-memory
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: s_rows = defaultdict(int) for i in range(len(grid)): row = " ".join([str(g) for g in grid[i]]) s_rows[row] += 1 answer = 0 for i in range(len(grid[0])): col = " ".join([str(grid[j][i]) for j in range(len(grid))]) if col in s_rows: answer += s_rows[col] return answer
equal-row-and-column-pairs
Python HashMap | beats 90% by runtime | 100% by memory
pivovar3al
0
62
equal row and column pairs
2,352
0.71
Medium
32,271
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2332084/Python-easy-solution-for-beginners-memory-less-than-100
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: res = 0 transpose = list(list(x) for x in zip(*grid)) for i in grid: if i in transpose: res += transpose.count(i) return res
equal-row-and-column-pairs
Python easy solution for beginners, memory less than 100%
alishak1999
0
35
equal row and column pairs
2,352
0.71
Medium
32,272
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2331851/Counter-of-tuples-90-speed
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: cnt = Counter(tuple(g) for g in grid) return sum(cnt[row] for row in zip(*grid))
equal-row-and-column-pairs
Counter of tuples, 90% speed
EvgenySH
0
13
equal row and column pairs
2,352
0.71
Medium
32,273
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2329225/Python3-faster-than-100
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: new_arr=[] n=len(grid) for i in range(n): new_arr.append([0]*n) for i in range(n): for j in range(n): new_arr[i][j]=grid[j][i] d={} for i in grid: if tuple(i) in d: d[tuple(i)]+=1 else: d[tuple(i)]=1 ans=0 for i in new_arr: if tuple(i) in d: ans+=d[tuple(i)] return ans
equal-row-and-column-pairs
Python3 faster than 100%
svr300
0
14
equal row and column pairs
2,352
0.71
Medium
32,274
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2328906/python-hashmap-easy-understand
class Solution(object): def equalPairs(self, grid): """ :type grid: List[List[int]] :rtype: int """ m, n = len(grid), len(grid[0]) dic = defaultdict(int) res = 0 for i in range(m): cur = [] for j in range(n): cur.append(str(grid[i][j])) dic[','.join(cur)] += 1 for j in range(n): cur = [] for i in range(m): cur.append(str(grid[i][j])) if ','.join(cur) in dic: res += dic[','.join(cur)] return res
equal-row-and-column-pairs
python hashmap easy understand
Kennyyhhu
0
31
equal row and column pairs
2,352
0.71
Medium
32,275
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2328801/Python-2-lines
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: colCounter = Counter(zip(*grid)) return sum(colCounter[row] for row in map(tuple, grid))
equal-row-and-column-pairs
Python 2 lines
SmittyWerbenjagermanjensen
0
18
equal row and column pairs
2,352
0.71
Medium
32,276
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2326526/Python-Easy-Understanding-Solution
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: m = n = len(grid) res = 0 for i in range(n): for j in range(n): tmp = [grid[k][j] for k in range(n)] if grid[i] == tmp: res += 1 return res
equal-row-and-column-pairs
Python Easy Understanding Solution
blazers08
0
6
equal row and column pairs
2,352
0.71
Medium
32,277
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2325728/PYTHON-3-BRUTE-FORCE-SOLUTION
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: col = list(list(item) for item in zip(*grid)) count = 0 for arr in grid: for arr1 in col: if arr == arr1: count += 1 return count
equal-row-and-column-pairs
PYTHON 3 BRUTE FORCE SOLUTION
varunshrivastava2706
0
5
equal row and column pairs
2,352
0.71
Medium
32,278
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2325260/Intutive-Python-solution
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: cols = [] for c in range(len(grid[0])): temp = [] for r in range(len(grid)): temp.append(grid[r][c]) cols.append(temp) count = 0 for r in grid: for c in cols: if r==c: count+=1 return count
equal-row-and-column-pairs
Intutive Python solution
pradeep288
0
23
equal row and column pairs
2,352
0.71
Medium
32,279
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2325119/Python3-Easy-hashmap-solution
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) check = collections.defaultdict(int) for row in grid: s = "-".join(map(str, row)) check[s] += 1 ans = 0 for col in range(n): arr = [] for r in range(m): arr.append(grid[r][col]) s = "-".join(map(str, arr)) if s in check: ans += check[s] return ans
equal-row-and-column-pairs
[Python3] Easy hashmap solution
nightybear
0
21
equal row and column pairs
2,352
0.71
Medium
32,280
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2324778/Python-or-Counting
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: rows = grid.copy() cols = [] N = len(grid) for c in range(N): each_col = [] for r in range(N): each_col.append(grid[r][c]) cols.append(each_col) print(rows, cols) count = 0 for r in rows: for c in cols: if r == c: count += 1 return count
equal-row-and-column-pairs
[Python] | Counting
tejeshreddy111
0
18
equal row and column pairs
2,352
0.71
Medium
32,281
https://leetcode.com/problems/equal-row-and-column-pairs/discuss/2324753/Python3-Prefix-Tree-Solution
class Solution: def equalPairs(self, grid: List[List[int]]) -> int: root = {} n = len(grid) for i in range(n): tmp = root for j in range(n): if grid[i][j] not in tmp: tmp[grid[i][j]] = {} tmp = tmp[grid[i][j]] if '#' in tmp: tmp['#'] += 1 else: tmp['#'] = 1 numPair = 0 for j in range(n): tmp = root for i in range(n): if grid[i][j] not in tmp: break tmp = tmp[grid[i][j]] if '#' in tmp: numPair += tmp['#'] return numPair
equal-row-and-column-pairs
Python3 Prefix Tree Solution
TongHeartYes
0
6
equal row and column pairs
2,352
0.71
Medium
32,282
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2324641/Python3-Sorting-Hamming-Weights-%2B-Binary-Search-With-Detailed-Explanations
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: hamming = sorted([self.hammingWeight(num) for num in set(nums)]) ans = 0 for h in hamming: ans += len(hamming) - bisect.bisect_left(hamming, k - h) return ans def hammingWeight(self, n): ans = 0 while n: n &amp;= (n - 1) ans += 1 return ans
number-of-excellent-pairs
[Python3] Sorting Hamming Weights + Binary Search With Detailed Explanations
xil899
20
674
number of excellent pairs
2,354
0.458
Hard
32,283
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2324641/Python3-Sorting-Hamming-Weights-%2B-Binary-Search-With-Detailed-Explanations
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: hamming = sorted([num.bit_count() for num in set(nums)]) ans = 0 for h in hamming: ans += len(hamming) - bisect.bisect_left(hamming, k - h) return ans
number-of-excellent-pairs
[Python3] Sorting Hamming Weights + Binary Search With Detailed Explanations
xil899
20
674
number of excellent pairs
2,354
0.458
Hard
32,284
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2324641/Python3-Sorting-Hamming-Weights-%2B-Binary-Search-With-Detailed-Explanations
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: hamming = Counter([num.bit_count() for num in set(nums)]) ans = 0 for h1 in hamming: for h2 in hamming: if h1 + h2 < k: continue ans += hamming[h1] * hamming[h2] return ans
number-of-excellent-pairs
[Python3] Sorting Hamming Weights + Binary Search With Detailed Explanations
xil899
20
674
number of excellent pairs
2,354
0.458
Hard
32,285
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2353661/python-or-O(nlogn)-sorting-%2B-binary-search-solution-explained
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: # Count the Number of Bits in Each Unique Number - O(n) # Tally the Number of Times Each Bit Count Occurs - O(n) # Sort the (bit count, tally) pairs by bit count - O(nlogn) counts = sorted(Counter(map(int.bit_count, set(nums))).items()) # (I am fully aware that this line of code is really doing too much work) # Compute the Reversed Prefix Sum of the Tallies (i.e. sums[i] is how many numbers have at least counts[i][0] '1' bits) - O(n) sums = [0]*len(counts) sums[-1] = counts[-1][1] for i in range(len(sums) - 2, -1, -1): sums[i] += counts[i][1] + sums[i + 1] # Choose Each Number as the First Number of a Pair - O(nlogn) pairs = 0 for n, c in counts: # Find the Smallest Number Which Forms a Valid Pair - O(logn) i = bisect_left(counts, k - n, key = lambda x: x[0]) # Check if Any Pairs Can be Formed if i < len(sums): # Compute the Number of Pairs Which Start With the Given Collection of Numbers pairs += c*sums[i] # Return the Number of Pairs return pairs
number-of-excellent-pairs
python | O(nlogn) sorting + binary search solution explained
queenTau
4
100
number of excellent pairs
2,354
0.458
Hard
32,286
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2472142/Python-Solution-or-Brute-Force-or-Optimized-or-O(N)
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: count=0 n=len(nums) # Brute Force # s=set() # for i in range(n): # for j in range(n): # a=nums[i] | nums[j] # b=nums[i] &amp; nums[j] # a_count=bin(a).count('1') # b_count=bin(b).count('1') # if a_count+b_count>=k and (nums[i], nums[j]) not in s: # s.add((nums[i], nums[j])) # count+=1 # return count arr=[] for num in set(nums): arr.append(bin(num).count('1')) arr.sort() l=0 r=len(arr)-1 ans=0 while l<=r: if arr[l]+arr[r]>=k: ans+=(r-l)*2 + 1 r-=1 else: l+=1 return ans
number-of-excellent-pairs
Python Solution | Brute Force | Optimized | O(N)
Siddharth_singh
1
41
number of excellent pairs
2,354
0.458
Hard
32,287
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2325096/Python-Simple-Solution
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: nums.sort() mapp = defaultdict(set) ans = 0 last = None for i in nums: if i==last: continue b = format(i,'b').count('1') mapp[b].add(i) t = k-b for j in range(max(0,t),31): ans+=len(mapp[j])*2 if i in mapp[j]: ans-=1 last = i return ans
number-of-excellent-pairs
Python Simple Solution
RedHeadphone
1
52
number of excellent pairs
2,354
0.458
Hard
32,288
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2324816/Python3-sum-of-set-bits
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: nums = set(nums) freq = [0]*30 for x in nums: freq[bin(x).count('1')] += 1 prefix = list(accumulate(freq, initial=0)) ans = 0 for x in nums: bits = bin(x).count('1') lo = min(30, max(0, k-bits)) ans += prefix[-1] - prefix[lo] return ans
number-of-excellent-pairs
[Python3] sum of set bits
ye15
1
56
number of excellent pairs
2,354
0.458
Hard
32,289
https://leetcode.com/problems/number-of-excellent-pairs/discuss/2324816/Python3-sum-of-set-bits
class Solution: def countExcellentPairs(self, nums: List[int], k: int) -> int: freq = Counter(map(int.bit_count, set(nums))) return sum(v1*v2 for k1, v1 in freq.items() for k2, v2 in freq.items() if k1+k2 >= k)
number-of-excellent-pairs
[Python3] sum of set bits
ye15
1
56
number of excellent pairs
2,354
0.458
Hard
32,290
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2357747/Set
class Solution: def minimumOperations(self, nums: List[int]) -> int: return len(set(nums) - {0})
make-array-zero-by-subtracting-equal-amounts
Set
votrubac
17
1,500
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,291
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2357890/Python-easy-solution
class Solution: def minimumOperations(self, nums: List[int]) -> int: n = len(nums) count = 0 while nums != [0]*n: count += 1 small = min([i for i in nums if i > 0]) for i in range(n): if nums[i] != 0: nums[i] -= small return count
make-array-zero-by-subtracting-equal-amounts
Python easy solution
ratre21
5
658
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,292
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2532978/Python3-Solution
class Solution: def minimumOperations(self, nums: List[int]) -> int: nums = set(nums) if 0 in nums: return(len(nums)-1) else: return(len(nums))
make-array-zero-by-subtracting-equal-amounts
Python3 Solution
nkrishk
3
134
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,293
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2358185/Python-oror-Easy-Approach
class Solution: def minimumOperations(self, nums: List[int]) -> int: num = set(nums) a = list(num) a.sort() ans = 0 for i in range(len(a)): if a[i] != 0: ans += 1 for j in range(i + 1, len(a)): a[j] -= a[i] return ans
make-array-zero-by-subtracting-equal-amounts
✅Python || Easy Approach
chuhonghao01
2
138
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,294
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2358185/Python-oror-Easy-Approach
class Solution: def minimumOperations(self, nums: List[int]) -> int: count = 0 while max(nums) != 0: minValue = float('inf') for i in range(len(nums)): if nums[i] != 0 and nums[i] < minValue: minValue = nums[i] for i in range(len(nums)): nums[i] = max(nums[i] - minValue, 0) count += 1 return count
make-array-zero-by-subtracting-equal-amounts
✅Python || Easy Approach
chuhonghao01
2
138
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,295
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2563261/Make-Array-Zero-by-Subtracting-Equal-Amounts
class Solution: def minimumOperations(self, nums: List[int]) -> int: uniqueNumbers = set() for i in nums: if i==0: continue if i in uniqueNumbers: continue uniqueNumbers.add(i) return len(uniqueNumbers)
make-array-zero-by-subtracting-equal-amounts
Make Array Zero by Subtracting Equal Amounts
dhananjayaduttmishra
1
37
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,296
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2394569/Heap-PYTHON3-Solution
class Solution: def minimumOperations(self, nums: List[int]) -> int: minHeap=[] for i in nums: if i!=0: heappush(minHeap,i) ans=0 while minHeap: t=heappop(minHeap) while minHeap and t==minHeap[0]: heappop(minHeap) ans+=1 for i in range(len(minHeap)): minHeap[i]-=t return ans
make-array-zero-by-subtracting-equal-amounts
Heap PYTHON3 Solution
shambhavi_gupta
1
70
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,297
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2381544/Python-easy-solution-for-beginners-using-slicing-and-sorting
class Solution: def minimumOperations(self, nums: List[int]) -> int: if nums == [0] * len(nums): return 0 count_op = 0 while True: nums.sort() count_zero = nums.count(0) nums = nums[count_zero:] if nums == []: return count_op choose = nums[0] for i in range(len(nums)): nums[i] -= choose count_op += 1
make-array-zero-by-subtracting-equal-amounts
Python easy solution for beginners using slicing and sorting
alishak1999
1
98
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,298
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/discuss/2793353/Brute-force-python-solution-or-Faster-than-86
class Solution: def minimumOperations(self, nums: List[int]) -> int: count = 0 while sum(nums) != 0: min_elem = min([i for i in nums if i != 0]) for index, num in enumerate(nums): if num != 0: nums[index] -= min_elem count += 1 return count
make-array-zero-by-subtracting-equal-amounts
Brute force python solution | Faster than 86%
prameshbajra
0
5
make array zero by subtracting equal amounts
2,357
0.727
Easy
32,299