post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2715901/Python3-The-Way-That-Makes-Sense-To-Me-O(n) | class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
bad, mi, ma, t = -1, -1, -1, 0
for right in range(len(nums)):
if nums[right] == minK:
mi=right
if nums[right] == maxK:
ma=right
if nums[right] >... | count-subarrays-with-fixed-bounds | Python3 The Way That Makes Sense To Me O(n) | godshiva | 0 | 23 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,500 |
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2710881/Python3-sliding-window | class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
ans = 0
ii = imin = imax = -1
for i, x in enumerate(nums):
if minK <= x <= maxK:
if minK == x: imin = i
if maxK == x: imax = i
ans += max(0,... | count-subarrays-with-fixed-bounds | [Python3] sliding window | ye15 | 0 | 21 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,501 |
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2710552/Python3-Easy-Solution | ```class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
def solve(arr):
M=len(arr)
minx=collections.deque()
maxx=collections.deque()
for i in range(M):
if arr[i]==minK:
minx.ap... | count-subarrays-with-fixed-bounds | Python3 Easy Solution | Motaharozzaman1996 | 0 | 19 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,502 |
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2709454/Python3-or-Sliding-Window | class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
dq=deque()
n=len(nums)
mn,mx=-1,-1
ans=0
for i in range(n):
if minK<=nums[i]<=maxK:
dq.append(i)
if nums[i]==minK:
mn=i
... | count-subarrays-with-fixed-bounds | [Python3] | Sliding Window | swapnilsingh421 | 0 | 14 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,503 |
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2708477/Python-Easy-to-understand-O(n)-time-O(1)-space. | class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
n = len(nums)
# store indices of previous minK, maxK, outliers
prev_min, prev_max, prev_out = -1, -1, -1
cnt = 0
for i in range(n):
if nums[i] < minK or nums[i]... | count-subarrays-with-fixed-bounds | [Python] Easy to understand, O(n) time, O(1) space. | YYYami | 0 | 14 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,504 |
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2708411/Python3-O(N)-dp-solution | class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
ans = 0
# kmin: number of subarrays ending at last pos that contains minK
# kmax: number of subarrays ending at last pos that contains maxK
# start: pos of previous invalid number (namely > maxK or < minK)
... | count-subarrays-with-fixed-bounds | [Python3] O(N) dp solution | caijun | 0 | 26 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,505 |
https://leetcode.com/problems/count-subarrays-with-fixed-bounds/discuss/2708344/Sliding-Window-with-Explanation-oror-O(N)-time | class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
i = 0 # left index
j = 0 # right index
ans = 0
minn = [] # store the indices of minK in a window which have no element less than minK and greater than maxK
maxx = [] # store th... | count-subarrays-with-fixed-bounds | Sliding Window with Explanation || O(N) time | Laxman_Singh_Saini | 0 | 42 | count subarrays with fixed bounds | 2,444 | 0.432 | Hard | 33,506 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2744018/Python-Elegant-and-Short | class Solution:
"""
Time: O(1)
Memory: O(1)
"""
def haveConflict(self, a: List[str], b: List[str]) -> bool:
a_start, a_end = a
b_start, b_end = b
return b_start <= a_start <= b_end or \
b_start <= a_end <= b_end or \
a_start <= b_start <= a_en... | determine-if-two-events-have-conflict | Python Elegant & Short | Kyrylo-Ktl | 3 | 79 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,507 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2774277/Easy-Python-Solution | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
e1s=int(event1[0][:2])*60 + int(event1[0][3:])
e1e=int(event1[1][:2])*60 + int(event1[1][3:])
e2s=int(event2[0][:2])*60 + int(event2[0][3:])
e2e=int(event2[1][:2])*60 + int(event2[1][3:])
if... | determine-if-two-events-have-conflict | Easy Python Solution | Vistrit | 2 | 54 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,508 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734341/Python3-1-line | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
return event1[0] <= event2[0] <= event1[1] or event2[0] <= event1[0] <= event2[1] | determine-if-two-events-have-conflict | [Python3] 1-line | ye15 | 2 | 70 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,509 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734202/Python3-Straight-Forward-Clean-and-Concise | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
start1 = int(event1[0][:2]) * 60 + int(event1[0][3:])
end1 = int(event1[1][:2]) * 60 + int(event1[1][3:])
start2 = int(event2[0][:2]) * 60 + int(event2[0][3:])
end2 = int(event2[1][:2]) * 60 + int(e... | determine-if-two-events-have-conflict | [Python3] Straight-Forward, Clean & Concise | xil899 | 1 | 82 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,510 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734106/Simple-and-Easy-2-linesPython | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
arr = [[(int(event1[0][:2])*60)+int(event1[0][3:]) , (int(event1[1][:2])*60)+int(event1[1][3:])] ,
[(int(event2[0][:2])*60)+int(event2[0][3:]) , (int(event2[1][:2])*60)+int(event2[1][3:])]]
... | determine-if-two-events-have-conflict | Simple and Easy 2 lines[Python] | CoderIsCodin | 1 | 39 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,511 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2843230/Python3-Convert-to-minutes | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
def to_minutes(s):
return int(s[:2])*60 + int(s[3:])
e1 = (to_minutes(event1[0]), to_minutes(event1[1]))
e2 = (to_minutes(event2[0]), to_minutes(event2[1]))
return e1[0] in range(e2[... | determine-if-two-events-have-conflict | Python3 Convert to minutes | bettend | 0 | 3 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,512 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2817649/My-solution-with-35ms | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
start1 = int(event1[0].replace(':',''))
end1 = int(event1[1].replace(':',''))
start2 = int(event2[0].replace(':',''))
end2 = int(event2[1].replace(':',''))
if end1 < start2 or end2 < start1:... | determine-if-two-events-have-conflict | My solution with 35ms | letrungkienhd1308 | 0 | 6 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,513 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2780012/One-line-Python-Code | class Solution:
def haveConflict(self, e1: List[str], e2: List[str]) -> bool:
return e1[0] <= e2[0] <= e1[1] or e2[0] <= e1[0] <= e2[1] | determine-if-two-events-have-conflict | One line Python Code | dnvavinash | 0 | 10 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,514 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2751301/Convert-to-minutes | class Solution:
def haveConflict(self, e1: List[str], e2: List[str]) -> bool:
return ((int(e1[0][:2]) * 60 + int(e1[0][3:])) <=
(int(e2[1][:2]) * 60 + int(e2[1][3:])) and
(int(e2[0][:2]) * 60 + int(e2[0][3:])) <=
(int(e1[1][:2]) * 60 + int(e1[1][3:]))) | determine-if-two-events-have-conflict | Convert to minutes | EvgenySH | 0 | 10 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,515 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2749816/Python-easy-solution | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
st1=int(''.join(list(event1[0].split(":"))))
et1=int(''.join(list(event1[1].split(":"))))
st2=int(''.join(list(event2[0].split(":"))))
et2=int(''.join(list(event2[1].split(":"))))
if st1>=st... | determine-if-two-events-have-conflict | Python easy solution | AviSrivastava | 0 | 12 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,516 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2735859/Python-one-liner | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
return not (event1[1] < event2[0] or event2[1] < event1[0]) | determine-if-two-events-have-conflict | Python, one liner | blue_sky5 | 0 | 14 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,517 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2735209/Python-3-or-One-Liner-or-Easy | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
return (event1[0]<=event2[0] and event1[1]>=event2[0]) or (event1[0]>=event2[0] and event2[1]>=event1[0]) | determine-if-two-events-have-conflict | Python 3 | One Liner | Easy | RickSanchez101 | 0 | 11 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,518 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734685/Python3 | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
hour1a, minute1a = map(int, event1[0].split(':'))
hour1b, minute1b = map(int, event1[1].split(':'))
hour2a, minute2a = map(int, event2[0].split(':'))
hour2b, minute2b = map(int, event2[1].split(':')... | determine-if-two-events-have-conflict | Python3 | mediocre-coder | 0 | 6 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,519 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734558/Python3-cast-time-to-minutes | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
def helper(time):
hour, minute = int(time[:2]), int(time[3:])
return hour*60 + minute
event1 = [helper(x) for x in event1]
event2 = [helper(x) for x in event2]
... | determine-if-two-events-have-conflict | Python3 cast time to minutes | xxHRxx | 0 | 10 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,520 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734527/Python-O(1)TC-solution | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
if event2[1]>=event1[0]:
if event2[0]<=event1[1]:
return True
return False | determine-if-two-events-have-conflict | Python O(1)TC solution | sundram_somnath | 0 | 4 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,521 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734362/Easy-Python3-code-with-explanation | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
a=int(event1[0][0]+event1[0][1]+event1[0][3]+event1[0][4])
b=int(event1[1][0]+event1[1][1]+event1[1][3]+event1[1][4])
c=int(event2[0][0]+event2[0][1]+event2[0][3]+event2[0][4])
d=int(event2... | determine-if-two-events-have-conflict | Easy Python3 code with explanation | shreyasjain0912 | 0 | 8 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,522 |
https://leetcode.com/problems/determine-if-two-events-have-conflict/discuss/2734298/EASY-PYTHON-SOLUTION-USING-IF-LOOP | class Solution:
def haveConflict(self, event1: List[str], event2: List[str]) -> bool:
if event1[1][0:2]==event2[0][0:2]:
if event1[1][3:5]>=event2[0][3:5]:
return True
else:
return False
else:
if event1[1][0:2]<event2[0][0:2] or eve... | determine-if-two-events-have-conflict | EASY PYTHON SOLUTION USING IF LOOP | sowmika_chaluvadi | 0 | 11 | determine if two events have conflict | 2,446 | 0.494 | Easy | 33,523 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734224/Python3-Brute-Force-%2B-Early-Stopping-Clean-and-Concise | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
n = len(nums)
ans = 0
for i in range(n):
temp = nums[i]
for j in range(i, n):
temp = math.gcd(temp, nums[j])
if temp == k:
ans += 1
... | number-of-subarrays-with-gcd-equal-to-k | [Python3] Brute Force + Early Stopping, Clean & Concise | xil899 | 4 | 423 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,524 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2835238/Python-(Simple-Maths) | class Solution:
def subarrayGCD(self, nums, k):
n, count = len(nums), 0
for i in range(n):
ans = nums[i]
for j in range(i,n):
ans = math.gcd(ans,nums[j])
if ans == k:
count += 1
elif ans < k:
... | number-of-subarrays-with-gcd-equal-to-k | Python (Simple Maths) | rnotappl | 0 | 1 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,525 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2814449/2447.-Number-of-Subarrays-With-GCD-Equal-to-K-oror-Python3-oror-GCD-Recursive | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
c=0
for i in range(len(nums)):
a=nums[i]
for j in range(i,len(nums)):
a=gcd(a,nums[j])
if a==k:
c+=1
return c
def gcd(a,b):
if b==0:
... | number-of-subarrays-with-gcd-equal-to-k | 2447. Number of Subarrays With GCD Equal to K || Python3 || GCD Recursive | shagun_pandey | 0 | 6 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,526 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2737230/Python3-or-Easy-Solution | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
n = len(nums)
i = 0;j = 0;ans = 0
while(i<n):
#print(gcd)
gcd = nums[i]
for j in range(i,n):
gcd = GCD(nums[j],gcd)
if(gcd==k):
... | number-of-subarrays-with-gcd-equal-to-k | Python3 | Easy Solution | ty2134029 | 0 | 26 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,527 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734815/number-of-subarrays-with-gcd-equal-to-k | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
c=0
for i in range(len(nums)):
temp=nums[i]
if temp==k:
c=c+1
for j in range(i+1,len(nums)):
temp=math.gcd(temp,nums[j])
if temp==k:
... | number-of-subarrays-with-gcd-equal-to-k | number-of-subarrays-with-gcd-equal-to-k | meenu155 | 0 | 14 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,528 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734764/Understandable-Python-Solution | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
count = 0
n=len(nums)
for i in range(n):
curr = 0
for j in range(i,n):
curr = math.gcd(curr,nums[j]) # You can use math.gcd or gcd. Both will work.
if(curr == k):
... | number-of-subarrays-with-gcd-equal-to-k | Understandable Python Solution | Ayush_Kumar27 | 0 | 15 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,529 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734690/Python3-Brute-Force-with-a-little-optimization | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
def gcd(x, y):
while y:
x, y = y, x % y
return x
candidates = [_ % k for _ in nums]
factor = [_ //k for _ in nums]
segments = []
start = 0... | number-of-subarrays-with-gcd-equal-to-k | Python3 Brute Force with a little optimization | xxHRxx | 0 | 3 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,530 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734418/simple-python-solution | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
def find_gcd(x, y):
while(y):
x, y = y, x % y
return x
res=0
for i in range(len(nums)):
gcd=nums[i]
for j in range(i,len(nums)):
... | number-of-subarrays-with-gcd-equal-to-k | simple python solution | sundram_somnath | 0 | 8 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,531 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734380/Python3-brute-force | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
ans = 0
for i in range(len(nums)):
g = 0
for j in range(i, len(nums)):
g = gcd(g, nums[j])
if g == k: ans += 1
return ans | number-of-subarrays-with-gcd-equal-to-k | [Python3] brute-force | ye15 | 0 | 7 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,532 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734194/Easy-Python3-Solution | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
def gcd(a,b):
while b:
a,b=b,a%b
return a
ans=0
for i in range(0,len(nums)):
g=0
for j in range(i,len(nums)):
g=gcd(g,nums[j])
... | number-of-subarrays-with-gcd-equal-to-k | Easy Python3 Solution | shreyasjain0912 | 0 | 31 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,533 |
https://leetcode.com/problems/number-of-subarrays-with-gcd-equal-to-k/discuss/2734144/Python-or-brute-force | class Solution:
def subarrayGCD(self, nums: List[int], k: int) -> int:
def gcd(n1, n2):
if n2==0:
return n1
return gcd(n2, n1%n2)
ans = 0
n = len(nums)
for i in range(n):
curr_gcd = 0
for j in range(i, n):
... | number-of-subarrays-with-gcd-equal-to-k | Python | brute force | diwakar_4 | 0 | 13 | number of subarrays with gcd equal to k | 2,447 | 0.482 | Medium | 33,534 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734183/Python3-Weighted-Median-O(NlogN)-with-Explanations | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
arr = sorted(zip(nums, cost))
total, cnt = sum(cost), 0
for num, c in arr:
cnt += c
if cnt > total // 2:
target = num
break
return sum(c * abs(num - tar... | minimum-cost-to-make-array-equal | [Python3] Weighted Median O(NlogN) with Explanations | xil899 | 109 | 2,300 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,535 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734625/DP-Solution.-Intuitive-and-Clear-or-Python | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
n = len(nums)
# Sort by nums
arr = sorted((nums[i], cost[i]) for i in range(n))
nums = [i[0] for i in arr]
cost = [i[1] for i in arr]
# Compute DP left to right
left2right = [0] * n
... | minimum-cost-to-make-array-equal | DP Solution. Intuitive and Clear | Python | alex391a | 29 | 893 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,536 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734089/Python-C%2B%2B-or-Binary-search-or-Clean-solution | class Solution:
def calculateSum(self, nums, cost, target):
res = 0
for n, c in zip(nums, cost):
res += abs(n - target) * c
return res
def minCost(self, nums: List[int], cost: List[int]) -> int:
s, e = min(nums), max(nums)
while s < e:
... | minimum-cost-to-make-array-equal | Python, C++ | Binary search | Clean solution | alexnik42 | 10 | 518 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,537 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734499/Python3-Ternary-Search-or-O(NlogN) | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
def cnt(t): #function for calculating cost for Target "t"
cur = cur_prev = cur_nxt = 0 #initialize the cost counting
for i, el in enumerate(nums):
cur += abs(el-t) * cost[i] #update the cost f... | minimum-cost-to-make-array-equal | [Python3] Ternary Search | O(NlogN) | Parag_AP | 2 | 30 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,538 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734506/Python3-Sorting-%2B-Prefix_sum-and-postfix_sum-for-cost-O(nlogn)-Solution | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
n = len(nums)
pairs = sorted(zip(nums, cost))
if pairs[0][0] == pairs[-1][0]:
return 0
post_sum = [0] * (n + 1)
res = 0
for i in range(n - 1, -1, -1):
post_sum... | minimum-cost-to-make-array-equal | [Python3] Sorting + Prefix_sum and postfix_sum for cost, O(nlogn) Solution | celestez | 1 | 43 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,539 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734299/Python-3oror-Time%3A-3204-ms-Space%3A-42.6-MB | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
n=len(nums)
pre=[0 for i in range(n)]
suff=[0 for i in range(n)]
z=list(zip(nums,cost))
z.sort()
t=z[0][1]
for i in range(1,n):
pre[i]=pre[i-1]+(t*abs(z[i][0]-z[i-1][0]))
... | minimum-cost-to-make-array-equal | Python 3|| Time: 3204 ms , Space: 42.6 MB | koder_786 | 1 | 21 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,540 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2786257/python-two-pointer | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
d = defaultdict(int)
for i,num in enumerate(nums):
d[num]+=cost[i]
arr = [[key,val] for key,val in d.items()]
arr.sort()
res, i, j = 0, 0, len(arr)-1
while i<j:
if arr[... | minimum-cost-to-make-array-equal | python two pointer | Jeremy0923 | 0 | 6 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,541 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2741476/python3-Prefix-calculation-increment-and-decrement-sol-for-reference. | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
N = len(nums)
nc = sorted(zip(nums, cost))
inc = [0]*N
dec = [0]*N
rolling = nc[0][1]
for i in range(1,N):
inc[i] = ((nc[i][0]-nc[i-1][0])*rolling) + inc[i-1]
rolling ... | minimum-cost-to-make-array-equal | [python3] Prefix calculation increment and decrement sol for reference. | vadhri_venkat | 0 | 8 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,542 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2740399/sorting-with-explanation | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
# sort nums and cost by nums
cost = [x for _, x in sorted(zip(nums, cost))]
nums = sorted(nums)
# suppose x is the average:
# while nums[0]<=x<=nums[1]:
# min cost = x*cost[0] - nums[0]*cost[0... | minimum-cost-to-make-array-equal | sorting with explanation | nuya | 0 | 6 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,543 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2737125/Python-3-or-prefix-sum-or-O(nlogn)O(n) | class Solution:
def minCost(self, nums: List[int], costs: List[int]) -> int:
sortedNums = sorted(zip(nums, costs))
costsPrefix, costsSuffix = 0, sum(costs)
numsCost = res = sum(num * cost for num, cost in sortedNums)
for num, cost in sortedNums:
numsCost -= 2 * n... | minimum-cost-to-make-array-equal | Python 3 | prefix sum | O(nlogn)/O(n) | dereky4 | 0 | 22 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,544 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2736676/PYTHON-or-MEDIAN-or-O(NlogN)-SOLUTION-or-CLEAN-CODE | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
n=sum(cost)
arr=[]
for i in range(len(nums)):
arr.append((nums[i],cost[i]))
arr=sorted(arr)
index=n//2
# print(index)
curr=0
x=nums[0]
for i in range(len(ar... | minimum-cost-to-make-array-equal | PYTHON | MEDIAN | O(NlogN) SOLUTION | CLEAN CODE | VISHNU_Mali | 0 | 12 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,545 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2736111/Python3-prefix-and-suffix-sum-solution | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
data = [(x,y) for x, y in zip(nums, cost)]
data.sort(key = lambda x : x[0])
prefix = []
suffix = []
start = 0
for _, cost in data:
start += cost
... | minimum-cost-to-make-array-equal | Python3 prefix and suffix sum solution | xxHRxx | 0 | 7 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,546 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2735353/Python-oror-fastest-oror-easy-to-understand-using-prefixSum-and-sorting | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
def find_cost(target):
totalCost = 0
for i in range(len(nums)):
totalCost += abs(nums[i] - target) * cost[i]
return totalCost
pairs = sorted(zip(nums, cost))
... | minimum-cost-to-make-array-equal | Python || fastest || easy to understand using prefixSum and sorting | Yared_betsega | 0 | 21 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,547 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734903/Python-Brute-Force-If-you-didn't-realize-this-is-convex-problem-we-can-still-do-it! | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
import collections
start, end = min(nums), max(nums)
pluscost = 0
currentcost = 0
d = collections.defaultdict(list)
for idx, i in enumerate(nums):
d[i].append(idx)
curr... | minimum-cost-to-make-array-equal | [Python] Brute Force - If you didn't realize this is convex problem we can still do it! | A_Pinterest_Employee | 0 | 13 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,548 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734817/Python3-or-Prefix-and-Suffix-Sum | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
n=len(nums)
arr=sorted(list(zip(nums,cost)))
prefix=[0]*n
suffix=[0]*n
prefix[0]=arr[0][1]
suffix[n-1]=arr[n-1][1]
currCost,ans=0,float('inf')
for i in range(1,n):
... | minimum-cost-to-make-array-equal | [Python3] | Prefix and Suffix Sum | swapnilsingh421 | 0 | 19 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,549 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734383/Python3-Binary-Search | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
def get_cost(t):
return sum([abs(num-t)*c for (num, c) in zip(nums, cost)])
l, r = min(nums), max(nums)
while l < r:
mid = (l+r)//2
if get_cost(mid) <= get_cost(mid+1): r = mid
... | minimum-cost-to-make-array-equal | [Python3] Binary Search | teyuanliu | 0 | 11 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,550 |
https://leetcode.com/problems/minimum-cost-to-make-array-equal/discuss/2734356/Python3-Median | class Solution:
def minCost(self, nums: List[int], cost: List[int]) -> int:
nums, cost = zip(*sorted(zip(nums, cost)))
total = sum(cost)
prefix = 0
for i, x in enumerate(cost):
prefix += x
if prefix > total//2: break
return sum(c*abs(x-nums[i]) for ... | minimum-cost-to-make-array-equal | [Python3] Median | ye15 | 0 | 19 | minimum cost to make array equal | 2,448 | 0.346 | Hard | 33,551 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2734139/Python-or-Simple-OddEven-Sorting-O(nlogn)-or-Walmart-OA-India-or-Simple-Explanation | class Solution:
def makeSimilar(self, A: List[int], B: List[int]) -> int:
if sum(A)!=sum(B): return 0
# The first intuition is that only odd numbers can be chaged to odd numbers and even to even hence separate them
# Now minimum steps to making the target to highest number in B is by convert... | minimum-number-of-operations-to-make-arrays-similar | Python | Simple Odd/Even Sorting O(nlogn) | Walmart OA India | Simple Explanation | tarushfx | 3 | 245 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,552 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2737259/Python3-One-Liner-Sort | class Solution:
def makeSimilar(self, nums: List[int], target: List[int]) -> int:
return sum(map(lambda p:abs(p[1]-p[0]), zip(list(sorted([i + 10**9 * (i%2) for i in nums])), list(sorted([j + 10**9 * (j%2) for j in target])))))>>2 | minimum-number-of-operations-to-make-arrays-similar | Python3 One Liner - Sort | godshiva | 0 | 9 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,553 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2736557/Easy-solution-using-odd-even-and-sorting | class Solution:
def makeSimilar(self, nums: List[int], target: List[int]) -> int:
nums.sort()
target.sort()
res=0
numseven=[]
numsodd=[]
targeteven=[]
targetodd=[]
for i in nums:
if i%2==0:
numseven.append(i)
els... | minimum-number-of-operations-to-make-arrays-similar | Easy solution using odd, even and sorting | sundram_somnath | 0 | 14 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,554 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2735773/Python-oror-heap-oror-evenodd-separately | class Solution:
def makeSimilar(self, nums: List[int], target: List[int]) -> int:
evenNums = []
evenTargets = []
oddNums = []
oddTargets = []
for num, tar in zip(nums, target):
evenNums.append(num) if not num & 1 else oddNums.append(num)
evenTarget... | minimum-number-of-operations-to-make-arrays-similar | Python || heap || even/odd separately | Yared_betsega | 0 | 11 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,555 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2734657/JavaPython3-or-Separating-even-and-odd-or-Sorting | class Solution:
def makeSimilar(self, nums: List[int], target: List[int]) -> int:
nums.sort()
target.sort()
evenNums,oddNums=[],[]
evenTarget,oddTarget=[],[]
n=len(nums)
for i in range(n):
if nums[i]%2==0:
evenNums.append(nums[i])
... | minimum-number-of-operations-to-make-arrays-similar | [Java/Python3] | Separating even and odd | Sorting | swapnilsingh421 | 0 | 16 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,556 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2734440/Python3-Sorting | class Solution:
def makeSimilar(self, nums: List[int], target: List[int]) -> int:
nums.sort()
target.sort()
nums_even_odd = [[num for num in nums if num%2 == 0], [num for num in nums if num%2 == 1]]
target_even_odd = [[num for num in target if num%2 == 0], [num for num in target if n... | minimum-number-of-operations-to-make-arrays-similar | [Python3] Sorting | teyuanliu | 0 | 15 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,557 |
https://leetcode.com/problems/minimum-number-of-operations-to-make-arrays-similar/discuss/2734371/Python3-parity | class Solution:
def makeSimilar(self, nums: List[int], target: List[int]) -> int:
ne = sorted(x for x in nums if not x&1)
no = sorted(x for x in nums if x&1)
te = sorted(x for x in target if not x&1)
to = sorted(x for x in target if x&1)
return (sum(abs(x-y) f... | minimum-number-of-operations-to-make-arrays-similar | [Python3] parity | ye15 | 0 | 12 | minimum number of operations to make arrays similar | 2,449 | 0.649 | Hard | 33,558 |
https://leetcode.com/problems/odd-string-difference/discuss/2774188/Easy-to-understand-python-solution | class Solution:
def oddString(self, words: List[str]) -> str:
k=len(words[0])
arr=[]
for i in words:
l=[]
for j in range(1,k):
diff=ord(i[j])-ord(i[j-1])
l.append(diff)
arr.append(l)
for i in range(len(arr)):
... | odd-string-difference | Easy to understand python solution | Vistrit | 2 | 87 | odd string difference | 2,451 | 0.595 | Easy | 33,559 |
https://leetcode.com/problems/odd-string-difference/discuss/2844752/Python3-Builtin-Party | class Solution:
def oddString(self, words: List[str]) -> str:
# go through all of the words find and track all the diffs
cn = collections.defaultdict(list)
for idx, word in enumerate(words):
# get the difference array
diff_arr = Solution.differen... | odd-string-difference | [Python3] - Builtin-Party | Lucew | 0 | 2 | odd string difference | 2,451 | 0.595 | Easy | 33,560 |
https://leetcode.com/problems/odd-string-difference/discuss/2826031/Python-Simple-Solution-With-No-Hashmaps-and-EarlyStopping-(O(1)-Memory-and-O(N2)-Average-Runtime | class Solution:
def oddString(self, words: List[str]) -> str:
def getDiffArray(word):
return [ord(word[i+1]) - ord(word[i]) for i in range(len(word) - 1)]
diff1 = getDiffArray(words[0])
diff2 = getDiffArray(words[1])
for word in words[2:]:
curDiff = g... | odd-string-difference | [Python] Simple Solution With No Hashmaps and EarlyStopping (O(1) Memory and O(N/2) Average Runtime | dumbitdownJr | 0 | 3 | odd string difference | 2,451 | 0.595 | Easy | 33,561 |
https://leetcode.com/problems/odd-string-difference/discuss/2809208/Python3-beats-97.13-using-defaultdict | class Solution:
def oddString(self, words):
d = defaultdict(list)
for w in words:
f = [ord(w[i+1])-ord(w[i]) for i in range(len(w)-1)]
d[tuple(f)].append(w)
for x in d:
if len(d[x])==1:
return d[x][0] | odd-string-difference | [Python3] beats 97.13% using defaultdict | U753L | 0 | 9 | odd string difference | 2,451 | 0.595 | Easy | 33,562 |
https://leetcode.com/problems/odd-string-difference/discuss/2797704/Python3-34ms-13.9MB-Easy-to-read-solution | class Solution:
def oddString(self, words: List[str]) -> str:
# input: list of words with equal len
# use ord(char) - 97 to get num for each char
# then, we calculate each word element first through for loop
# then we compare between each list element
final_list = []
... | odd-string-difference | [Python3] 34ms / 13.9MB - Easy-to-read solution | phucnguyen290198 | 0 | 8 | odd string difference | 2,451 | 0.595 | Easy | 33,563 |
https://leetcode.com/problems/odd-string-difference/discuss/2785999/Python-(Simple-Maths) | class Solution:
def dfs(self,word):
res = []
for i in range(1,len(word)):
res.append(ord(word[i]) - ord(word[i-1]))
return res
def oddString(self, words):
dict1 = defaultdict(list)
for i in words:
dict1[tuple(self.dfs(i))].append(i)
fo... | odd-string-difference | Python (Simple Maths) | rnotappl | 0 | 9 | odd string difference | 2,451 | 0.595 | Easy | 33,564 |
https://leetcode.com/problems/odd-string-difference/discuss/2764216/odd-string-difference | class Solution:
def oddString(self, words: List[str]) -> str:
#print(ord('z')-96)
#words = ["adc","wzy","abc"]
mp=dict()
for i in range(len(words)):
diff=[]
for j in range(len(words[i])-1):
diff.append((ord(words[i][j+1])-96)-(ord(words[i][j])... | odd-string-difference | odd string difference | shivansh2001sri | 0 | 11 | odd string difference | 2,451 | 0.595 | Easy | 33,565 |
https://leetcode.com/problems/odd-string-difference/discuss/2762929/Python-1-Brute-Force-Without-Hashmap-2Optimized-With-Hashmap | class Solution:
def oddString(self, words: List[str]) -> str:
# Brute Force
difference=[]
for word in words:
temp=[]
for j in range(len(word)-1):
temp.append(ord(word[j+1])-ord(word[j]))
difference.append(temp)
#print(difference)
... | odd-string-difference | Python [1] Brute Force {Without Hashmap} [2]Optimized {With Hashmap} | mehtay037 | 0 | 18 | odd string difference | 2,451 | 0.595 | Easy | 33,566 |
https://leetcode.com/problems/odd-string-difference/discuss/2762929/Python-1-Brute-Force-Without-Hashmap-2Optimized-With-Hashmap | class Solution:
def oddString(self, words: List[str]) -> str:
hashmap=defaultdict(list)
for word in words:
difference=[]
for i in range(len(word)-1):
difference.append(ord(word[i+1])-ord(word[i]))
hashmap[tuple(difference)].append(word)
... | odd-string-difference | Python [1] Brute Force {Without Hashmap} [2]Optimized {With Hashmap} | mehtay037 | 0 | 18 | odd string difference | 2,451 | 0.595 | Easy | 33,567 |
https://leetcode.com/problems/odd-string-difference/discuss/2762348/Python3-Convert-To-Number | class Solution:
def oddString(self, words: List[str]) -> str:
def v(s):
x = 0
for i in range(0, len(s)-1):
x+= (ord(s[i]) - ord(s[i+1]) + 26) * (54**i)
return x
x = [v(s) for s in words]
c = Counter(x)
for i in range(len(x)):
... | odd-string-difference | Python3 Convert To Number | godshiva | 0 | 12 | odd string difference | 2,451 | 0.595 | Easy | 33,568 |
https://leetcode.com/problems/odd-string-difference/discuss/2759810/Early-loop-termination-using-dict-100-speed | class Solution:
def oddString(self, words: List[str]) -> str:
dict_diff = dict()
for w in words:
diff = tuple(ord(a) - ord(b) for a, b in zip(w, w[1:]))
if diff in dict_diff:
dict_diff[diff][0] += 1
else:
dict_diff[diff] = [1, w]
... | odd-string-difference | Early loop termination using dict, 100% speed | EvgenySH | 0 | 12 | odd string difference | 2,451 | 0.595 | Easy | 33,569 |
https://leetcode.com/problems/odd-string-difference/discuss/2759296/Python-Simple-Python-Solution-Using-HashMap-or-Dictionary | class Solution:
def oddString(self, words: List[str]) -> str:
alpha = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
result = []
for... | odd-string-difference | [ Python ] ✅✅ Simple Python Solution Using HashMap | Dictionary🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 20 | odd string difference | 2,451 | 0.595 | Easy | 33,570 |
https://leetcode.com/problems/odd-string-difference/discuss/2758863/Python3-30ms-Compare-in-Pairs-O(k*n)-Time-O(1)-Space | class Solution:
def oddString(self, words: List[str]) -> str:
k = len(words)
n = len(words[0])
for i in range(0, k, 2):
for j in range(n-1):
a = ord(words[i%k][j+1]) - ord(words[i%k][j])
b = ord(words[(i+1)%k][j+1]) - ord(words[(i... | odd-string-difference | [Python3] 30ms Compare in Pairs - O(k*n) Time, O(1) Space | rt500 | 0 | 19 | odd string difference | 2,451 | 0.595 | Easy | 33,571 |
https://leetcode.com/problems/odd-string-difference/discuss/2757841/Python3-Hashmap-Solution | class Solution:
def oddString(self, words: List[str]) -> str:
d = {}
for word in words:
arr = []
for i in range(len(word)-1):
arr.append(ord(word[i + 1]) - ord(word[i]))
t = tuple(arr)
if t not in d:
d[t] = [... | odd-string-difference | Python3 Hashmap Solution | theReal007 | 0 | 8 | odd string difference | 2,451 | 0.595 | Easy | 33,572 |
https://leetcode.com/problems/odd-string-difference/discuss/2757341/Python3-Simple-Solution | class Solution:
def oddString(self, words: List[str]) -> str:
n = len(words[0])
difference = defaultdict(list)
for word in words:
temp = "["
for j in range(0, n-1):
temp += str(ord(word[j+1]) - ord(word[j])) + ","
temp = temp[:-1] ... | odd-string-difference | Python3 Simple Solution | mediocre-coder | 0 | 8 | odd string difference | 2,451 | 0.595 | Easy | 33,573 |
https://leetcode.com/problems/odd-string-difference/discuss/2757199/Python-beats-100-easy-explained-with-comments | class Solution:
def oddString(self, words: List[str]) -> str:
def calculateDifference(word: str) -> tuple:
# Calculate Difference between i and i + 1 of the word
return tuple(ord(word[i + 1]) - ord(word[i]) for i in range(len(word) - 1))
differenceDict = collections.defaultd... | odd-string-difference | ✅ Python beats 100%, easy explained with comments | sezanhaque | 0 | 12 | odd string difference | 2,451 | 0.595 | Easy | 33,574 |
https://leetcode.com/problems/odd-string-difference/discuss/2757127/Python3-freq-table | class Solution:
def oddString(self, words: List[str]) -> str:
mp = defaultdict(list)
for word in words:
diff = tuple(ord(word[i]) - ord(word[i-1]) for i in range(1, len(word)))
mp[diff].append(word)
return next(v[0] for v in mp.values() if len(v) == 1) | odd-string-difference | [Python3] freq table | ye15 | 0 | 8 | odd string difference | 2,451 | 0.595 | Easy | 33,575 |
https://leetcode.com/problems/odd-string-difference/discuss/2756851/Python | class Solution:
def oddString(self, words: List[str]) -> str:
diffs = list(map(lambda w: [ord(c1) - ord(c2) for c1, c2 in zip(w[1:], w)], words))
if diffs[0] != diffs[1] and diffs[0] != diffs[2]:
return words[0]
for i in range(1, len(words)):
if diff... | odd-string-difference | Python | blue_sky5 | 0 | 9 | odd string difference | 2,451 | 0.595 | Easy | 33,576 |
https://leetcode.com/problems/odd-string-difference/discuss/2756727/Easy-Python-Solution | class Solution:
def oddString(self, words: List[str]) -> str:
d = defaultdict(list)
for i in words:
l = []
for j in range(1,len(i)):
x = ord(i[j]) - ord(i[j - 1])
l.append(x)
d[tuple(l)].append(i)
for k,v in d.items():
... | odd-string-difference | Easy Python Solution | a_dityamishra | 0 | 8 | odd string difference | 2,451 | 0.595 | Easy | 33,577 |
https://leetcode.com/problems/odd-string-difference/discuss/2756692/Python3-O(n*length)-Solution | class Solution:
def oddString(self, words: List[str]) -> str:
size_t = len(words[0])
for t in range(size_t-1):
dict1 = defaultdict(list)
for i in range(len(words)):
temp = ord(words[i][t]) - ord(words[i][t-1])
dict1[temp].append(i)
... | odd-string-difference | Python3 O(n*length) Solution | xxHRxx | 0 | 9 | odd string difference | 2,451 | 0.595 | Easy | 33,578 |
https://leetcode.com/problems/odd-string-difference/discuss/2756675/Python3-Dictionary | class Solution:
def oddString(self, words: List[str]) -> str:
def wDif(w):
return tuple(ord(a)-ord(b) for a,b in zip(w,w[1:]))
d={}
q=[(wDif(w),w) for w in words]
for v,w in q:
if v not in d:
d[v]=[]
d[v].append(w)
... | odd-string-difference | Python3, Dictionary | Silvia42 | 0 | 5 | odd string difference | 2,451 | 0.595 | Easy | 33,579 |
https://leetcode.com/problems/odd-string-difference/discuss/2756650/Two-dicts | class Solution:
def oddString(self, words: List[str]) -> str:
def ret_int(s):
ans = []
for i in range(1, len(s)):
ans.append(ord(s[i]) - ord(s[i - 1]))
return ' '.join(map(str, ans))
ans = {}
count = {}
for w in words:
... | odd-string-difference | Two dicts | evgen8323 | 0 | 6 | odd string difference | 2,451 | 0.595 | Easy | 33,580 |
https://leetcode.com/problems/odd-string-difference/discuss/2756536/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[... | odd-string-difference | [Python3] | Brute Force | swapnilsingh421 | 0 | 12 | odd string difference | 2,451 | 0.595 | Easy | 33,581 |
https://leetcode.com/problems/odd-string-difference/discuss/2756394/Odd-String-Difference-or-PYTHON | class Solution:
def oddString(self, words: List[str]) -> str:
a="abcdefghijklmnopqrstuvwxyz"
l=[]
for i in range(0,len(words)):
w=words[i]
ans=[]
for j in range(1,len(w)):
s=a.index(w[j])-a.index(w[j-1])
ans.append(s)
... | odd-string-difference | Odd String Difference | PYTHON | saptarishimondal | 0 | 13 | odd string difference | 2,451 | 0.595 | Easy | 33,582 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757378/Python-Simple-HashSet-solution-O((N%2BM)-*-L3) | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
# T: ((N + M) * L^3), S: O(M * L^3)
N, M, L = len(queries), len(dictionary), len(queries[0])
validWords = set()
for word in dictionary:
for w in self.wordModifications... | words-within-two-edits-of-dictionary | [Python] Simple HashSet solution O((N+M) * L^3) | rcomesan | 2 | 45 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,583 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2797299/Python-(Simple-Maths) | class Solution:
def twoEditWords(self, queries, dictionary):
n, ans = len(queries[0]), []
for i in queries:
for j in dictionary:
if sum(i[k] != j[k] for k in range(n)) < 3:
ans.append(i)
break
return ans | words-within-two-edits-of-dictionary | Python (Simple Maths) | rnotappl | 0 | 6 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,584 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2779217/Python-Brute-Force-88-speed | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
def distance_le_2(a, b):
d = 0
for i, j in zip(a, b):
if i != j:
d += 1
if d > 2:
return False
... | words-within-two-edits-of-dictionary | Python Brute Force 88% speed | very_drole | 0 | 9 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,585 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2767642/Python-Easy-and-Intuitive-Solution. | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
def difference(word1, word2):
count = 0
for a, b in zip(word1, word2):
if a != b:
count += 1
return count... | words-within-two-edits-of-dictionary | Python Easy & Intuitive Solution. | MaverickEyedea | 0 | 9 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,586 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2765093/Python3-Brute-Force-Approach | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
# input: queries, dict
# both have words with same length
# e.g.: ["word", "note", "ants", "wood"]
# ["wood", "joke", "moat"]
# word -> wood: edit r -> o
# note -... | words-within-two-edits-of-dictionary | [Python3] Brute Force Approach | phucnguyen290198 | 0 | 4 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,587 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2762219/One-line-with-list-comprehension-100-speed | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
return [w for w in queries if
any(sum(cw != cd for cw, cd in zip(w, d)) < 3
for d in dictionary)] | words-within-two-edits-of-dictionary | One line with list comprehension, 100% speed | EvgenySH | 0 | 7 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,588 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2761965/Python-Easy-To-Understand-Pythonic-Solution | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
res = []
for query in queries:
for word in dictionary:
# Idea is to get the count of difference of chars at the positions
w = len([c for c in zip(query, word) if c[0] != ... | words-within-two-edits-of-dictionary | Python Easy To Understand Pythonic Solution | Advyat | 0 | 4 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,589 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2760648/Python-2-lines | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
# return True if only 2 differences between s1 and s2
f = lambda s1, s2: sum(a != b for a, b in zip(s1, s2)) <= 2
return [q for q in queries if any(f(q, word) for word in dictionary)] | words-within-two-edits-of-dictionary | Python 2 lines | SmittyWerbenjagermanjensen | 0 | 8 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,590 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757796/Python-edit-distance-DP-solution | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
res = []
for wd1 in queries:
for wd2 in dictionary:
if self.editDistance(wd1, wd2):
res.append(wd1)
break
return res
... | words-within-two-edits-of-dictionary | Python edit-distance DP solution | sakshampandey273 | 0 | 15 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,591 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757370/Python-1-liner-beats-100-easy-explained-with-comments | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
return [q for q in queries if any(sum(i != j for i, j in zip(q, d)) <= 2 for d in dictionary)] | words-within-two-edits-of-dictionary | ✅ Python 1 liner beats 100%, easy explained with comments | sezanhaque | 0 | 9 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,592 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757370/Python-1-liner-beats-100-easy-explained-with-comments | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
def check(word1: str, word2: str) -> bool:
# Check how many different character are between word1 & word2
# Return True if difference is <= 2
return sum(1 for i, j in zip(w... | words-within-two-edits-of-dictionary | ✅ Python 1 liner beats 100%, easy explained with comments | sezanhaque | 0 | 9 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,593 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757208/Python-3Trie-%2B-DFS | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
trie = {}
for word in dictionary:
node = trie
for w in word:
node = node.setdefault(w, {})
node['*'] = word
def d... | words-within-two-edits-of-dictionary | [Python 3]Trie + DFS | chestnut890123 | 0 | 29 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,594 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757144/Easy-Python-Solution | class Solution:
def twoEditWords(self, queries: List[str], dictt: List[str]) -> List[str]:
finalwords = []; n = len(queries[0])
for word in queries:
for wrd in dictt:
count = 0;
for i in range(n):
if (word[i] != wrd[i]): count += 1;
... | words-within-two-edits-of-dictionary | Easy Python Solution | avinashdoddi2001 | 0 | 9 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,595 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2757138/Python3-1-edit-for-dictionary-and-1-edit-for-queries | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
seen = set()
for word in dictionary:
for i in range(len(word)):
for ch in ascii_lowercase:
edit = word[:i] + ch + word[i+1:]
seen.add... | words-within-two-edits-of-dictionary | [Python3] 1 edit for dictionary & 1 edit for queries | ye15 | 0 | 12 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,596 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756950/Python-Answer-Comparing-Strings | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
def match(word:str,d):
t = 0
m = 2
for i,w in enumerate(word):
if w != d[i]:
m -=1
if m == -1:
... | words-within-two-edits-of-dictionary | [Python Answer🤫🐍🐍🐍] Comparing Strings | xmky | 0 | 9 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,597 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756821/Python-3-or-Brute-Force | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
ans=[]
for i in queries:
for j in dictionary:
ctr=0
for k in range(len(i)):
if i[k]!=j[k]:
ctr+=1
if... | words-within-two-edits-of-dictionary | Python 3 | Brute Force | RickSanchez101 | 0 | 6 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,598 |
https://leetcode.com/problems/words-within-two-edits-of-dictionary/discuss/2756818/Python-or-Easy-to-Understand | class Solution:
def twoEditWords(self, queries: List[str], dictionary: List[str]) -> List[str]:
res=[]
for i in queries:
for j in dictionary:
cnt=0
for char in range(len(i)):
if i[char]!=j[char]:
cnt+=1
if cnt<=2:
res.append... | words-within-two-edits-of-dictionary | Python | Easy to Understand | varun21vaidya | 0 | 6 | words within two edits of dictionary | 2,452 | 0.603 | Medium | 33,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.