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/words-within-two-edits-of-dictionary/discuss/2756737/Simple-and-Clean-python-code
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: def fut(a,b): ret = 0 for i in range(len(a)): if a[i] != b[i]: ret += 1 return ret res = [] for q in queries: for d in dictionary: if fut(q,d) <= 2: res.append(q) break return res
words-within-two-edits-of-dictionary
Simple and Clean python code
w7Pratham
0
9
words within two edits of dictionary
2,452
0.603
Medium
33,600
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756735/Python-Solution
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: ans = [] s = set() def isequal(a,b,x): c = 0 i = 0 n = len(a) while i < n: if a[i] != b[i]: c += 1 i += 1 if c <= 2 and x not in s: ans.append(a) s.add(x) return for i in range(len(queries)): for j in range(len(dictionary)): isequal(queries[i],dictionary[j],i) return ans
words-within-two-edits-of-dictionary
Python Solution
a_dityamishra
0
7
words within two edits of dictionary
2,452
0.603
Medium
33,601
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756715/Python3-Brute-Force-Solution
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: res = [] for word0 in queries: for word1 in dictionary: diff = 0 for t in range(len(word0)): if word0[t] != word1[t]: diff += 1 if diff <= 2: res.append(word0) break return res
words-within-two-edits-of-dictionary
Python3 Brute Force Solution
xxHRxx
0
4
words within two edits of dictionary
2,452
0.603
Medium
33,602
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756662/Python3-set-of-all-acceptable-words
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: d=set(dictionary) for w in dictionary: for i in range(len(w)): w1=w[:i]+'*'+w[i+1:] d.add(w1) for j in range(i,len(w)): w2=w1[:j]+'*'+w1[j+1:] d.add(w2) answ=[] for w in queries: br=0 if w in d: answ.append(w) continue for i in range(len(w)): if br: break w1=w[:i]+'*'+w[i+1:] if w1 in d: answ.append(w) break for j in range(i,len(w)): w2=w1[:j]+'*'+w1[j+1:] if w2 in d: answ.append(w) br=1 break return answ
words-within-two-edits-of-dictionary
Python3, set of all acceptable words
Silvia42
0
3
words within two edits of dictionary
2,452
0.603
Medium
33,603
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756646/PythonStraight-forward-code
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: output = [] count = 0 for word_queries in queries: for word_dictionary in dictionary: count = 0 for i in range(len(word_queries)): if word_queries[i] != word_dictionary[i]: count += 1 if count<=2: output.append(word_queries) break return output
words-within-two-edits-of-dictionary
【Python】Straight forward code ✅
gordonkwok
0
8
words within two edits of dictionary
2,452
0.603
Medium
33,604
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756522/Python-Easy-to-Understand-With-Explanation
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: def count(s, t): ans = 0 for i in range(len(s)): if s[i] != t[i]: ans += 1 if ans > 2: return False return True ans = [] for word in queries: for d in dictionary: if count(word, d): ans.append(word) break return ans
words-within-two-edits-of-dictionary
[Python] Easy to Understand With Explanation
neil_teng
0
15
words within two edits of dictionary
2,452
0.603
Medium
33,605
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756513/Python3-or-Brute-Force
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: n,m=len(queries),len(dictionary) ans=[] for q in queries: for d in dictionary: cnt=0 i=0 while i<len(q): if q[i]!=d[i]: cnt+=1 i+=1 if cnt<=2: ans.append(q) break return ans
words-within-two-edits-of-dictionary
[Python3] | Brute Force
swapnilsingh421
0
7
words within two edits of dictionary
2,452
0.603
Medium
33,606
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756457/Python-or-Easy-or
class Solution: def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]: n = len(queries[0]) ans = [] for i in range(len(queries)): for j in range(len(dictionary)): count = 0 for k in range(n): if(dictionary[j][k] != queries[i][k]): count += 1 if(count > 2): break if(count <= 2): ans.append(queries[i]) break return ans
words-within-two-edits-of-dictionary
Python | Easy |
LittleMonster23
0
24
words within two edits of dictionary
2,452
0.603
Medium
33,607
https://leetcode.com/problems/destroy-sequential-targets/discuss/2756374/Python-or-Greedy-or-Group-or-Example
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: # example: nums = [3,7,8,1,1,5], space = 2 groups = defaultdict(list) for num in nums: groups[num % space].append(num) # print(groups) # defaultdict(<class 'list'>, {1: [3, 7, 1, 1, 5], 0: [8]}) groups is [3, 7, 1, 1, 5] and [8] """ min of [3, 7, 1, 1, 5] can destroy all others (greedy approach) => 1 can destory 1,3,5,7 ... """ performance = defaultdict(list) for group in groups.values(): performance[len(group)].append(min(group)) # print(performance) # defaultdict(<class 'list'>, {5: [1], 1: [8]}) # nums that can destory 5 targets are [1], nums that can destory 1 target are [8] return min(performance[max(performance)])
destroy-sequential-targets
Python | Greedy | Group | Example
yzhao156
3
122
destroy sequential targets
2,453
0.373
Medium
33,608
https://leetcode.com/problems/destroy-sequential-targets/discuss/2794087/Python-(Simple-Maths)
class Solution: def destroyTargets(self, nums, space): dict1 = defaultdict(int) for i in nums: dict1[i%space] += 1 max_val = max(dict1.values()) return min([i for i in nums if dict1[i%space] == max_val])
destroy-sequential-targets
Python (Simple Maths)
rnotappl
1
5
destroy sequential targets
2,453
0.373
Medium
33,609
https://leetcode.com/problems/destroy-sequential-targets/discuss/2844702/Python3-Commented-and-Concise-Solution-Modulo-and-HashMap
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: # get a dict of modulos modulo_dict = dict() # go through the array and compute the modulo and their counter for idx, num in enumerate(nums): # compute the modulo modi = num % space # check whether we need to create one if modi in modulo_dict: # keep track of the smalles number modulo_dict[modi][0] = min(num, modulo_dict[modi][0]) # keep track of the count of this specific modulo modulo_dict[modi][1] += 1 else: # create this modulo modulo_dict[modi] = [num, 1] # go through the dict and element with the highest modulo count best_ele = max(modulo_dict.values(), key=lambda x: (x[1], -x[0])) # return the good element return best_ele[0]
destroy-sequential-targets
[Python3] - Commented and Concise Solution - Modulo and HashMap
Lucew
0
1
destroy sequential targets
2,453
0.373
Medium
33,610
https://leetcode.com/problems/destroy-sequential-targets/discuss/2766934/One-pass-no-sorting-59-speed
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: d = dict() for n in nums: rem = n % space if rem in d: if n < d[rem][1]: d[rem][1] = n d[rem][0] -= 1 else: d[rem] = [-1, n] return min(d.values())[1]
destroy-sequential-targets
One pass, no sorting, 59% speed
EvgenySH
0
6
destroy sequential targets
2,453
0.373
Medium
33,611
https://leetcode.com/problems/destroy-sequential-targets/discuss/2760145/Python-or-Three-Lines-or-Mod-Counter-wexplanation
class Solution: def destroyTargets(self, xs: List[int], space: int) -> int: class_freqs = Counter(x % space for x in xs) max_count = max (class_freqs.values()) return min(x for x in xs if class_freqs [x % space] == max_count)
destroy-sequential-targets
Python | Three Lines | Mod Counter w/explanation
on_danse_encore_on_rit_encore
0
11
destroy sequential targets
2,453
0.373
Medium
33,612
https://leetcode.com/problems/destroy-sequential-targets/discuss/2757190/Python3-modulo
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: freq = Counter() mp = defaultdict(lambda : inf) for x in nums: k = x % space freq[k] += 1 mp[k] = min(mp[k], x) m = max(freq.values()) return min(mp[k] for k, v in freq.items() if v == m)
destroy-sequential-targets
[Python3] modulo
ye15
0
9
destroy sequential targets
2,453
0.373
Medium
33,613
https://leetcode.com/problems/destroy-sequential-targets/discuss/2756967/Python-Answer-Group-Numbers-by-Modulo-Space-into-Dict
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: #1,3,5,7,9,... #3, 5, 7, 9 #8, 10, #nums = [3,7,8,1,1,5], space = 2 groups = defaultdict(int) val = defaultdict(list) for n in nums: groups[n%space] += 1 val[n%space].append(n) n = max(groups.values()) m = float('inf') for v in val: if len(val[v]) == n: m = min(m, min(val[v])) return m
destroy-sequential-targets
[Python Answer🤫🐍🐍🐍] Group Numbers by Modulo Space into Dict
xmky
0
9
destroy sequential targets
2,453
0.373
Medium
33,614
https://leetcode.com/problems/destroy-sequential-targets/discuss/2756811/Python3-Easy-Remainder-Solution-O(N)
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: nums.sort() dicts = defaultdict(int) for element in nums: dicts[element % space] += 1 target = max(dicts.values()) for element in nums: if dicts[element % space] == target: return element
destroy-sequential-targets
Python3 Easy Remainder Solution O(N)
xxHRxx
0
4
destroy sequential targets
2,453
0.373
Medium
33,615
https://leetcode.com/problems/destroy-sequential-targets/discuss/2756559/Group-nums-by-mod-space
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: numsByMod = defaultdict(list) for num in nums: numsByMod[num % space].append(num) maxLength = 0 for group in numsByMod.values(): if maxLength < len(group): maxLength = len(group) maxDestroyer = min(group) elif maxLength == len(group): maxDestroyer = min(maxDestroyer, min(group)) return maxDestroyer
destroy-sequential-targets
Group nums by mod space
sr_vrd
0
7
destroy sequential targets
2,453
0.373
Medium
33,616
https://leetcode.com/problems/destroy-sequential-targets/discuss/2756511/Python-3-oror-Time%3A-1539-ms-Space%3A-34-MB
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: n=len(nums) t=[0 for i in range(n)] for i in range(len(nums)): t[i]=nums[i]%space z=Counter(t) t1=0 for i in z: t1=max(z[i],t1) ans=float('inf') for i in range(n): if(z[t[i]]==t1): ans=min(ans,nums[i]) return ans
destroy-sequential-targets
Python 3 || Time: 1539 ms , Space: 34 MB
koder_786
0
9
destroy sequential targets
2,453
0.373
Medium
33,617
https://leetcode.com/problems/destroy-sequential-targets/discuss/2756460/Python3-or-Modulus-%2B-Hashmap
class Solution: def destroyTargets(self, nums: List[int], space: int) -> int: hmap=defaultdict(list) for i in nums: if i%space in hmap: occ,val=hmap[i%space] hmap[i%space]=[occ+1,min(val,i)] else: hmap[i%space]=[1,i] value=-float('inf') ans=-1 for i,j in hmap.items(): if j[0]>value: value=j[0] ans=j[1] if j[0]==value: ans=min(ans,j[1]) return ans
destroy-sequential-targets
[Python3] | Modulus + Hashmap
swapnilsingh421
0
9
destroy sequential targets
2,453
0.373
Medium
33,618
https://leetcode.com/problems/next-greater-element-iv/discuss/2757259/Python3-intermediate-stack
class Solution: def secondGreaterElement(self, nums: List[int]) -> List[int]: ans = [-1] * len(nums) s, ss = [], [] for i, x in enumerate(nums): while ss and nums[ss[-1]] < x: ans[ss.pop()] = x buff = [] while s and nums[s[-1]] < x: buff.append(s.pop()) while buff: ss.append(buff.pop()) s.append(i) return ans
next-greater-element-iv
[Python3] intermediate stack
ye15
1
38
next greater element iv
2,454
0.396
Hard
33,619
https://leetcode.com/problems/next-greater-element-iv/discuss/2757157/O(n-log-n)-Python-sorted-list-solution
class Solution: def secondGreaterElement(self, nums: List[int]) -> List[int]: from sortedcontainers import SortedList as slist l=len(nums) ans=[-1]*l print(ans) lst=slist([(nums[0], 0, [])], key=lambda x:(x[0], x[1])) i=1 while i<l: tmp=nums[i] j=0 while j<len(lst) and lst[j][0]<tmp: lst[j][2].append(nums[i]) if len(lst[j][2])>=2: ans[lst[j][1]]=lst[j][2][1] lst.discard(lst[j]) else: j+=1 lst.add((nums[i], i, [])) i+=1 return ans
next-greater-element-iv
O(n log n) Python sorted list solution
mbeceanu
0
11
next greater element iv
2,454
0.396
Hard
33,620
https://leetcode.com/problems/next-greater-element-iv/discuss/2757122/Python-3Heap-%2B-Monotonic-queue-(hint-solution)
class Solution: def secondGreaterElement(self, nums: List[int]) -> List[int]: n = len(nums) first, second = [], [] ans = [-1] * n for i in range(n): # check if current greater than candidates waiting for second greater element while second and nums[i] > second[0][0]: val, idx = heappop(second) ans[idx] = nums[i] # check if current greater than candidates waiting for first greater element while first and nums[i] > nums[first[-1]]: tmp = first.pop() # push into candidates waiting for second greater element # min-heap with smallest value on top heappush(second, (nums[tmp], tmp)) first.append(i) return ans
next-greater-element-iv
[Python 3]Heap + Monotonic queue (hint solution)
chestnut890123
0
20
next greater element iv
2,454
0.396
Hard
33,621
https://leetcode.com/problems/next-greater-element-iv/discuss/2756483/Python3-or-Stack-%2B-Priority-Queue
class Solution: def secondGreaterElement(self, nums: List[int]) -> List[int]: st1,st2=[],[] heapify(st2) ans=[-1 for i in range(len(nums))] for i in range(len(nums)): while st2 and nums[-st2[0]]<nums[i]: ans[-heappop(st2)]=nums[i] while st1 and nums[st1[-1]]<nums[i]: heappush(st2,-st1.pop()) st1.append(i) return ans
next-greater-element-iv
[Python3] | Stack + Priority Queue
swapnilsingh421
0
57
next greater element iv
2,454
0.396
Hard
33,622
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2770799/Easy-Python-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: l=[] for i in nums: if i%6==0: l.append(i) return sum(l)//len(l) if len(l)>0 else 0
average-value-of-even-numbers-that-are-divisible-by-three
Easy Python Solution
Vistrit
2
107
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,623
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2764545/Python-Simple-Python-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: s=0 k=0 for i in nums: if i%6==0: k+=1 s+=i if k==0: return 0 else: return(s//k)
average-value-of-even-numbers-that-are-divisible-by-three
[ Python ]🐍🐍Simple Python Solution ✅✅
sourav638
2
17
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,624
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2780425/Python-one-pass-O(n)
class Solution: def averageValue(self, nums: List[int]) -> int: _ans =[] for i in nums: if (i%2==0) and (i%3==0): _ans.append(i) return sum(_ans)//len(_ans) if len(_ans) > 0 else 0
average-value-of-even-numbers-that-are-divisible-by-three
Python one pass O(n)
ATHBuys
1
18
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,625
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2763059/Easiest-Python-Solution-oror-With-Explanation
class Solution: def averageValue(self, nums: List[int]) -> int: ans=0 # ans will store the sum of elements which are even and divisible by 3; cnt=0 # cnt will store the number of elements which are even and divisible by 3; for ele in nums: # Elements which are divisible by 3 and are even simply means **It must be divisible by 6** So we are checking that in the loop # we are adding it to ans if it is divisible by 6 and increase cnt by 1; if (ele%6==0): ans+=ele; cnt+=1; if (cnt==0): return 0; # if no element is found return 0; return (floor(ans/cnt)); # else return the floor value ofaverage that is sum of elements divided by no. of elements
average-value-of-even-numbers-that-are-divisible-by-three
Easiest Python Solution || With Explanation ✔
akanksha984
1
75
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,626
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2828543/4-line-Python-solution
class Solution: def averageValue(self, nums: List[int]) -> int: valid_nums = [num for num in nums if num % 3 == 0 and num % 2 == 0] if len(valid_nums) == 0: return 0 return int(sum(valid_nums)/len(valid_nums))
average-value-of-even-numbers-that-are-divisible-by-three
4 line Python solution
lornedot
0
2
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,627
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2820623/python-O(n)-beats-66
class Solution: ## def averageValue(self, nums: List[int]) -> int: ## Time Complexity == O(n) lst = [i for i in nums if i%2 == 0 and i%3 ==0] return sum(lst) // len(lst) if lst else 0
average-value-of-even-numbers-that-are-divisible-by-three
python O(n) beats 66%
syedsajjad62
0
2
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,628
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2820592/Python-3-Simple-code
class Solution: def averageValue(self, nums: List[int]) -> int: res = 0 count = 0 for i in nums: # Even number that is divisible by 3 # Same conditions as divisible by 6 if i % 6 == 0: res += i count += 1 if count != 0: res = res // count return res
average-value-of-even-numbers-that-are-divisible-by-three
[Python 3] Simple code
nguyentangnhathuy
0
1
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,629
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2795381/Easy-approach
class Solution: def averageValue(self, nums: List[int]) -> int: l=[] for i in nums: if i%6==0: l.append(i) if(len(l)>0): return sum(l)//len(l) else: return 0
average-value-of-even-numbers-that-are-divisible-by-three
Easy approach
nishithakonuganti
0
2
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,630
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2795283/Python-Easy-Solution-Time%3A-0(n)-Space%3A-0(1)
class Solution: def averageValue(self, nums: List[int]) -> int: s = 0 c = 0 for i, v in enumerate(nums): if v % 2 == 0 and v % 3 == 0: s += v c += 1 return math.floor(s / c) if c != 0 else 0
average-value-of-even-numbers-that-are-divisible-by-three
Python Easy Solution Time: 0(n) Space: 0(1)
ben-tiki
0
4
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,631
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2783199/2455.-Average-Value-of-Even-Numbers-That-Are-Divisible-by-Three
class Solution: def averageValue(self, nums: List[int]) -> int: add=0 lst=[] for i in range(len(nums)): if(nums[i]%3==0 and nums[i]%2==0): lst.append(nums[i]) x=len(lst) if(x==0): return 0 for i in range(x): add+=lst[i] return add//x
average-value-of-even-numbers-that-are-divisible-by-three
2455. Average Value of Even Numbers That Are Divisible by Three
knock_knock47
0
2
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,632
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2779971/Simple-Python-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: list2 = [] for i in nums: if i%2 == 0: if i%3 == 0: list2.append(i) b = len(list2) sum1 = 0 if b>0: for i in list2: sum1 = sum1 + i else: return 0 c = sum1//b return c
average-value-of-even-numbers-that-are-divisible-by-three
Simple Python Solution
dnvavinash
0
5
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,633
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2774622/Python-Simple-and-Crisp-Solution!
class Solution: def averageValue(self, nums: List[int]) -> int: total = 0 count = 0 for x in nums: if x % 2 == 0 and x % 3 == 0: total += x count += 1 if count == 0: return 0 return total // count
average-value-of-even-numbers-that-are-divisible-by-three
Python Simple and Crisp Solution!
vedanthvbaliga
0
2
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,634
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2770560/Python
class Solution: def averageValue(self, nums: List[int]) -> int: list1 = [x for x in nums if x%6 == 0] if (len(list1) > 0): return sum(list1)//len(list1) else: return 0
average-value-of-even-numbers-that-are-divisible-by-three
Python
user1079z
0
3
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,635
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2766941/easy-way-in-python
class Solution: def averageValue(self, nums: List[int]) -> int: s=0 count=0 for i in nums: if i%2==0 and i%3==0: print(i) s=s+i count=count+1 if count==0: return 0 else: return s//count
average-value-of-even-numbers-that-are-divisible-by-three
easy way in python
sindhu_300
0
13
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,636
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2764757/Python-Easy-Understanding-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: n = 0 total = 0 for val in nums: if val % 6 == 0 and val != 0: n += 1 total += val return int(total/n) if n else 0
average-value-of-even-numbers-that-are-divisible-by-three
Python Easy Understanding Solution
MaverickEyedea
0
3
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,637
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2761942/One-pass
class Solution: def averageValue(self, nums: List[int]) -> int: sum_nums = count = 0 for n in nums: if not n % 6: sum_nums += n count += 1 return sum_nums // count if count > 0 else 0
average-value-of-even-numbers-that-are-divisible-by-three
One pass
EvgenySH
0
5
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,638
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2761930/Easy-FIlter-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: filt =[i for i in nums if i % 6 == 0] return sum(filt) // len(filt) if len(filt) else 0
average-value-of-even-numbers-that-are-divisible-by-three
Easy FIlter Solution
cyber_kazakh
0
7
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,639
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2761255/Python3-oror-Easy-oror-O(N)
class Solution: def averageValue(self, nums: List[int]) -> int: sumo = 0 count = 0 for i in range(len(nums)): if nums[i] % 6 == 0: sumo += nums[i] count += 1 return 0 if (count == 0) else sumo//count
average-value-of-even-numbers-that-are-divisible-by-three
Python3 || Easy || O(N)
akshdeeprr
0
6
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,640
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2759518/python-O(n)
class Solution: def averageValue(self, nums: List[int]) -> int: temp_sum = 0 no = 0 for i in nums: if i % 3 == 0 and i % 2 == 0: temp_sum += i no += 1 if no == 0: return 0 else: return temp_sum//no
average-value-of-even-numbers-that-are-divisible-by-three
python O(n)
akashp2001
0
5
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,641
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2759225/Python-Simple-Python-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: result = 0 current_sum = 0 count = 0 for num in nums: if num % 2 == 0 and num % 3 == 0: current_sum = current_sum + num count = count + 1 if count == 0: return 0 else: result = current_sum // count return result
average-value-of-even-numbers-that-are-divisible-by-three
[ Python ] ✅✅ Simple Python Solution 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
11
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,642
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758828/Python%2BNumPy
class Solution: def averageValue(self, nums: List[int]) -> int: import numpy as np nums = [x for x in nums if x%6==0] return int(np.average(nums)) if nums else 0
average-value-of-even-numbers-that-are-divisible-by-three
Python+NumPy
Leox2022
0
3
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,643
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758699/Python-or-Three-lines
class Solution: def averageValue(self, nums: List[int]) -> int: xs = [m for m in nums if m % 6 == 0] n = len(xs) return 0 if n == 0 else sum(xs) // n
average-value-of-even-numbers-that-are-divisible-by-three
Python | Three lines
on_danse_encore_on_rit_encore
0
11
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,644
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758436/Python
class Solution: def averageValue(self, nums: List[int]) -> int: s = c = 0 for n in nums: if n % 6 == 0: s += n c += 1 return s // c if c else 0
average-value-of-even-numbers-that-are-divisible-by-three
Python
blue_sky5
0
7
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,645
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758382/Python-Solution(Brute-force-and-Optimal-approach)
class Solution: def averageValue(self, nums: List[int]) -> int: # Brute Force Approach, T.C = O(N), S.C = O(N) # N = len(nums) # ans = [] # for i in range(N): # if nums[i] % 2 == 0 and nums[i] % 3 == 0: # ans.append(nums[i]) # if len(ans) == 0: # return 0 # else: # return sum(ans)//len(ans) # Optimal Approach, T.C = O(N), S.C = O(1) N = len(nums) ans = 0 count = 0 for i in range(N): if nums[i] % 2 == 0 and nums[i] % 3 == 0: ans += nums[i] count += 1 if ans == 0: return 0 else: return ans//count
average-value-of-even-numbers-that-are-divisible-by-three
Python Solution(Brute force and Optimal approach)
Ayush_Kumar27
0
6
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,646
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758362/Python3-Simple-2-lines
class Solution: def averageValue(self, nums: List[int]) -> int: q=[x for x in nums if not x%6] return q and sum(q)//len(q) or 0
average-value-of-even-numbers-that-are-divisible-by-three
Python3, Simple 2 lines
Silvia42
0
8
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,647
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758275/Python-Answer-Modulo
class Solution: def averageValue(self, nums: List[int]) -> int: res = [] for n in nums: if n % 3 == 0 and n % 2 == 0: res.append(n) return floor(sum(res) / len(res)) if len(res) > 0 else 0
average-value-of-even-numbers-that-are-divisible-by-three
[Python Answer🤫🐍🐍🐍] Modulo
xmky
0
4
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,648
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758257/Python-Solution
class Solution: def averageValue(self, nums: List[int]) -> int: # Brute Force Approach, T.C = O(N), S.C = O(N) # N = len(nums) # ans = [] # for i in range(N): # if nums[i] % 2 == 0 and nums[i] % 3 == 0: # ans.append(nums[i]) # if len(ans) == 0: # return 0 # else: # return sum(ans)//len(ans) # Optimal Approach, T.C = O(N), S.C = O(1) N = len(nums) ans = 0 count = 0 for i in range(N): if nums[i] % 2 == 0 and nums[i] % 3 == 0: ans += nums[i] count += 1 if ans == 0: return 0 else: return ans//count
average-value-of-even-numbers-that-are-divisible-by-three
Python Solution
Ayush_Kumar27
0
4
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,649
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758181/Python-Easy-Solution-with-explanation
class Solution: def averageValue(self, nums: List[int]) -> int: ''' Algorithm: 1.declare total variable to hold the sum of all even numbers divisible by 3 2. declare a count variable to hold the number of the even numbers divisible by 3 3. loop over the array 4. check if the number is even and divisible by 3 5. if yes update sum and number variables 6. when the loop ends, return total/count which is average Time Complexity O(N) Space Complexity O(1) ''' total = 0 count = 0 for i in nums: if i%2 == 0 and i%3 == 0: total += i count += 1 try: return int(total/count) except: return 0
average-value-of-even-numbers-that-are-divisible-by-three
Python Easy Solution with explanation
dee7
0
10
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,650
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758180/Python3-or-Implementation
class Solution: def averageValue(self, nums: List[int]) -> int: cnt=0 curr=0 for i in nums: if i%2==0 and i%3==0: curr+=i cnt+=1 if cnt==0: return 0 return curr//cnt
average-value-of-even-numbers-that-are-divisible-by-three
[Python3] | Implementation
swapnilsingh421
0
6
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,651
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758105/Python3-divisible-by-6
class Solution: def averageValue(self, nums: List[int]) -> int: cnt = total = 0 for x in nums: if x % 6 == 0: cnt += 1 total += x return cnt and total//cnt
average-value-of-even-numbers-that-are-divisible-by-three
[Python3] divisible by 6
ye15
0
11
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,652
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758056/easy-python-solutionororeasy-to-understand
class Solution: def averageValue(self, nums: List[int]) -> int: tot = 0 count=0 for ele in nums: if ele%2==0: if ele%3==0: tot+=ele count+=1 if count==0: return 0 else: return tot//count
average-value-of-even-numbers-that-are-divisible-by-three
✅✅easy python solution||easy to understand
chessman_1
0
2
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,653
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2758050/PythonStraight-forward-code
class Solution: def averageValue(self, nums: List[int]) -> int: sum = 0 divisor = 0 for num in nums: if num%2==0 and num%3==0: sum+=num divisor+=1 if divisor == 0: return 0 return int(sum/divisor)
average-value-of-even-numbers-that-are-divisible-by-three
【Python】Straight forward code ✅
gordonkwok
0
11
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,654
https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/discuss/2757986/python
class Solution: def averageValue(self, nums: List[int]) -> int: sum, count = 0,0 for n in nums: if n % 3 == 0 and n % 2 == 0: sum += n count += 1 if count == 0: return 0 return int(sum/count)
average-value-of-even-numbers-that-are-divisible-by-three
python
pradyumna04
0
5
average value of even numbers that are divisible by three
2,455
0.58
Easy
33,655
https://leetcode.com/problems/most-popular-video-creator/discuss/2758104/Python-Hashmap-solution-or-No-Sorting
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: memo = {} #tracking the max popular video count overall_max_popular_video_count = -1 #looping over the creators for i in range(len(creators)): if creators[i] in memo: #Step 1: update number of views for the creator memo[creators[i]][0] += views[i] #Step 2: update current_popular_video_view and id_of_most_popular_video_so_far if memo[creators[i]][2] < views[i]: memo[creators[i]][1] = ids[i] memo[creators[i]][2] = views[i] #Step 2a: finding the lexicographically smallest id as we hit the current_popularity_video_view again! elif memo[creators[i]][2] == views[i]: memo[creators[i]][1] = min(memo[creators[i]][1],ids[i]) else: #adding new entry to our memo #new entry is of the format memo[creator[i]] = [total number current views for the creator, store the lexicographic id of the popular video, current popular view of the creator] memo[creators[i]] = [views[i],ids[i],views[i]] #track the max popular video count overall_max_popular_video_count = max(memo[creators[i]][0],overall_max_popular_video_count) result = [] for i in memo: if memo[i][0] == overall_max_popular_video_count: result.append([i,memo[i][1]]) return result
most-popular-video-creator
Python Hashmap solution | No Sorting
dee7
5
330
most popular video creator
2,456
0.441
Medium
33,656
https://leetcode.com/problems/most-popular-video-creator/discuss/2758289/Python-Answer-Heap-%2B-Dict-solution
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: pop = defaultdict(int) pop_small = defaultdict(list) for i,creator in enumerate(creators): pop[creator] += views[i] heappush(pop_small[creator], [-views[i],ids[i]]) m = max(pop.values()) res = [] for creator in pop: if pop[creator] == m: res.append([creator, pop_small[creator][0][1]]) return res
most-popular-video-creator
[Python Answer🤫🐍🐍🐍] Heap + Dict solution
xmky
1
43
most popular video creator
2,456
0.441
Medium
33,657
https://leetcode.com/problems/most-popular-video-creator/discuss/2842036/Python3-Linear-Hashmap-with-multiple-elements
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: # make a dict to save the viwer count and the highest view count counter = collections.defaultdict(lambda: [0, "", -1]) # go through the content creators for creator, idd, view in zip(creators, ids, views): # get the list with informations infos = counter[creator] # increase the view infos[0] += view # check if the video is the most viewed if view > infos[2] or (view == infos[2] and infos[1] > idd): infos[2] = view infos[1] = idd # go through the creators and keep the highes result = [] max_count = -1 for creator, infos in counter.items(): # check if we are higher or equal to current count if max_count <= infos[0]: # check if we are higher than current max if max_count < infos[0]: # clear the list result.clear() # update the max count max_count = infos[0] # append our current content creator result.append([creator, infos[1]]) return result
most-popular-video-creator
[Python3] - Linear Hashmap with multiple elements
Lucew
0
1
most popular video creator
2,456
0.441
Medium
33,658
https://leetcode.com/problems/most-popular-video-creator/discuss/2841104/python-dictionary-beats-96-of-answers
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: """ summary : each creator will get to be a key in a dictionary that contains the 1st item the sum of all the views so far the 2nd the id(in case if the value of max seen is the same the smallest one lexicographically) the 3rd item the maxviews so far when we have all this data we look for the biggest sum and append the name of the creator and the id to the ans list """ data = {} for creator, id , view in zip(creators, ids, views): if creator in data: data[creator][0] += view if view>data[creator][2]: data[creator][2] = view data[creator][1] = id continue if view==data[creator][2] and id< data[creator][1]: data[creator][1] = id continue else: data[creator] = [view,id,view] maxView = 0 ans = [] for k,v in data.items(): if v[0]>maxView: ans =[] maxView =v[0] if v[0]==maxView: ans.append([k,v[1]]) return ans
most-popular-video-creator
python dictionary beats 96% of answers
rojanrookhosh
0
1
most popular video creator
2,456
0.441
Medium
33,659
https://leetcode.com/problems/most-popular-video-creator/discuss/2824294/Simple-efficient-and-fast-Hashing-in-Python
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: d = {} max_views = 0 for creator, view, id in zip(creators, views, ids): # views, max viewed, id if creator not in d: d[creator] = [0, view, id] elif view == d[creator][1]: d[creator][2] = id if id < d[creator][2] else d[creator][2] elif view > d[creator][1]: d[creator][1] = view d[creator][2] = id d[creator][0] += view if d[creator][0] > max_views: max_views = d[creator][0] res = [] for k, v in d.items(): if v[0] == max_views: res.append([k, v[2]]) return res
most-popular-video-creator
Simple efficient and fast - Hashing in Python
cgtinker
0
1
most popular video creator
2,456
0.441
Medium
33,660
https://leetcode.com/problems/most-popular-video-creator/discuss/2806466/Hasmap-Solution
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: d = defaultdict(int) maxmap,mx = {},0 for c,i,v in zip(creators,ids,views): d[c]+=v mx = max(mx,d[c]) if c not in maxmap or maxmap[c][1]<v or (maxmap[c][1]==v and maxmap[c][0]>i): maxmap[c] = [i,v] ans = [] for i in maxmap: if d[i]==mx: ans.append([i,maxmap[i][0]]) return ans
most-popular-video-creator
Hasmap Solution
Rtriders
0
2
most popular video creator
2,456
0.441
Medium
33,661
https://leetcode.com/problems/most-popular-video-creator/discuss/2779895/Python-O(n)-solution-using-dictionary
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: creators_set = set() sum_map = defaultdict(int) order_map = {} n = len(views) for i in range(n): creators_set.add(creators[i]) sum_map[creators[i]]+=views[i] if creators[i] not in order_map or views[i]>order_map[creators[i]][-1]: order_map[creators[i]] = [ids[i], views[i]] elif views[i]==order_map[creators[i]][-1] and ids[i]<order_map[creators[i]][0]: order_map[creators[i]] = [ids[i], views[i]] res = [] max_views = max(sum_map.values()) for creator in creators_set: if sum_map[creator] == max_views: res.append([creator, order_map[creator][0]]) return res
most-popular-video-creator
Python O(n) solution using dictionary
dhanu084
0
8
most popular video creator
2,456
0.441
Medium
33,662
https://leetcode.com/problems/most-popular-video-creator/discuss/2767144/Dictionary-for-creators-100-speed
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: d = dict() for c, i, v in zip(creators, ids, views): if c in d: d[c][0] += v if i in d[c][1]: d[c][1][i] += v else: d[c][1][i] = v else: d[c] = [v, {i: v}] popularity = 0 for lst in d.values(): popularity = max(popularity, lst[0]) ans = [] for c, lst in d.items(): if lst[0] == popularity: ans_id = "" ans_v = -1 for i, v in lst[1].items(): if v > ans_v: ans_id = i ans_v = v elif v == ans_v: ans_id = min(ans_id, i) ans.append([c, ans_id]) return ans
most-popular-video-creator
Dictionary for creators, 100% speed
EvgenySH
0
8
most popular video creator
2,456
0.441
Medium
33,663
https://leetcode.com/problems/most-popular-video-creator/discuss/2760646/straight-solution-with-python3-%2B-two-dict
class Solution: def mostPopularCreator(self, creators, ids, views):# List[int]) -> List[List[str]]: d = collections.defaultdict(dict) c = collections.defaultdict(int) res = list() for i in range(len(creators)): d[creators[i]][ids[i]] = views[i] for i in range(len(creators)): c[creators[i]] += views[i] max_viewers= max(c.values()) max_creators = [k for k, v in c.items() if v == max_viewers] for creator in max_creators: t = [creator] max_v = max(d[creator].values()) t.append(min([k for k, v in d[creator].items() if v == max_v])) res.append(t) return res
most-popular-video-creator
straight solution with python3 + two dict
kingfighters
0
16
most popular video creator
2,456
0.441
Medium
33,664
https://leetcode.com/problems/most-popular-video-creator/discuss/2759539/easy-python-solutionororeasy-to-understand
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: d1 = {} d2 = {} s = '' ans = [] for i in range(len(creators)): if creators[i] not in d1: d1[creators[i]] = [ids[i],views[i]] else: if views[i]>d1[creators[i]][1]: d1[creators[i]] = [ids[i],views[i]] elif views[i]==d1[creators[i]][1]: s= min(ids[i],d1[creators[i]][0]) d1[creators[i]] = [s,views[i]] for i in range(len(creators)): if creators[i] not in d2: d2[creators[i]]=views[i] else: d2[creators[i]]+=views[i] l = list(d2.values()) maxviw = max(l) for ele in d2: if d2[ele]==maxviw: ans.append([ele,d1[ele][0]]) return ans
most-popular-video-creator
✅✅easy python solution||easy to understand
chessman_1
0
22
most popular video creator
2,456
0.441
Medium
33,665
https://leetcode.com/problems/most-popular-video-creator/discuss/2758959/Python-O(n)-by-dictionary-w-Comment
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: # Constant to help reader understand code _ID, _View = 0, 1 # key: creator # value: total view count creatorView = defaultdict(int) # key: creator # value: best video with maximal view count, and lexical smallest video ID creatorBestVid = defaultdict( lambda: ("Z_Dummy", -1) ) maxViewsOfCreator = -1 popCreators = set() for creator, ID, view in zip( creators, ids, views): # update total video view of current creator creatorView[creator] += view # update most popular creator if( creatorView[creator] == maxViewsOfCreator): popCreators.add(creator) elif creatorView[creator] > maxViewsOfCreator: popCreators.clear() popCreators.add(creator) # update maximal view among different creators maxViewsOfCreator = max( maxViewsOfCreator, creatorView[creator]) # update best video for each creator if view > creatorBestVid[creator][_View] or \ ( view == creatorBestVid[creator][_View] and ID < creatorBestVid[creator][_ID] ): creatorBestVid[creator] = (ID, view) return [ [ star, creatorBestVid[star][_ID] ] for star in popCreators ]
most-popular-video-creator
Python O(n) by dictionary [w/ Comment]
brianchiang_tw
0
21
most popular video creator
2,456
0.441
Medium
33,666
https://leetcode.com/problems/most-popular-video-creator/discuss/2758844/Python-3-Dictionaries-or-Easy-Implementation-or-Small
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: vi, v = defaultdict(int), defaultdict(list) for i in range(len(ids)): vi[creators[i]] += views[i] v[creators[i]].append([ids[i], views[i]]) max_v = max(vi.values()) ans = [] for x in v: if vi[x] == max_v: p, y = v[x][0][0], v[x][0][1] for i in range(len(v[x])): if v[x][i][1] > y: y = v[x][i][1] p = v[x][i][0] elif v[x][i][1] == y: if v[x][i][0] < p: p= v[x][i][0] ans.append([x, p]) return ans
most-popular-video-creator
[Python 3] Dictionaries | Easy Implementation | Small
Rajesh1809
0
14
most popular video creator
2,456
0.441
Medium
33,667
https://leetcode.com/problems/most-popular-video-creator/discuss/2758350/Python-O(n)-SpaceTime-Solution
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: max_total_views = 0 total_views = collections.defaultdict(int) # track each creator's total view count for all videos most_viewed = {} # track each creator's most viewed video in the form of a tuple -> (video_view_count, video_id) for i in range(len(creators)): total_views[creators[i]] += views[i] max_total_views = max(total_views[creators[i]], max_total_views) # if current creator not seen before OR # current video has more views than creator's previous most viewed video OR # videos have equal views but id is lexicographically smaller if (creators[i] not in most_viewed or views[i] > most_viewed[creators[i]][0] or (views[i] == most_viewed[creators[i]][0] and ids[i] < most_viewed[creators[i]][1])): most_viewed[creators[i]] = (views[i], ids[i]) return [[creator, most_viewed[creator][1]] for creator, views in total_views.items() if views == max_total_views]
most-popular-video-creator
Python O(n) Space/Time Solution
whatdoyumin
0
23
most popular video creator
2,456
0.441
Medium
33,668
https://leetcode.com/problems/most-popular-video-creator/discuss/2758338/Python3-Dictionary-and-Dictionary-of-Dictionary
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: autors={} popularity={} for a,i,v in zip(creators,ids,views): if a not in autors: autors[a]={} popularity[a]=0 autors[a][i]=v popularity[a]+=v answ=[] m=max(popularity.values()) for a,v in popularity.items(): if v==m: val=autors[a].values() k=max(val) q=min(i for i,v in autors[a].items() if v==k) answ.append([a,q]) return answ
most-popular-video-creator
Python3, Dictionary and Dictionary of Dictionary
Silvia42
0
11
most popular video creator
2,456
0.441
Medium
33,669
https://leetcode.com/problems/most-popular-video-creator/discuss/2758303/Simple-Python-O(n)
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: table = defaultdict(lambda: defaultdict(int)) for h, v, c in zip(creators, ids, views): table[h]['_'] += c table[h][v] = c max_count = max(table[h]['_'] for h in table.keys()) res = [] for h in table.keys(): if table[h]['_'] == max_count: pair = min([[-c, v] for v, c in table[h].items() if v != '_']) res.append([h, pair[1]]) return res
most-popular-video-creator
Simple Python O(n)
ShuaSomeTea
0
17
most popular video creator
2,456
0.441
Medium
33,670
https://leetcode.com/problems/most-popular-video-creator/discuss/2758198/Python-Clean-Python-Solution-or-Dictionary-Counter
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: creator_total_views = Counter() d = collections.defaultdict(list) max_views = 0 for i in range(len(creators)): creator_total_views[creators[i]]+=views[i] d[creators[i]].append((-views[i],ids[i])) max_views = max(max_views,creator_total_views[creators[i]]) res = [] for creator,v in d.items(): if creator_total_views[creator] == max_views: res.append([creator,min(v)[1]]) return res
most-popular-video-creator
[Python] Clean Python Solution | Dictionary, Counter
daftpnk
0
26
most popular video creator
2,456
0.441
Medium
33,671
https://leetcode.com/problems/most-popular-video-creator/discuss/2758196/Python3-or-Hashmap-%2B-Sorting
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: hmap=defaultdict(list) hmap_view=defaultdict(int) n=len(ids) for i in range(n): hmap[creators[i]].append([ids[i],views[i]]) hmap_view[creators[i]]+=views[i] c=[] mx=max(hmap_view.values()) for i,j in hmap_view.items(): if j==mx: c.append(i) ans=[] for cr in c: tmp=[cr] tmp.append(sorted(hmap[cr],key=lambda x:(-x[1],x[0]))[0][0]) ans.append(tmp) return ans
most-popular-video-creator
[Python3] | Hashmap + Sorting
swapnilsingh421
0
16
most popular video creator
2,456
0.441
Medium
33,672
https://leetcode.com/problems/most-popular-video-creator/discuss/2758113/Python3-hash-table
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: mp = defaultdict(list) total = defaultdict(int) for c, i, v in zip(creators, ids, views): mp[c].append((-v, i)) total[c] += v ans = [] most = max(total.values()) for c, x in total.items(): if x == most: ans.append([c, min(mp[c])[1]]) return ans
most-popular-video-creator
[Python3] hash table
ye15
0
19
most popular video creator
2,456
0.441
Medium
33,673
https://leetcode.com/problems/most-popular-video-creator/discuss/2758083/Python3-or-Dictionary
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: has={} for i in range(len(creators)): if creators[i] in has: has[creators[i]][0]+=views[i] if has[creators[i]][2]<views[i]: has[creators[i]][1]=ids[i] has[creators[i]][2]=views[i] elif has[creators[i]][2]==views[i]: has[creators[i]][1]=min(ids[i],has[creators[i]][1]) else: has[creators[i]]=[views[i],ids[i],views[i]] ma=0 ans=[] for i in has: ma=max(has[i][0],ma) for i in has: if has[i][0]==ma: ans.append([i,has[i][1]]) return ans
most-popular-video-creator
Python3 | Dictionary
RickSanchez101
0
33
most popular video creator
2,456
0.441
Medium
33,674
https://leetcode.com/problems/most-popular-video-creator/discuss/2758065/Python-solution-with-dict
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: m = {} for i in range(len(creators)): creator = creators[i] view = views[i] id = ids[i] if creator not in m: m[creator] = {"tview": view, "id": [id], "view": view} else: mcreator = m[creator] mcreator["tview"] += view if mcreator["view"] < view: mcreator["id"] = [id] mcreator["view"] = view elif mcreator["view"] == view: mcreator["id"].append(id) hview = 0 c = [] for creator, cmap in m.items(): if cmap["tview"] > hview: hview = cmap["tview"] c = [creator] elif cmap["tview"] == hview: c.append(creator) answer = [] for ans in c: aids = m[ans]["id"] aids.sort() answer.append([ans, aids[0]]) return answer
most-popular-video-creator
Python solution with dict
pradyumna04
0
23
most popular video creator
2,456
0.441
Medium
33,675
https://leetcode.com/problems/most-popular-video-creator/discuss/2758047/Python-or-O(n)
class Solution: def mostPopularCreator(self, creators: List[str], ids: List[str], views: List[int]) -> List[List[str]]: total_views = {} first_max_view = {} for i in range(len(creators)): total_views[creators[i]] = total_views.get(creators[i], 0)+views[i] if creators[i] in first_max_view: if views[i] > views[first_max_view[creators[i]]]: #update value with highest view id first_max_view[creators[i]] = i elif views[i] == views[first_max_view[creators[i]]]: #if views of some video are equal update with lexicographically smallest id. if ids[i] < ids[first_max_view[creators[i]]]: first_max_view[creators[i]] = i else: first_max_view[creators[i]] = i max_views = max(total_views.values()) ans = [] for name in total_views: if total_views[name] == max_views: #select most popular video creators ans.append([name, ids[first_max_view[name]]]) return ans
most-popular-video-creator
Python | O(n)
diwakar_4
-1
35
most popular video creator
2,456
0.441
Medium
33,676
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758079/Check-next-10-next-100-next-1000-and-so-on...
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: i=n l=1 while i<=10**12: s=0 for j in str(i): s+=int(j) if s<=target: return i-n i//=10**l i+=1 i*=10**l l+=1
minimum-addition-to-make-integer-beautiful
Check next 10, next 100, next 1000 and so on...
shreyasjain0912
2
97
minimum addition to make integer beautiful
2,457
0.368
Medium
33,677
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2844877/Python3-Next-multiple-of-Ten-Digit-Accumulation
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: # the sum of digits by addition only gets smaller if we # get zeros certain digits by going to the next power # of ten for that digit, which increases the previous # digit by one # lets get the digits of the number digits = [int(ch) for ch in list(str(n))] # compute the prefix sum for the digits digits_acc = list(itertools.accumulate(digits)) # check the last sum to immediately break if digits_acc[-1] <= target: return 0 # go through the digits and check when we are lower than # the target found = False for idx in range(len(digits)-1, -1, -1): if digits_acc[idx] < target: found = True break # now get the number if found: number = reduce(lambda x, y: x*10 + y, digits[:idx+1]) + 1 number *= 10**(len(digits)-idx-1) else: number = 10**(len(digits)) return number - n
minimum-addition-to-make-integer-beautiful
[Python3] - Next multiple of Ten - Digit Accumulation
Lucew
0
1
minimum addition to make integer beautiful
2,457
0.368
Medium
33,678
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2776113/Easy-python-solution-with-good-comments
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: #function to get sum of the number def getsum(n): return sum([int(i) for i in str(n)]) #for the current digit place, if ones the 0, if tens then 1 as 10**1 =10 level=0 #storing ans toadd=0 #calling function to get sum intially of n sm=getsum(n) #we need to loop till the sum is greater than target #take example of 467 while(sm > target): #pick the last digit last = n%10 #last == 464%10 => 7 n+=(10-last) #467+=(10-7) == 470 n=n//10 #n==470//10 =>47 toadd+=(10**level)*(10-last) #toadd= (10**0)*(10-7)=>3 level+=1 #level is now 1, for next iteration since we modified n only we need to keep #track of current digit place sm = getsum(n) #check the current sum again return toadd
minimum-addition-to-make-integer-beautiful
Easy python solution with good comments
user9781TM
0
9
minimum addition to make integer beautiful
2,457
0.368
Medium
33,679
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2766456/One-pass-over-digits-97-speed
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: lst_n = list(map(int, str(n))) lst_n.reverse() ans = [] i = 0 while sum(lst_n) > target: if lst_n[i] > 0: diff = 10 - lst_n[i] ans.append(diff) lst_n[i] = 0 if i + 1 < len(lst_n): lst_n[i + 1] += 1 else: lst_n.append(1) else: ans.append(0) i += 1 if ans: return sum(v * pow(10, i) for i, v in enumerate(ans)) return 0
minimum-addition-to-make-integer-beautiful
One pass over digits, 97% speed
EvgenySH
0
19
minimum addition to make integer beautiful
2,457
0.368
Medium
33,680
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2766161/Python-Easy-Intuitive-Explanation
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: def helper(x): total = 0 while x > 0: total += x % 10 x //= 10 return total res = 0 base = 1 while helper(n + res) > target: base = base * 10 res = base - (n % base) return res
minimum-addition-to-make-integer-beautiful
Python Easy Intuitive Explanation
MaverickEyedea
0
7
minimum addition to make integer beautiful
2,457
0.368
Medium
33,681
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2760295/Python3-Greedy-Algorithm
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: if sum([int(x) for x in str(n)]) <= target: return 0 else: arr = [int(x) for x in str(n)] arr.insert(0, 0) current = sum(arr) while True: for i in range(len(arr)-1, 0, -1): if sum(arr) <= target: return int(''.join([str(x) for x in arr])) - n else: if arr[i] != 0: current -= arr[i] current += 1 arr[i-1] += 1 start = i-1 while start >= 1: if arr[start] == 10: arr[start-1] += 1 arr[start] = 0 else: break start -= 1 arr[i] = 0
minimum-addition-to-make-integer-beautiful
Python3 Greedy Algorithm
xxHRxx
0
9
minimum addition to make integer beautiful
2,457
0.368
Medium
33,682
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758724/python3-Recursion-solution-for-reference
class Solution: def countDigits(self, n): c = 0 zdigit = 0 s = 0 while n: digit = n%10 s += digit n = n//10 c += 1 if digit and not zdigit: z = 10**(c-1) zdigit = (10*z-z*digit) return zdigit, s def makeIntegerBeautiful(self, n: int, target: int) -> int: zdigit, sm = self.countDigits(n) if sm <= target: return 0 return zdigit + self.makeIntegerBeautiful(n+zdigit, target)
minimum-addition-to-make-integer-beautiful
[python3] Recursion solution for reference
vadhri_venkat
0
12
minimum addition to make integer beautiful
2,457
0.368
Medium
33,683
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758679/Prefix-sum-solution-O(n)-in-python-3
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: if n<=target: return 0 nsp = [int(k) for k in str(n)] prefix_sum = [0]*len(nsp) for i in range(len(nsp)): if i == 0: prefix_sum[i] = nsp[i] else: prefix_sum[i] = prefix_sum[i-1]+ nsp[i] #Edge case if prefix_sum[-1] <= target: return 0 for pos in range(len(prefix_sum)): if prefix_sum[pos]+1>target: break if pos == 0: #example 209 vs 2, has to become 1000, so 1000-209 is the answer res = int('1'+len(nsp)*'0') - n else: #example 109 vs 2, just need to become 110 res = int('1'+(len(nsp)-pos)*'0') - int(str(n)[pos:]) return res
minimum-addition-to-make-integer-beautiful
Prefix sum solution O(n) in python 3
Arana
0
4
minimum addition to make integer beautiful
2,457
0.368
Medium
33,684
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758567/Simple-math
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: def sumofc(num): s = 0 while num != 0: s += num % 10 num = int(num/10) return s ans = 0 tmp = n mult = 1 #keep making last digit zero till sum of digits is less than target while sumofc(tmp) > target: lastn = tmp % 10 tmp = int(tmp/10) + 1 #making first digit of our answer ans = ans + mult * (10 - lastn) multp = mult * 10 return ans
minimum-addition-to-make-integer-beautiful
Simple math
praneeth_357
0
12
minimum addition to make integer beautiful
2,457
0.368
Medium
33,685
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758297/Python-Answer-Cheesed-solution
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: def getnum(num): s = str(num) total = 0 for c in s: total += int(c) return total t= getnum(n) st = str(n) res = 0 index = len(st) - 1 rindex = 0 while t > target: st = str(n) extra = (10 - int(st[index]) ) * (10**rindex) res += extra index -= 1 rindex += 1 n += extra t = getnum(n) return res return -1
minimum-addition-to-make-integer-beautiful
[Python Answer🤫🐍🐍🐍] Cheesed solution
xmky
0
7
minimum addition to make integer beautiful
2,457
0.368
Medium
33,686
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758269/Python3-Simple-and-Quick
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: i,d=n,10 while 1: if sum(int(ch) for ch in str(i)) <= target: return i-n i=(i//d)*d+d d*=10
minimum-addition-to-make-integer-beautiful
Python3, Simple and Quick
Silvia42
0
4
minimum addition to make integer beautiful
2,457
0.368
Medium
33,687
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758246/Python-solution-using-strings
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: if sum([int(x) for x in str(n)]) <= target: return 0 x = s = 0 a = str(n) for i in range(len(a)): s += int(a[i]) if s >= target: break x += 1 return int("1" + len(a[x:]) * "0") - int(a[x:])
minimum-addition-to-make-integer-beautiful
Python solution using strings
ayamdobhal
0
11
minimum addition to make integer beautiful
2,457
0.368
Medium
33,688
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758238/Python-100-faster
class Solution(object): def makeIntegerBeautiful(self, n, target): def round_up(n, decimals = 0): multiplier = 10 ** decimals return math.ceil(n * multiplier) / multiplier ln=len(str(n)) # print round_up(19,-2) def sumi(m): l=[] for i in str(m): l.append(int(i)) # print l return sum(l) if sumi(n)<=target: # print l return 0 else: # print l,round_up(n,-(ln-1)),n if ln>1: temp=n for i in range(1,15): temp=int(round(round_up(n,-(i-1)))-n) + n # print temp # print(temp,sumi(int(temp)),round(round_up(n,-(i-1))),n) if sumi(int(temp))<=target: return abs(n-temp) else: return int(round_up(n,-1)-n)
minimum-addition-to-make-integer-beautiful
Python 100% faster
singhdiljot2001
0
19
minimum addition to make integer beautiful
2,457
0.368
Medium
33,689
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758208/Python3-or-Greedy-%2B-Maths
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: n=list(map(int,str(n))) curr=sum(n) ans=0 i=0 n.reverse() while curr>target: d=10-n[i] ans+=d*(10**i) curr=curr-n[i]+1 if i<len(n)-1: n[i+1]+=1 i+=1 return ans
minimum-addition-to-make-integer-beautiful
[Python3] | Greedy + Maths
swapnilsingh421
0
11
minimum addition to make integer beautiful
2,457
0.368
Medium
33,690
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758134/Python-or-O(logn)
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: def isLess(n): digit_sum = 0 while n!=0: digit_sum += n%10 n //= 10 return True if (digit_sum <= target) else False ans = 0 digit_position = 10 while 1: if isLess(n): return ans else: required = digit_position-(n%digit_position) n += required ans += required digit_position *= 10 return ans
minimum-addition-to-make-integer-beautiful
Python | O(logn)
diwakar_4
0
17
minimum addition to make integer beautiful
2,457
0.368
Medium
33,691
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758125/Python3-check-digits
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: diff = i = 0 while sum(map(int, str(n+diff))) > target: i += 1 diff = 10**i - n % 10**i return diff
minimum-addition-to-make-integer-beautiful
[Python3] check digits
ye15
0
14
minimum addition to make integer beautiful
2,457
0.368
Medium
33,692
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758121/Intuitive-solution-within-10-lines
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: num = [int(i) for i in str(n)] length, cur, out = len(num), 0, 0 for i in range(length): cur+=num[i] if cur>target or (cur==target and i<length-1 and sum(num[i+1:])>0): goal = int('1'+'0'*(length-i)) out = goal-int(''.join([str(k) for k in num[i:]])) break return out
minimum-addition-to-make-integer-beautiful
Intuitive solution within 10 lines
30qwwq
0
10
minimum addition to make integer beautiful
2,457
0.368
Medium
33,693
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758082/Python-3-Handling-from-right-oror-Time%3A-44-ms-Space%3A-13.9-MB
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: def calc(s): ans=0 for i in s: ans+=int(i) return ans ans="" t=str(n) s=calc(t) while(s>target): i=len(t)-1 while(t[i]=="0"): i-=1 k=10-int(t[i]) ans+=str(10-int(t[i])) t=str(int(t)+k*pow(10,len(t)-i-1)) s=calc(t) i-=1 return int(t)-n
minimum-addition-to-make-integer-beautiful
Python 3 Handling from right || Time: 44 ms , Space: 13.9 MB
koder_786
0
15
minimum addition to make integer beautiful
2,457
0.368
Medium
33,694
https://leetcode.com/problems/minimum-addition-to-make-integer-beautiful/discuss/2758028/Python-simple-code-explained
class Solution: def makeIntegerBeautiful(self, n: int, target: int) -> int: #function to get sum of digits def sumofc(num): s = 0 while num != 0: s += num % 10 num = int(num/10) return s sumc = sumofc(n) ans = 0 tmpn = n multp = 1 #keep making last digit zero till sum of digits is less than target while sumc > target: lastn = tmpn % 10 tmpn = int(tmpn/10) + 1 ans = ans + multp * (10 - lastn) sumc = sumofc(tmpn) multp = multp * 10 return ans
minimum-addition-to-make-integer-beautiful
Python simple code explained
pradyumna04
0
17
minimum addition to make integer beautiful
2,457
0.368
Medium
33,695
https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/discuss/2760795/Python3-Triple-dict-depth-height-nodes_at_depth
class Solution: def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]: depth = {} height = {} nodes_at_depth = {} max_height = 0 def rec(n, d): nonlocal max_height if n is None: return 0 height_below = max(rec(n.left, d+1), rec(n.right, d+1)) v = n.val depth[v] = d h = d + 1 + height_below height[v] = h max_height = max(max_height, h) if d not in nodes_at_depth: nodes_at_depth[d] = [v] else: nodes_at_depth[d].append(v) return 1 + height_below rec(root, -1) # subtract one because the problem reports heights weird ret = [] for q in queries: if height[q] >= max_height: d = depth[q] for cousin in nodes_at_depth[depth[q]]: if cousin != q: # don't count self, obviously d = max(d, height[cousin]) ret.append(d) else: ret.append(max_height) return ret
height-of-binary-tree-after-subtree-removal-queries
Python3 Triple dict depth, height, nodes_at_depth
godshiva
0
12
height of binary tree after subtree removal queries
2,458
0.354
Hard
33,696
https://leetcode.com/problems/height-of-binary-tree-after-subtree-removal-queries/discuss/2758144/Python3-heights-by-level
class Solution: def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]: depth = {} height = {0 : 0} def fn(node, d): if not node: return 0 depth[node.val] = d height[node.val] = 1 + max(fn(node.left, d+1), fn(node.right, d+1)) return height[node.val] h = fn(root, 0) level = [[0, 0] for _ in range(h)] for k, v in depth.items(): if height[k] >= height[level[v][0]]: level[v] = [k, level[v][0]] elif height[k] > height[level[v][1]]: level[v][1] = k ans = [] for q in queries: d = depth[q] if q == level[d][0]: ans.append(h-1-height[q]+height[level[d][1]]) else: ans.append(h-1) return ans
height-of-binary-tree-after-subtree-removal-queries
[Python3] heights by level
ye15
0
56
height of binary tree after subtree removal queries
2,458
0.354
Hard
33,697
https://leetcode.com/problems/apply-operations-to-an-array/discuss/2786435/Python-Simple-Python-Solution-89-ms
class Solution: def applyOperations(self, nums: List[int]) -> List[int]: for i in range(len(nums)-1): if nums[i]==nums[i+1]: nums[i]*=2 nums[i+1]=0 temp = [] zeros = [] a=nums for i in range(len(a)): if a[i] !=0: temp.append(a[i]) else: zeros.append(a[i]) return (temp+zeros)
apply-operations-to-an-array
[ Python ] 🐍🐍 Simple Python Solution ✅✅ 89 ms
sourav638
3
31
apply operations to an array
2,460
0.671
Easy
33,698
https://leetcode.com/problems/apply-operations-to-an-array/discuss/2817847/Easy-Python-Solution
class Solution: def applyOperations(self, nums: List[int]) -> List[int]: l=[] c=0 for i in range(len(nums)-1): if(nums[i]==nums[i+1]): nums[i]=nums[i]*2 nums[i+1]=0 for i in nums: if i!=0: l.append(i) else: c+=1 return l+[0]*c
apply-operations-to-an-array
Easy Python Solution
Vistrit
2
30
apply operations to an array
2,460
0.671
Easy
33,699