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/maximum-bags-with-full-capacity-of-rocks/discuss/2215447/Python-simple-solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: empty = sorted([c-r for c,r in zip(capacity, rocks)]) ans = 0 for i in empty: if i == 0: ans += 1 elif i <= additionalRocks: additionalRocks -= i ans += 1 elif i > additionalRocks: break return ans
maximum-bags-with-full-capacity-of-rocks
Python simple solution
StikS32
0
5
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,500
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2135384/Python-oror-Straight-Forward
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: n, tofill = len(capacity), defaultdict(int) for i in range(n): tofill[i] = capacity[i] - rocks[i] k, v = tofill.keys(), tofill.values() bags, i, filled = sorted(v), 0, 0 while additionalRocks and i < n: if additionalRocks >= bags[i]: additionalRocks -= bags[i] filled += 1 i += 1 else: return filled return filled
maximum-bags-with-full-capacity-of-rocks
Python || Straight Forward
morpheusdurden
0
16
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,501
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2116182/Python-Easy-to-understand-Solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: n = len(capacity) minus_list = [] for i in range(n): minus_list.append(capacity[i] - rocks[i]) minus_list.sort() add_rocks = 0 ans = 0 while ans < len(minus_list) and add_rocks < additionalRocks: add_rocks += minus_list[ans] ans += 1 if add_rocks > additionalRocks: return ans - 1 else: return ans
maximum-bags-with-full-capacity-of-rocks
✅Python Easy-to-understand Solution
chuhonghao01
0
13
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,502
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2071568/python-3-oror-simple-greedy-solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: n = len(capacity) for i in range(n): capacity[i] -= rocks[i] capacity.sort() for i, additonalCapacity in enumerate(capacity): additionalRocks -= additonalCapacity if additionalRocks < 0: return i return n
maximum-bags-with-full-capacity-of-rocks
python 3 || simple greedy solution
dereky4
0
36
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,503
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2068291/Python3-greedy
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: ans = 0 for x in sorted(c - r for c, r in zip(capacity, rocks)): if x <= additionalRocks: ans += 1 additionalRocks -= x return ans
maximum-bags-with-full-capacity-of-rocks
[Python3] greedy
ye15
0
21
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,504
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2066020/java-python-small-and-fast-(sorting)
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: for i in range(len(capacity)): capacity[i] -= rocks[i] capacity.sort() for i in range(len(capacity)): if capacity[i] > additionalRocks : return i additionalRocks -= capacity[i] return len(capacity)
maximum-bags-with-full-capacity-of-rocks
java, python - small & fast (sorting)
ZX007java
0
15
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,505
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2061929/Very-simple-and-easy-to-understand-Heap-Solution%3A-Python
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: heap = [] answer = 0 n = len(rocks) for i in range(n): heappush(heap, ( capacity[i] - rocks[i], i )) while heap: remainingCapacity, index = heappop(heap) if remainingCapacity == 0: answer += 1 if 0 < remainingCapacity <= additionalRocks and additionalRocks > 0: heappush(heap, ( 0, index )) additionalRocks -= remainingCapacity return answer
maximum-bags-with-full-capacity-of-rocks
Very simple and easy to understand Heap Solution: Python
tyrocoder
0
15
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,506
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2061893/Python-or-Easy-to-Understand
class Solution: def minimumLines(self, stockPrices: List[List[int]]) -> int: # key point: never use devision to judge whether 3 points are on a same line or not, use the multiplication instead !! n = len(stockPrices) stockPrices.sort(key = lambda x: (x[0], x[1])) if n == 1: return 0 pre_delta_y = stockPrices[0][1] - stockPrices[1][1] pre_delta_x = stockPrices[0][0] - stockPrices[1][0] num = 1 for i in range(1, n-1): cur_delta_y = stockPrices[i][1] - stockPrices[i+1][1] cur_delta_x = stockPrices[i][0] - stockPrices[i+1][0] if pre_delta_y * cur_delta_x != pre_delta_x * cur_delta_y: num += 1 pre_delta_x = cur_delta_x pre_delta_y = cur_delta_y return num
minimum-lines-to-represent-a-line-chart
Python | Easy to Understand
Mikey98
11
548
minimum lines to represent a line chart
2,280
0.238
Medium
31,507
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2061827/PYTHON-SIMPLE-SOLUTION-or-EASY-TO-UNDERSTAND-LOGIC-or-SORTING
class Solution: def minimumLines(self, stockPrices: List[List[int]]) -> int: if len(stockPrices) == 1: return 0 stockPrices.sort(key = lambda x: x[0]) ans = 1 for i in range(1,len(stockPrices)-1): if (stockPrices[i+1][1]-stockPrices[i][1])*(stockPrices[i][0]-stockPrices[i-1][0]) != (stockPrices[i+1][0]-stockPrices[i][0])*(stockPrices[i][1]-stockPrices[i-1][1]): ans += 1 return ans
minimum-lines-to-represent-a-line-chart
PYTHON SIMPLE SOLUTION | EASY TO UNDERSTAND LOGIC | SORTING
AkashHooda
2
74
minimum lines to represent a line chart
2,280
0.238
Medium
31,508
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2097954/PYTHON-oror-EXPLAINED-oror-EASY
class Solution: def minimumLines(self, a: List[List[int]]) -> int: if len(a)<=1: return 0 a.sort() ans=1 for i in range(2,len(a)): if ((a[i-1][1]-a[i-2][1])*(a[i][0]-a[i-1][0]))!=((a[i][1]-a[i-1][1])*(a[i-1][0]-a[i-2][0])): ans+=1 return ans
minimum-lines-to-represent-a-line-chart
✔️PYTHON || ✔️EXPLAINED || EASY
karan_8082
1
76
minimum lines to represent a line chart
2,280
0.238
Medium
31,509
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2572896/Concise-Python-Implementation-with-GCD
class Solution: def gcd(self, a, b): while b != 0: a, b = b, a % b return a def compute_slope(self, p1, p2): slope_x, slope_y = p2[0] - p1[0], p2[1] - p1[1] g = self.gcd(slope_x, slope_y) return (slope_x // g, slope_y // g) def minimumLines(self, points: List[List[int]]) -> int: points.sort() ans = 0 slope = (0, 0) for i in range(1, len(points)): new_slope = self.compute_slope(points[i-1], points[i]) if new_slope != slope: ans += 1 slope = new_slope return ans
minimum-lines-to-represent-a-line-chart
Concise Python Implementation with GCD
metaphysicalist
0
27
minimum lines to represent a line chart
2,280
0.238
Medium
31,510
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2135385/Python-oror-Straight-Forward
class Solution: def minimumLines(self, stockPrices: List[List[int]]) -> int: n = len(stockPrices) if n == 2: return 1 if n == 1: return 0 day, price = [],[] for d, p in stockPrices: day.append(d) price.append(p) stocks = sorted(zip(day,price)) line = 1 for i in range(1,n-1): p1, p2, p3 = stocks[i-1], stocks[i], stocks[i+1] if (p1[1]-p2[1])*(p2[0]-p3[0]) != (p2[1]-p3[1])*(p1[0]-p2[0]): line += 1 return line
minimum-lines-to-represent-a-line-chart
Python || Straight Forward
morpheusdurden
0
74
minimum lines to represent a line chart
2,280
0.238
Medium
31,511
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2116173/Python-Easy-to-understand-Solution
class Solution: def minimumLines(self, stockPrices: List[List[int]]) -> int: stockPrices.sort() n = len(stockPrices) ans = 1 i = 0 if n < 2: return 0 if len(stockPrices) == 0: return 0 while i + 2 < n: while ((i + 2) < n) and ((stockPrices[i + 2][1] - stockPrices[i + 1][1]) * (stockPrices[i + 1][0] - stockPrices[i][0]) == ((stockPrices[i + 1][1] - stockPrices[i][1]) * (stockPrices[i + 2][0] - stockPrices[i + 1][0]))): i += 1 if i + 2 >= n: break else: i += 1 ans += 1 return ans
minimum-lines-to-represent-a-line-chart
✅Python Easy-to-understand Solution
chuhonghao01
0
30
minimum lines to represent a line chart
2,280
0.238
Medium
31,512
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2068296/Python3-piece-by-piece
class Solution: def minimumLines(self, stockPrices: List[List[int]]) -> int: stockPrices.sort() ans = 0 for i in range(1, len(stockPrices)): if i == 1 or (stockPrices[i][1]-stockPrices[i-1][1])*(stockPrices[i-1][0]-stockPrices[i-2][0]) != (stockPrices[i-1][1]-stockPrices[i-2][1])*(stockPrices[i][0]-stockPrices[i-1][0]): ans += 1 return ans
minimum-lines-to-represent-a-line-chart
[Python3] piece by piece
ye15
0
17
minimum lines to represent a line chart
2,280
0.238
Medium
31,513
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2066085/python-java-c%2B%2B-easy-and-small-(commented)
class Solution: def minimumLines(self, s: List[List[int]]) -> int: if len(s) == 1 : return 0 s.sort() ans = 1 for i in range(2,len(s)) : if (s[i][0] - s[i-1][0])*(s[i-2][1] - s[i-1][1]) != (s[i][1] - s[i-1][1])*(s[i-2][0] - s[i-1][0]): ans+=1 return ans
minimum-lines-to-represent-a-line-chart
python, java, c++ - easy & small (commented)
ZX007java
0
20
minimum lines to represent a line chart
2,280
0.238
Medium
31,514
https://leetcode.com/problems/minimum-lines-to-represent-a-line-chart/discuss/2062112/Easy-python-code-using-slope
class Solution: def minimumLines(self, stockPrices: List[List[int]]) -> int: if len(stockPrices) == 1 : return 0 if len(stockPrices) == 2: return 1 number = 1 stockPrices.sort() for i in range(2, len(stockPrices)): if int((stockPrices[i][1]-stockPrices[i-1][1])*(stockPrices[i-1][0]-stockPrices[i-2][0])) !=\ int((stockPrices[i-1][1]-stockPrices[i-2][1])*(stockPrices[i][0]-stockPrices[i-1][0])): number = number + 1 return number ```
minimum-lines-to-represent-a-line-chart
Easy python code using slope
mdai26
0
26
minimum lines to represent a line chart
2,280
0.238
Medium
31,515
https://leetcode.com/problems/sum-of-total-strength-of-wizards/discuss/2373525/faster-than-98.90-or-easy-python-or-solution
class Solution: def totalStrength(self, strength: List[int]) -> int: strength = [0] + strength + [0] def calc_prefix_sum(array): if not array: return [] result = [array[0]] for el in array[1:]: result.append(array[-1]+el) return result prefix_sums = calc_prefix_sum(strength) pp_sums = calc_prefix_sum(prefix_sums) stack = [0] total = 0 for right in range(len(strength)): while pp_sums[stack[-1]] > pp_sums[right]: left = stack[-2] i = stack.pop() pos = (i - left) * (pp_sums[right] - pp_sums[i]) neg = (right - i) * (pp_sums[i] - pp_sums[left]) total += pp_sums[i] * (pos - neg) stack.push(right) return total % (10**9+7) def totalStrength(self, strength): res, S, A = 0, [0], [0] + strength + [0] # O(N) P = list(itertools.accumulate(itertools.accumulate(A), initial=0)) # O(N) for r in range(len(A)): # O(N) while A[S[-1]] > A[r]: # O(1) amortized l, i = S[-2], S.pop() res += A[i] * ((i - l) * (P[r] - P[i]) - (r - i) * (P[i] - P[l])) S.append(r) return res % (10 ** 9 + 7)
sum-of-total-strength-of-wizards
faster than 98.90% | easy python | solution
vimla_kushwaha
2
1,800
sum of total strength of wizards
2,281
0.279
Hard
31,516
https://leetcode.com/problems/sum-of-total-strength-of-wizards/discuss/2068305/Python3-mono-stack
class Solution: def totalStrength(self, strength: List[int]) -> int: ans = 0 stack = [] prefix = list(accumulate(accumulate(strength), initial=0)) for i, x in enumerate(strength + [0]): while stack and stack[-1][1] >= x: mid = stack.pop()[0] lo = stack[-1][0] if stack else -1 left = prefix[mid] - prefix[max(lo, 0)] right = prefix[i] - prefix[mid] ans = (ans + strength[mid]*(right*(mid-lo) - left*(i-mid))) % 1_000_000_007 stack.append((i, x)) return ans
sum-of-total-strength-of-wizards
[Python3] mono-stack
ye15
1
258
sum of total strength of wizards
2,281
0.279
Hard
31,517
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084112/Python-Easy-solution
class Solution: def digitCount(self, num: str) -> bool: counter=Counter(num) for i in range(len(num)): if counter[f'{i}'] != int(num[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python Easy solution
constantine786
3
142
check if number has equal digit count and digit value
2,283
0.735
Easy
31,518
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084641/Python-or-Easy-and-understanding-solution
class Solution: def digitCount(self, num: str) -> bool: n=len(num) for i in range(n): if(num.count(str(i))!=int(num[i])): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python | Easy & understanding solution
backpropagator
2
116
check if number has equal digit count and digit value
2,283
0.735
Easy
31,519
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2710543/Easty-Python
class Solution: def digitCount(self, s: str) -> bool: for i in range(len(s)): c=s.count(str(i)) if c==int(s[i]): continue else: return False return True
check-if-number-has-equal-digit-count-and-digit-value
[Easty Python ]
Sneh713
1
97
check if number has equal digit count and digit value
2,283
0.735
Easy
31,520
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084315/Clean-Python-Code-using-Counter
class Solution: def digitCount(self, num: str) -> bool: d = Counter(num) for idx, val in enumerate(num): if int(val) != d[str(idx)]: return False return True
check-if-number-has-equal-digit-count-and-digit-value
Clean Python Code using Counter
yzhao156
1
23
check if number has equal digit count and digit value
2,283
0.735
Easy
31,521
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2083886/Solution-using-hash-map-in-Python
class Solution: def digitCount(self, num: str) -> bool: leng = len(num) freq = {} for i in range(leng): freq[str(i)] = 0 for i in range(leng): if num[i] in freq: freq[num[i]] += 1 for i in range(leng): if num[i] == str(freq[str(i)]): continue else: return False return True
check-if-number-has-equal-digit-count-and-digit-value
Solution using hash map in Python
ReBirthing
1
73
check if number has equal digit count and digit value
2,283
0.735
Easy
31,522
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2752706/Python-one-line-without-count
class Solution: def digitCount(self, num: str) -> bool: return num in {'1210','21200','2020'} or num == str(len(num)-4) + '21' + '0' * (len(num)-7) + '1000'
check-if-number-has-equal-digit-count-and-digit-value
Python one line without count
Fredrick_LI
0
3
check if number has equal digit count and digit value
2,283
0.735
Easy
31,523
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2735627/Pyhton-or-Easy-Solution
class Solution: def digitCount(self, num: str) -> bool: num = [int(i) for i in num] for i in range(len(num)): if int(list(num).count(i)) != int(num[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Pyhton | Easy Solution
atharva77
0
1
check if number has equal digit count and digit value
2,283
0.735
Easy
31,524
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2583434/hashmap
class Solution: def digitCount(self, num: str) -> bool: # the index is the key # the element at the index dictates the freq # iterate over nums storing occurences of nums in d # iterate nums asserting that the values match # time O(n^2) space O(1) d = dict() n = len(num) for char in num: if char in d: d[char] += 1 else: d[char] = 1 for i in range(n): val, freq = str(i),int(num[i]) if val in d: if d[val] != freq: return False elif freq > 0: return False return True
check-if-number-has-equal-digit-count-and-digit-value
hashmap
andrewnerdimo
0
27
check if number has equal digit count and digit value
2,283
0.735
Easy
31,525
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2499371/Python3-Straightforward-One-liner
class Solution: def digitCount(self, num: str) -> bool: return all(num.count(str(i)) == int(num[i]) for i in range(len(num)))
check-if-number-has-equal-digit-count-and-digit-value
[Python3] Straightforward One-liner
ivnvalex
0
20
check if number has equal digit count and digit value
2,283
0.735
Easy
31,526
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2342188/Python-simple-and-fast-solution
class Solution: def digitCount(self, num: str) -> bool: for i in range(0, len(num)): if num[i] != str(num.count(str(i))): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python - simple and fast solution
kennytruong001
0
40
check if number has equal digit count and digit value
2,283
0.735
Easy
31,527
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2314996/Python3-freq-table
class Solution: def digitCount(self, num: str) -> bool: freq = Counter(map(int, str(num))) return all(freq[i] == int(ch) for i, ch in enumerate(str(num)))
check-if-number-has-equal-digit-count-and-digit-value
[Python3] freq table
ye15
0
18
check if number has equal digit count and digit value
2,283
0.735
Easy
31,528
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2296508/EASY-PYTHON-SOLUTION-oror-using-dictionary-oror
class Solution: def digitCount(self, num: str) -> bool: dic = {} for i in range(len(num)) : if num[i] in dic : dic[num[i]] += 1 else : dic[num[i]] = 0 if str(i) in dic : dic[str(i)] += 1 else : dic[str(i)] = 0 for i in range(len(num)) : if num[i] != str(dic[str(i)]) : return False return True
check-if-number-has-equal-digit-count-and-digit-value
EASY PYTHON SOLUTION || using dictionary ||
rohitkhairnar
0
19
check if number has equal digit count and digit value
2,283
0.735
Easy
31,529
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2296508/EASY-PYTHON-SOLUTION-oror-using-dictionary-oror
class Solution: def digitCount(self, num: str) -> bool: for i in range(len(num)): if(num.count(str(i))!=int(num[i])): return False return True
check-if-number-has-equal-digit-count-and-digit-value
EASY PYTHON SOLUTION || using dictionary ||
rohitkhairnar
0
19
check if number has equal digit count and digit value
2,283
0.735
Easy
31,530
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2208669/python-1-Liner-using-Counter
class Solution: def digitCount(self, num: str) -> bool: return all([Counter(num)[str(i)]==int(num[i]) for i in range(len(num))])
check-if-number-has-equal-digit-count-and-digit-value
python 1-Liner using Counter
XRFXRF
0
33
check if number has equal digit count and digit value
2,283
0.735
Easy
31,531
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2131537/Python-oror-Easy-Solution
class Solution: def digitCount(self, nums: str) -> bool: n = len(nums) count = defaultdict(int) for dig in nums: count[dig] += 1 for i in range(n): if count[str(i)] != int(nums[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python || Easy Solution
morpheusdurden
0
50
check if number has equal digit count and digit value
2,283
0.735
Easy
31,532
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2119335/Python3-easy-solution
class Solution: def digitCount(self, num: str) -> bool: new = Counter(num) for i in range(len(num)): if str(num[i]) != str(new[str(i)]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python3 easy solution
yashkumarjha
0
46
check if number has equal digit count and digit value
2,283
0.735
Easy
31,533
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2116157/Python-Easy-to-understand-Solution
class Solution: def digitCount(self, num: str) -> bool: for i in range(len(num)): s = str(i) if num.count(s) != int(num[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
✅Python Easy-to-understand Solution
chuhonghao01
0
20
check if number has equal digit count and digit value
2,283
0.735
Easy
31,534
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2109034/Python-3-Easy-Python-solution-for-Beginners
class Solution: def digitCount(self, num: str) -> bool: for i in range(len(num)): if num.count(str(i))!=int(num[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
[Python 3] Easy Python solution for Beginners
kushal2201
0
34
check if number has equal digit count and digit value
2,283
0.735
Easy
31,535
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2099803/Python-simple-solution
class Solution: def digitCount(self, num: str) -> bool: for ind, i in enumerate(num): if num.count(str(ind)) != int(i): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python simple solution
StikS32
0
27
check if number has equal digit count and digit value
2,283
0.735
Easy
31,536
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2094846/Python-easy-solution-for-beginners
class Solution: def digitCount(self, num: str) -> bool: for i in range(len(num)): if str(num.count(str(i))) != num[i]: return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python easy solution for beginners
alishak1999
0
22
check if number has equal digit count and digit value
2,283
0.735
Easy
31,537
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2094363/Python-Detailed-Solution
class Solution: def digitCount(self, num: str) -> bool: m = {} for digit in num: if digit in m: m[digit] += 1 else: m[digit] = 1 for i in range(len(num)): x = int(num[i]) if str(i) not in m: y = 0 else: y = m[str(i)] if x != y: return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python Detailed Solution
Hejita
0
20
check if number has equal digit count and digit value
2,283
0.735
Easy
31,538
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2088328/java-paython-easy-ASCII-solution
class Solution: def digitCount(self, num: str) -> bool: digits = [0]*10 for letter in num : digits[ord(letter) - 48] += 1 for i in range(len(num)) : idx = ord(num[i]) - 48 if digits[i] != idx : return False return True
check-if-number-has-equal-digit-count-and-digit-value
java, paython - easy ASCII solution
ZX007java
0
16
check if number has equal digit count and digit value
2,283
0.735
Easy
31,539
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084493/Python-2-lines-or-Counter-or-100-faster
class Solution: def digitCount(self, num: str) -> bool: # create counter map &amp; compare hm = Counter(map(int,num)) return all(int(num[i]) == hm.get(i,0) for i in range(len(num)))
check-if-number-has-equal-digit-count-and-digit-value
Python 2 lines | Counter | 100% faster
Nk0311
0
19
check if number has equal digit count and digit value
2,283
0.735
Easy
31,540
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084418/Python3-Simple-Solution
class Solution: def digitCount(self, num: str) -> bool: d = {} for i in num: n = int(i) d[n] = d.get(n,0) + 1 for i in range(len(num)): n = d.get(i,0) if n != int(num[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Python3 Simple Solution
abhijeetmallick29
0
14
check if number has equal digit count and digit value
2,283
0.735
Easy
31,541
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084363/Python-using-Counter
class Solution: def digitCount(self, num: str) -> bool: num = list(map(int, num)) c = Counter(num) return all(c[i] == d for i, d in enumerate(num))
check-if-number-has-equal-digit-count-and-digit-value
Python, using Counter
blue_sky5
0
28
check if number has equal digit count and digit value
2,283
0.735
Easy
31,542
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084301/Easy-Understanding-python-solution
class Solution: def digitCount(self, num: str) -> bool: flag = 0 for i in range(len(num)): if num.count(str(i)) == int(num[i]): flag=1 else: return False if flag == 1: return True
check-if-number-has-equal-digit-count-and-digit-value
Easy Understanding python solution
srija100
0
13
check if number has equal digit count and digit value
2,283
0.735
Easy
31,543
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084207/Easy-Python-solution
class Solution: def digitCount(self, num: str) -> bool: for i in range(len(num)): if int(num.count(str(i)))!=int(num[i]): return False return True
check-if-number-has-equal-digit-count-and-digit-value
Easy Python solution
a_dityamishra
0
11
check if number has equal digit count and digit value
2,283
0.735
Easy
31,544
https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/discuss/2084178/Python-3-Easy-Solution
class Solution: def digitCount(self, num: str) -> bool: for i in range(len(num)): if int(num[i]) != num.count(str(i)): return False return True
check-if-number-has-equal-digit-count-and-digit-value
[Python 3] - Easy Solution
hari19041
0
25
check if number has equal digit count and digit value
2,283
0.735
Easy
31,545
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084222/Easy-Python-Solution-With-Dictionary
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d={} l=[] for i in range(len(messages)): if senders[i] not in d: d[senders[i]]=len(messages[i].split()) else: d[senders[i]]+=len(messages[i].split()) x=max(d.values()) for k,v in d.items(): if v==x : l.append(k) if len(l)==1: return l[0] else: l=sorted(l)[::-1] #Lexigograhical sorting of list return l[0]
sender-with-largest-word-count
Easy Python Solution With Dictionary
a_dityamishra
7
356
sender with largest word count
2,284
0.561
Medium
31,546
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2643277/Python3-or-Hash-word-count-associated-with-a-sender's-name-then-return-max
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: words_count = defaultdict(int) for m, person in zip(messages, senders): words_count[person] += len(m.split()) max_len = max(words_count.values()) names = sorted([name for name, words in words_count.items() if words == max_len], reverse=True) return names[0]
sender-with-largest-word-count
Python3 | Hash word count associated with a sender's name then return max
Ploypaphat
1
43
sender with largest word count
2,284
0.561
Medium
31,547
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2151937/Python3-Clear-solution-using-dictionaries-for-word-count-per-sender
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: word_count = {} for message, sender in zip(messages, senders): message_word_count = message.count(" ") + 1 word_count[sender] = word_count.get(sender, 0) + message_word_count top_sender = "" max_count = 0 for sender, wc in word_count.items(): if wc > max_count or (wc == max_count and sender > top_sender ): top_sender = sender max_count = wc return top_sender
sender-with-largest-word-count
Python3 - Clear solution using dictionaries for word count per sender
ahmadheshamzaki
1
67
sender with largest word count
2,284
0.561
Medium
31,548
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084386/Python-Solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d = defaultdict(list) for m, s in zip(messages, senders): d[s].append(m) longest = [0, []] for s in d: l = 0 for m in d[s]: l += len(m.split()) if l > longest[0]: longest[0] = l longest[1] = [s] elif l == longest[0]: longest[1].append(s) return max(longest[1])
sender-with-largest-word-count
Python Solution
user6397p
1
26
sender with largest word count
2,284
0.561
Medium
31,549
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084321/Python3-or-5-Lines-or-Easy-understanding
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d = defaultdict(int) for sender, size in zip( senders, [len(message.split()) for message in messages] ): d[sender] += size max_word = max(d.values()) return sorted([sender for sender, size in d.items() if size == max_word])[-1]
sender-with-largest-word-count
Python3 | 5 Lines | Easy understanding
yzhao156
1
22
sender with largest word count
2,284
0.561
Medium
31,550
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2083973/Solution-in-Python-or-Hashmap-or-Sorting
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: leng = len(senders) freq = {} for i in range(leng): sender = senders[i] msg = messages[i] msg_len = len(msg.split()) if sender in freq: freq[sender] += msg_len else: freq[sender] = msg_len max_sender_len = float('-inf') max_sender_name = '' for sender in freq: if freq[sender] > max_sender_len: max_sender_len = freq[sender] max_sender_name = sender elif freq[sender] == max_sender_len: temp = sorted([sender, max_sender_name], key=str, reverse=True) max_sender_name = temp[0] return max_sender_name
sender-with-largest-word-count
Solution in Python | Hashmap | Sorting
ReBirthing
1
60
sender with largest word count
2,284
0.561
Medium
31,551
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2083912/Python-Easy-Solution-using-Hashing-and-Sorting
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: mapping, res = {}, [] for i, sender in enumerate(senders): if sender not in mapping: mapping[sender] = len(messages[i].split()) else: mapping[sender] += len(messages[i].split()) mapping = {val[0] : val[1] for val in sorted(mapping.items(), key = lambda x: (-x[1], x[0]))} max_val = max(mapping.values()) for k, v in mapping.items(): if v == max_val: res.append(k) if len(res) == 1: return res[0] res.sort(key = len) res.sort(key = str) return res[-1]
sender-with-largest-word-count
Python Easy Solution using Hashing and Sorting
MiKueen
1
43
sender with largest word count
2,284
0.561
Medium
31,552
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2083877/Python-Easy-Approach
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: counter = defaultdict(int) max_count = -math.inf res='' # find the word count tally for each sender for message, sender in zip(messages, senders): counter[sender]+=len(message.split(' ')) if counter[sender]>=max_count: # pick lexicographically larger name in case same word count if max_count<counter[sender] or sender>res: res=sender max_count=counter[sender] return res
sender-with-largest-word-count
Python Easy Approach
constantine786
1
56
sender with largest word count
2,284
0.561
Medium
31,553
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2830991/Python-hashmap-solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d = {} for i in range(len(senders)): if senders[i] in d.keys(): d[senders[i]] += len(messages[i].split()) else: d[senders[i]] = len(messages[i].split()) ans, m = "", 0 print(d) for key, value in d.items(): if value > m: m = value ans = key elif value == m: ans = max(ans, key) return ans
sender-with-largest-word-count
Python hashmap solution
AdvaitShukla
0
1
sender with largest word count
2,284
0.561
Medium
31,554
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2827234/Simple-Solution-using-Dictionary-in-Python
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: map = {} m=0 ans="" for i in range(len(senders)): if senders[i] not in map: map[senders[i]] = messages[i].count(' ')+1 else: map[senders[i]]+= messages[i].count(' ')+1 m=max(m,map[senders[i]]) for i,k in map.items(): if k==m: ans=max(ans,i) return ans
sender-with-largest-word-count
Simple Solution using Dictionary in Python
niketh_1234
0
1
sender with largest word count
2,284
0.561
Medium
31,555
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2812586/Simple-Python-Solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: count = collections.defaultdict(int) for i in range(len(senders)): count[senders[i]] += len(messages[i].split()) ans = [] for k, v in count.items(): if ans == [] or v > ans[0][1]: ans = [[k, v]] elif v == ans[0][1]: ans.append([k, v]) if len(ans) > 1: return sorted([ans[i][0] for i in range(len(ans))])[-1] return ans[0][0]
sender-with-largest-word-count
Simple Python Solution
akki2790
0
1
sender with largest word count
2,284
0.561
Medium
31,556
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2740029/Python3-Solution-with-using-hashmap
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d = collections.defaultdict(int) for idx, sender in enumerate(senders): space_cnt = 0 for char in messages[idx]: if char == " ": space_cnt += 1 d[sender] += space_cnt + 1 max_words = float('-inf') res = "" for sender in d: if d[sender] > max_words or (d[sender] == max_words and sender > res): res = sender max_words = d[sender] return res
sender-with-largest-word-count
[Python3] Solution with using hashmap
maosipov11
0
3
sender with largest word count
2,284
0.561
Medium
31,557
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2723526/Python-solution-beats-100-on-time-(340ms)-and-94.42-on-space-(21.2-MB)
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: a = {} for message, sender in zip(messages, senders): words = message.count(' ') + 1 if sender not in a: a[sender] = words else: a[sender] += words most = max(a.values()) ans = '' for sender in a: if a[sender] == most and sender > ans: ans = sender return ans
sender-with-largest-word-count
Python solution beats 100% on time (340ms) and 94.42% on space (21.2 MB)
dmonack
0
3
sender with largest word count
2,284
0.561
Medium
31,558
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2721547/PYTHON-Faster-than-99.82-oror-Easy-python-solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: hashmap = dict() for message, name in zip(messages, senders): if name in hashmap: hashmap[name] += len(message.split(" ")) else: hashmap[name] = len(message.split(" ")) maximum = max(hashmap.values()) return max([key for key, value in hashmap.items() if value == maximum])
sender-with-largest-word-count
[PYTHON] Faster than 99.82% || Easy python solution
valera_grishko
0
5
sender with largest word count
2,284
0.561
Medium
31,559
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2690573/Python-or-7-lines-or-Dictionary
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: counts = Counter() for mess, sender in zip(messages, senders): counts[sender] += mess.count(' ') + 1 ans = (float('-inf'), "") for sender, count in counts.items(): ans = max(ans, (count, sender)) return ans[1]
sender-with-largest-word-count
Python | 7 lines | Dictionary
on_danse_encore_on_rit_encore
0
4
sender with largest word count
2,284
0.561
Medium
31,560
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2547219/easy-python-solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: sender_dict = {} for i in range(len(senders)) : if senders[i] not in sender_dict.keys() : sender_dict[senders[i]] = len(messages[i].split()) else : sender_dict[senders[i]] += len(messages[i].split()) max_person, max_count = "", 0 for key in sender_dict.keys() : if sender_dict[key] > max_count : max_count = sender_dict[key] max_person = key if sender_dict[key] == max_count : if key > max_person : max_person = key return max_person
sender-with-largest-word-count
easy python solution
sghorai
0
22
sender with largest word count
2,284
0.561
Medium
31,561
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2315000/Python3-freq-table
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: freq = defaultdict(int) for m, s in zip(messages, senders): freq[s] += len(m.split()) return max(freq, key=lambda x: (freq[x], x))
sender-with-largest-word-count
[Python3] freq table
ye15
0
13
sender with largest word count
2,284
0.561
Medium
31,562
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2305816/Easy-python-Hashmap-%2B-count-spaces
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: sender_dict={} max_count = 0 for mess in range(len(messages)): space_count=1 for i in messages[mess]: if i == ' ': space_count+=1 if senders[mess] in sender_dict: sender_dict[senders[mess]] += space_count max_count = max(max_count,sender_dict[senders[mess]]) else: sender_dict[senders[mess]] = space_count max_count = max(max_count,sender_dict[senders[mess]]) ans=[] for sender,words in sender_dict.items(): if words == max_count: ans.append(sender) return sorted(ans)[-1]
sender-with-largest-word-count
Easy python - Hashmap + count spaces
sunakshi132
0
20
sender with largest word count
2,284
0.561
Medium
31,563
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2262796/Python-4-Liner
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d = defaultdict(int) for m, s in zip(messages, senders): d[s] += len(m.split()) return sorted(d.items(), key=lambda t: (t[1], t[0]))[-1][0]
sender-with-largest-word-count
Python 4-Liner
amaargiru
0
28
sender with largest word count
2,284
0.561
Medium
31,564
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2214898/Python-top-90-solution
class Solution: def largestWordCount(self, msg: List[str], snd: List[str]) -> str: ans = {x:0 for x in snd} for i in range(len(msg)): ans[snd[i]] += len(msg[i].split()) sender = '' mx = ans[max(ans, key=ans.get)] for k, v in ans.items(): if v == mx and sender < k: sender = k return sender
sender-with-largest-word-count
Python top 90% solution
StikS32
0
42
sender with largest word count
2,284
0.561
Medium
31,565
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2131538/Python-oror-Straight-Forward
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: msgCount = defaultdict(int) for msg, friend in zip(messages,senders): msg = msg.split() msgCount[friend] += len(msg) def lexi(w1,w2): n, m = len(w1), len(w2) for i in range(max(n,m)): if i >= len(w2): return w1 if i >= len(w1): return w2 l1, l2 = w1[i], w2[i] if ord(l1) > ord(l2): return w1 elif ord(l2) > ord(l1): return w2 max_ = 0 bestFriend = '' val = msgCount.values() friends = msgCount.keys() for count,friend in sorted(zip(val,friends), reverse = True): if count >= max_: max_ = count bestFriend = lexi(bestFriend,friend) else: break return bestFriend
sender-with-largest-word-count
Python || Straight Forward
morpheusdurden
0
36
sender with largest word count
2,284
0.561
Medium
31,566
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2093542/python-3-oror-4-line-hash-map-solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: words = collections.Counter() for message, sender in zip(messages, senders): words[sender] += message.count(' ') + 1 return max(words, key=lambda sender: (words[sender], sender))
sender-with-largest-word-count
python 3 || 4 line hash map solution
dereky4
0
38
sender with largest word count
2,284
0.561
Medium
31,567
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2091924/Python-6-lines-using-dictionary
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: mp = defaultdict(int) mx = 0 for s, m in zip(senders, messages): mp[s] += len(m.split()) mx = max(mx, mp[s]) return max(k for k, v in mp.items() if v == mx)
sender-with-largest-word-count
Python 6 lines using dictionary
SmittyWerbenjagermanjensen
0
32
sender with largest word count
2,284
0.561
Medium
31,568
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2089518/Python3-Straigth-Forward-Solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: word_count_map = {} # { person, word_count } n = len(messages) for i in range(n): if senders[i] in word_count_map: word_count_map[senders[i]] += len(messages[i].split()) else: word_count_map[senders[i]] = len(messages[i].split()) person_with_max_word = next(iter(word_count_map)) max_word_count = word_count_map[person_with_max_word] for person in word_count_map: if word_count_map[person] > max_word_count: max_word_count = word_count_map[person] person_with_max_word = person elif word_count_map[person] == max_word_count and person > person_with_max_word: max_word_count = word_count_map[person] person_with_max_word = person return person_with_max_word
sender-with-largest-word-count
Python3 Straigth Forward Solution
shtanriverdi
0
19
sender with largest word count
2,284
0.561
Medium
31,569
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2088383/python-java-easy-hash
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: ans = dict() for m, s in zip(messages, senders) : words = m.count(' ') + 1 if s in ans : ans[s] += words else : ans[s] = words sender = "" words = 0 for s, w in ans.items() : if w > words : words = w sender = s elif w == words : sender = max(s, sender) return sender
sender-with-largest-word-count
python, java - easy hash
ZX007java
0
13
sender with largest word count
2,284
0.561
Medium
31,570
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2086557/Python-or-Easy-Solution-or-Dictionary
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: wordCount={} n=len(senders) ans=0 res="" for i in range(n): if senders[i] not in wordCount: wordCount[senders[i]]=messages[i].count(' ')+1 else: wordCount[senders[i]]+=messages[i].count(' ')+1 if(wordCount[senders[i]]>ans): ans=wordCount[senders[i]] res=senders[i] elif(wordCount[senders[i]]==ans): res=max(res,senders[i]) return res
sender-with-largest-word-count
Python | Easy Solution | Dictionary
backpropagator
0
16
sender with largest word count
2,284
0.561
Medium
31,571
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2085619/Easy-to-understand-Python-Solution-with-explanation
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: # store count of words of each message in dictionary # key=senders[i] and value=count of words in messages send by sender[i] ans = {} for i in range(len(messages)): count_words = len(messages[i].split()) if senders[i] not in ans: ans.update({senders[i]:count_words}) else: ans[senders[i]] += count_words # find maximum count of words from each sender max_value = max(ans.values()) # In case there are multiple sender with same maximum count of words we need to return lexicographically largest final = [] for i in ans: if ans[i]==max_value: final.append(i) final.sort(reverse=True) return final[0]
sender-with-largest-word-count
Easy to understand Python Solution with explanation
harshkanani014
0
13
sender with largest word count
2,284
0.561
Medium
31,572
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2085083/Decent-Python-solution-using-Dictionary
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: dicti = {} n = len(senders) maxi = 0 arr = [] while n != 0: cnt = messages[n-1].count(' ') + 1 if senders[n-1] not in dicti: dicti[senders[n-1]] = cnt else: dicti[senders[n-1]] += cnt if dicti[senders[n-1]] > maxi: maxi = dicti[senders[n-1]] n-=1 for i in senders: if dicti[i] == maxi: arr.append(i) if len(arr) == 1: return arr[0] else: l = arr[0] for i in range(1,len(arr)): l = max(l,arr[i]) return l
sender-with-largest-word-count
Decent Python solution using Dictionary
onyxspk
0
6
sender with largest word count
2,284
0.561
Medium
31,573
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084480/Python-Simple-Solution-or-4-lines
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: hm = {} for message,sender in zip(messages, senders): hm[sender] = hm.get(sender, 0) + len(message.split(' ')) return sorted(list(hm.items()),key = lambda x: (x[1],x[0]), reverse=True)[0][0]
sender-with-largest-word-count
Python Simple Solution | 4 lines
Nk0311
0
12
sender with largest word count
2,284
0.561
Medium
31,574
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084323/Solution-using-Hashmap-in-python
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: r = [len(i.split(" ")) for i in messages] d = {} for i in range(len(messages)): if senders[i] in d: d[senders[i]] += r[i] else: d[senders[i]] = r[i] res = max(zip(d.values(), d.keys()))[1] return res
sender-with-largest-word-count
Solution using Hashmap in python
srija100
0
10
sender with largest word count
2,284
0.561
Medium
31,575
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084225/Python-3-String-Split-and-HashMap
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: maps = defaultdict(int) for me, s in zip(messages, senders): words = len(me.split(" ")) maps[s] += words ans = "1"*11 m = 0 for sender in senders: if maps[sender] >= m: if maps[sender] > m: m = maps[sender] ans = sender else: if sender > ans: m = maps[sender] ans = sender return ans
sender-with-largest-word-count
[Python 3] - String Split and HashMap
hari19041
0
15
sender with largest word count
2,284
0.561
Medium
31,576
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084055/Python-solution-using-Dictionary-and-sorting
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d = {} # combine messages and senders into a dictionary for item in range(len(messages)): key = senders[item] value = messages[item] # if key already exists, append the value to the already existing value if key in d.keys(): d[key] = d[key] + ' ' + value else: d[key] = value # sort based on word count. In case of ties, sort alphabetically by name sorted_d = sorted(d.items(), key=lambda item:(item[1].count(' ')+1, (item[0])), reverse=True) # return the first name from the sorted list return sorted_d[0][0]
sender-with-largest-word-count
Python solution using Dictionary and sorting
__sushi__
0
15
sender with largest word count
2,284
0.561
Medium
31,577
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2084014/Python-or-Hashmap
class Solution: def largestWordCount(self, mes: List[str], se: List[str]) -> str: d = {} for i in range(len(mes)): l = mes[i].split(" ") if d.get(se[i]): d[se[i]] += len(l) else: d[se[i]] = len(l) t = 0 ans = [] for val in d.keys(): if d[val]>t: t = d[val] ans = [val] elif d[val] == t: ans.append(val) ans.sort() return ans[-1]
sender-with-largest-word-count
Python | Hashmap
Shivamk09
0
16
sender with largest word count
2,284
0.561
Medium
31,578
https://leetcode.com/problems/sender-with-largest-word-count/discuss/2083926/Python3-simple-solution
class Solution: def largestWordCount(self, messages: List[str], senders: List[str]) -> str: d,res,s = {},-1,"" for i in range(len(senders)): sender = senders[i] message_count = len(messages[i].split(" ")) d[sender] = d.get(sender,0) + message_count if d[sender] > res: res = d[sender] s = sender elif d[sender] == res: s = max(sender,s) return s
sender-with-largest-word-count
Python3 simple solution
abhijeetmallick29
0
9
sender with largest word count
2,284
0.561
Medium
31,579
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2083990/Very-simple-Python-solution-O(nlog(n))
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: Arr = [0] * n # i-th city has Arr[i] roads for A,B in roads: Arr[A] += 1 # Each road increase the road count Arr[B] += 1 Arr.sort() # Cities with most road should receive the most score summ = 0 for i in range(len(Arr)): summ += Arr[i] * (i+1) # Multiply city roads with corresponding score return summ
maximum-total-importance-of-roads
Very simple Python solution O(nlog(n))
Eba472
14
452
maximum total importance of roads
2,285
0.608
Medium
31,580
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2086722/Python3-or-Easy-understanding-or-Faster-than-100-time-and-memory
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: '''The main idea is to count the frequency of the cities connected to roads and then keep on assigning the integer value from one to n to each cities after sorting it. ''' f = [0 for _ in range(n)] # for storing the frequency of each city connected to pathways for x, y in roads: f[x] += 1 f[y] += 1 f.sort() s = 0 for i in range(len(f)): s += f[i] * (i+1) # assigning and storing the integer value to each cities frequency in ascending order return s
maximum-total-importance-of-roads
Python3 | Easy understanding | Faster than 100% time and memory
shodan11
2
50
maximum total importance of roads
2,285
0.608
Medium
31,581
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2084168/Python-Easy-Solution-using-Sort
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: degrees = [0] * n for x,y in roads: degrees[x] += 1 degrees[y] += 1 degrees.sort() # Cities with most roads would receive the most score return sum(degrees[i] * (i+1) for i in range(len(degrees)))
maximum-total-importance-of-roads
Python Easy Solution using Sort
constantine786
2
52
maximum total importance of roads
2,285
0.608
Medium
31,582
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2554456/Python-Simple-solution-Optimized
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: in_deg = [0]*n for u, v in roads: in_deg[u] += 1 in_deg[v] += 1 return sum([deg*i for i, deg in enumerate(sorted(in_deg), start=1)])
maximum-total-importance-of-roads
Python Simple solution Optimized
sujittiwari
0
24
maximum total importance of roads
2,285
0.608
Medium
31,583
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2315001/Python3-greedy
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: degree = [0]*n for u, v in roads: degree[u] += 1 degree[v] += 1 return sum(x*y for x, y in zip(range(1, n+1), sorted(degree)))
maximum-total-importance-of-roads
[Python3] greedy
ye15
0
15
maximum total importance of roads
2,285
0.608
Medium
31,584
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2131543/Python-oror-Very-Easy-Solution
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: connexions = defaultdict(int) for i,j in roads: connexions[i] += 1 connexions[j] += 1 val , node = connexions.values(), connexions.keys() assign = defaultdict(int) ans = 0 for v,no in sorted(zip(val,node), reverse = True): ans += v*n n -= 1 return ans
maximum-total-importance-of-roads
Python || Very Easy Solution
morpheusdurden
0
40
maximum total importance of roads
2,285
0.608
Medium
31,585
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2091366/java-python-simple-easy-small-and-fast
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: g = [0]*n for r in roads : g[r[0]] += 1 g[r[1]] += 1 g.sort() sum = 0 for i in range (n): sum += g[i]*(i+1) return sum
maximum-total-importance-of-roads
java, python - simple, easy, small & fast
ZX007java
0
29
maximum total importance of roads
2,285
0.608
Medium
31,586
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2086044/Python-or-6-Lines-or-Sort-%2B-Counter
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: indegrees = Counter() for src, dst in roads: indegrees[src] += 1 indegrees[dst] += 1 indegrees = sorted(indegrees.values(), reverse=True) return sum((n - i) * degree for i, degree in enumerate(indegrees))
maximum-total-importance-of-roads
Python | 6 Lines | Sort + Counter
leeteatsleep
0
16
maximum total importance of roads
2,285
0.608
Medium
31,587
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2084446/Python-Solution-O(n)
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: graph = {i: [] for i in range(n)} for x, y in roads: graph[x].append(y) graph[y].append(x) c = defaultdict(list) for node in graph: c[len(graph[node])].append(node) value = n d = defaultdict(int) for i in reversed(range(n)): if i in c: for node in c[i]: d[node] = value value -= 1 ans = 0 for x, y in roads: ans += d[x] + d[y] return ans
maximum-total-importance-of-roads
Python Solution O(n)
user6397p
0
7
maximum total importance of roads
2,285
0.608
Medium
31,588
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2084383/Simple-Python-Solution
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: degree = [0]*n for i,j in roads: degree[i]+=1 degree[j]+=1 degree.sort() score = 0 for j in range(n): score+=degree[j]*(j+1) return score
maximum-total-importance-of-roads
Simple Python Solution
srija100
0
6
maximum total importance of roads
2,285
0.608
Medium
31,589
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2084039/Python-or-Greedy
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: ind = [0 for i in range(n)] dis = [0 for i in range(n)] for val in roads: ind[val[0]] += 1 ind[val[1]] += 1 l = [] for i in range(n): l.append([i,ind[i]]) l.sort(key=lambda x:x[1]) c = 1 for val in l: dis[val[0]] = c c += 1 ans = 0 for i in range(n): ans += ind[i]*dis[i] return ans
maximum-total-importance-of-roads
Python | Greedy
Shivamk09
0
10
maximum total importance of roads
2,285
0.608
Medium
31,590
https://leetcode.com/problems/maximum-total-importance-of-roads/discuss/2083963/solution-without-graphs-and-other-complex-terms
class Solution: def maximumImportance(self, n: int, roads: List[List[int]]) -> int: dict1={} for i in roads: if i[0] not in dict1:dict1[i[0]]=1 else:dict1[i[0]]+=1 if i[1] not in dict1:dict1[i[1]]=1 else:dict1[i[1]]+=1 sorted_dict = {} sorted_keys = sorted(dict1, key=dict1.get) for w in sorted_keys: sorted_dict[w] = dict1[w] items = list(sorted_dict.items()) sorted_dict = {k: v for k, v in reversed(items)} ans=0 for i in sorted_dict: sorted_dict[i]=n n-=1 print(sorted_dict) for i in roads: hold=sorted_dict[i[0]]+sorted_dict[i[1]] ans+=hold return ans
maximum-total-importance-of-roads
solution without graphs, and other complex terms
alenov
0
5
maximum total importance of roads
2,285
0.608
Medium
31,591
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2085849/Python-Two-Liner-Beats-~95
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: counter_s = Counter(s) return min(counter_s[c] // count for c,count in Counter(target).items())
rearrange-characters-to-make-target-string
Python Two Liner Beats ~95%
constantine786
14
854
rearrange characters to make target string
2,287
0.578
Easy
31,592
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2113253/Two-Counters
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: cnt, cnt1 = Counter(s), Counter(target) return min(cnt[ch] // cnt1[ch] for ch in cnt1.keys())
rearrange-characters-to-make-target-string
Two Counters
votrubac
2
155
rearrange characters to make target string
2,287
0.578
Easy
31,593
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2087665/Python-two-Counters
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: s = Counter(s) target = Counter(target) return min(s[c] // target[c] for c in target)
rearrange-characters-to-make-target-string
Python, two Counters
blue_sky5
2
46
rearrange characters to make target string
2,287
0.578
Easy
31,594
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2085846/Python-or-Stright-Forward
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: counter_s = Counter(s) res = float('inf') for k, v in Counter(target).items(): res = min(res, counter_s[k] // v) return res
rearrange-characters-to-make-target-string
Python | Stright Forward
yzhao156
2
117
rearrange characters to make target string
2,287
0.578
Easy
31,595
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2085788/Simple-Python-solution
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: freqs = {} # count the letter ffrequency of string s for char in s: freqs[char] = freqs.get(char,0) + 1 freqTarget = {} # count the letter ffrequency of target s for c in target: freqTarget[c] = freqTarget.get(c,0) + 1 mini = len(s) #Minimum value to be updated for c in target: mini = min(mini, freqs.get(c,0) // freqTarget[c]) # Don't forget to use freqs.get(c,0). freqs[c] can give you keyError return mini ```
rearrange-characters-to-make-target-string
Simple Python solution
Eba472
2
251
rearrange characters to make target string
2,287
0.578
Easy
31,596
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2098732/Python-3-Counter-HashMap-Easy-Solution-beats-99
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: count1 = Counter(target) count2 = Counter(s) s = inf for char in count1: if char not in count2: return 0 s = min(s, count2[char]//count1[char]) return s
rearrange-characters-to-make-target-string
[Python 3] - Counter HashMap - Easy Solution - beats 99%
hari19041
1
62
rearrange characters to make target string
2,287
0.578
Easy
31,597
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2743330/Python3-or-Brute-Force
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: S = Counter(s) ans = 0 while True: for i in target: if S[i] > 0: S[i] -= 1 else: return ans ans += 1 return 0 #A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.
rearrange-characters-to-make-target-string
Python3 | Brute Force
iftesamnabi
0
9
rearrange characters to make target string
2,287
0.578
Easy
31,598
https://leetcode.com/problems/rearrange-characters-to-make-target-string/discuss/2482587/Python-for-beginners-(1)-Naive-(2)-Optimized-(Counter)
class Solution: def rearrangeCharacters(self, s: str, target: str) -> int: #Naive Approach #Runtime:59ms dic={} for i in s: dic[i]=dic.get(i,0)+1 #print(dic) # The frequency of all the alphabets count=0 while sum(dic.values())>=len(target): # The loop will end when dictionary values become less than the length of the target for j in target: if j in dic: if dic[j]>0: dic[j]-=1 else: #if dic[j]==0 return count return count else: #if j not in dic return count return count count+=1 #After every for loop ends increase count by 1 return count
rearrange-characters-to-make-target-string
Python for beginners (1) Naive (2) Optimized (Counter)
mehtay037
0
75
rearrange characters to make target string
2,287
0.578
Easy
31,599