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/two-furthest-houses-with-different-colors/discuss/1589999/Python3-Brute-Force-Solution | class Solution:
def maxDistance(self, colors: List[int]) -> int:
n = len(colors)
for i in range(n - 1, 0, -1):
for j in range(n - i):
if colors[j] != colors[j + i]:
return i | two-furthest-houses-with-different-colors | [Python3] Brute Force Solution | terrencetang | 0 | 38 | two furthest houses with different colors | 2,078 | 0.671 | Easy | 28,700 |
https://leetcode.com/problems/watering-plants/discuss/1589030/Python3-simulation | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
ans = 0
can = capacity
for i, x in enumerate(plants):
if can < x:
ans += 2*i
can = capacity
ans += 1
can -= x
return ans | watering-plants | [Python3] simulation | ye15 | 19 | 1,000 | watering plants | 2,079 | 0.8 | Medium | 28,701 |
https://leetcode.com/problems/watering-plants/discuss/2277430/PYTHON-3-FASTER-THAN-96.9-or-LESS-THAN-84.8 | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
s, p, c = 0, -1, capacity
for i, e in enumerate(plants):
if e <= c: s += i - p; c -= e
else: s += p + i + 2; c = capacity - e
p = i
return s | watering-plants | [PYTHON 3] FASTER THAN 96.9% | LESS THAN 84.8% | omkarxpatel | 2 | 60 | watering plants | 2,079 | 0.8 | Medium | 28,702 |
https://leetcode.com/problems/watering-plants/discuss/1657546/WEEB-DOES-PYTHONC%2B%2B | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
result = 0
curCap = capacity
for i in range(len(plants)):
if curCap >= plants[i]:
curCap -= plants[i]
result += 1
else:
result += i * 2 + 1
curCap = capacity - plants[i]
return result | watering-plants | WEEB DOES PYTHON/C++ | Skywalker5423 | 2 | 69 | watering plants | 2,079 | 0.8 | Medium | 28,703 |
https://leetcode.com/problems/watering-plants/discuss/2807935/Python-Straight-forward-simulation | class Solution:
def wateringPlants(self, plants: list[int], capacity: int) -> int:
steps = 1 # the first step from -1 to 0
cur_capacity = capacity
for i in range(len(plants) - 1):
cur_capacity -= plants[i] # watering the current plant
if cur_capacity < plants[i + 1]: # if water isn't enough
steps += (i + 1) * 2 + 1 # steps from i to -1 then from -1 to i + 1
cur_capacity = capacity # restore the capacity
else:
steps += 1 # just move forward
return steps | watering-plants | [Python] Straight-forward simulation | Mark_computer | 1 | 4 | watering plants | 2,079 | 0.8 | Medium | 28,704 |
https://leetcode.com/problems/watering-plants/discuss/2474176/easy-python-solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
po = capacity
for i in range(len(plants)):
if plants[i]<=capacity:
capacity-=plants[i]
steps+=1
else:
steps+=i
capacity = po
capacity-=plants[i]
steps+=(i+1)
return steps | watering-plants | easy python solution | rohannayar8 | 1 | 14 | watering plants | 2,079 | 0.8 | Medium | 28,705 |
https://leetcode.com/problems/watering-plants/discuss/1604339/Simple-for-loop-0(n)-time-complexity | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps=0
rem_capacity=capacity
for i in range(len(plants)-1):
rem_capacity-=plants[i]
steps+=1
if rem_capacity<plants[i+1]:
steps+=2*(i+1)
rem_capacity=capacity
if plants[-1]<=rem_capacity:
steps+=1
return steps
``` | watering-plants | Simple for loop 0(n) time complexity | PrimeOp | 1 | 35 | watering plants | 2,079 | 0.8 | Medium | 28,706 |
https://leetcode.com/problems/watering-plants/discuss/1589346/Python-or-Simulation-Explained-or-100-faster-in-both-time-and-space | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
ans = 0
cur = capacity
for i in range(len(plants)):
if plants[i] > cur:
cur = capacity
ans += 2 * i
ans += 1
cur -= plants[i]
return ans | watering-plants | Python | Simulation Explained | 100% faster in both time and space | GigaMoksh | 1 | 28 | watering plants | 2,079 | 0.8 | Medium | 28,707 |
https://leetcode.com/problems/watering-plants/discuss/2848603/O(n)-easy-solution-in-Python3 | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
firstCap, result = capacity, 0
for i in range(len(plants)):
if capacity >= plants[i]:
result += 1
else:
capacity = firstCap
result += i + i + 1
capacity -= plants[i]
return result | watering-plants | O(n) easy solution in Python3 | DNST | 0 | 1 | watering plants | 2,079 | 0.8 | Medium | 28,708 |
https://leetcode.com/problems/watering-plants/discuss/2804938/Python3-Solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
step = 0
i = 0
c = capacity
while i < len(plants):
step += 1
c = c - plants[i]
if i + 1 < len(plants) and c < plants[i + 1]:
step += (i + 1) * 2
c = capacity
i += 1
return step | watering-plants | Python3 Solution | sipi09 | 0 | 1 | watering plants | 2,079 | 0.8 | Medium | 28,709 |
https://leetcode.com/problems/watering-plants/discuss/2788162/Python3%3A-Just-doing-what-the-question-says! | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
jalLeft = capacity
for i, jalNeeded in enumerate(plants):
steps += 1
if jalLeft < jalNeeded:
steps += i + i
jalLeft = capacity
jalLeft -= jalNeeded
return steps | watering-plants | Python3: Just doing what the question says! | mediocre-coder | 0 | 1 | watering plants | 2,079 | 0.8 | Medium | 28,710 |
https://leetcode.com/problems/watering-plants/discuss/2775011/Python-O(n)-Solution | class Solution:
def wateringPlants(self, plants: List[int], cap: int) -> int:
steps = 0
cup = cap
for i in range(len(plants)):
if(cup<plants[i]):
steps+=i*2 # add steps to take a full trip back
cup = cap # marked filled
cup -= plants[i] #take water out
return steps + len(plants) #return the extra steps needed for refilling water and the # of plants | watering-plants | [Python] O(n) Solution | keioon | 0 | 3 | watering plants | 2,079 | 0.8 | Medium | 28,711 |
https://leetcode.com/problems/watering-plants/discuss/2750428/Python3-faster-than-96.3 | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
water = capacity
count = []
n = len(plants)
for i in range(n):
if water >= plants[i]:
count.append(1)
water -= plants[i]
else:
water = capacity - plants[i]
count.append(i)
count.append(i+1)
return sum(count) | watering-plants | Python3 faster than 96.3% | EdenXiao | 0 | 4 | watering plants | 2,079 | 0.8 | Medium | 28,712 |
https://leetcode.com/problems/watering-plants/discuss/2671537/Python3-Easy-Simulation | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
water, steps = capacity, 0
for i, needed in enumerate(plants):
if water >= needed:
steps += 1
water -= needed
else:
steps += 2 * (i+1) - 1
water = capacity - needed
return steps | watering-plants | [Python3] Easy Simulation | ivnvalex | 0 | 9 | watering plants | 2,079 | 0.8 | Medium | 28,713 |
https://leetcode.com/problems/watering-plants/discuss/2628984/Python-Easy-to-Understand | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
full = capacity
for i, j in enumerate(plants, 1):
if capacity >= j:
steps += 1
else:
steps += i + i - 1
capacity = full
capacity -= j
return steps | watering-plants | Python - Easy to Understand | Guild_Arts | 0 | 3 | watering plants | 2,079 | 0.8 | Medium | 28,714 |
https://leetcode.com/problems/watering-plants/discuss/2574588/Python-easy-solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
step = 0
cap = capacity
i=0
while(i<len(plants)):
if(cap >= plants[i]):
# water it
cap -= plants[i]
step += 1
else:
# refill
step += i*2
cap = capacity
# and water it
cap -= plants[i]
step += 1
i += 1
return step | watering-plants | Python easy solution | Jack_Chang | 0 | 5 | watering plants | 2,079 | 0.8 | Medium | 28,715 |
https://leetcode.com/problems/watering-plants/discuss/2304880/Python3-or-One-Pass | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
currCap=capacity
steps=0
for i in range(len(plants)):
currCap-=plants[i]
if currCap<0:
currCap=capacity-plants[i]
steps+=(2*i)+1
else:
steps+=1
return steps | watering-plants | [Python3] | One Pass | swapnilsingh421 | 0 | 11 | watering plants | 2,079 | 0.8 | Medium | 28,716 |
https://leetcode.com/problems/watering-plants/discuss/2300309/C%2B%2B-Python-Python3-Kotlin-oror-Easy-Explained-Solution-oror-O(n) | class Solution(object):
def wateringPlants(self, plants, capacity):
bucket = 0
steps = 0
for i in range(len(plants)):
if bucket >= plants[i]:
bucket -= plants[i]
steps += 1
else:
steps += (2*i) + 1
bucket = capacity
bucket -= plants[i]
return steps | watering-plants | C++, Python, Python3, Kotlin || Easy Explained Solution || O(n) | r_o_xx_ | 0 | 9 | watering plants | 2,079 | 0.8 | Medium | 28,717 |
https://leetcode.com/problems/watering-plants/discuss/2215027/Python3-Simple-solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
cap = capacity
for i, water in enumerate(plants):
# If capacity is more, water the plant and move to next step
if cap >= water:
steps += 1
else:
# Go back to -1 index, refill the capacity
steps += (2 * i + 1)
cap = capacity
cap -= water
return steps | watering-plants | [Python3] Simple solution | Gp05 | 0 | 11 | watering plants | 2,079 | 0.8 | Medium | 28,718 |
https://leetcode.com/problems/watering-plants/discuss/2176177/Python-or-easy-and-commented-code-or-90-faster | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
curCapacity = capacity
for i in range(len(plants)):
if curCapacity >= plants[i]:
# incrementing count when i can water plant
steps += 1
# decerementing the capacity by the amount of water which was consumed by the prevoius plant
curCapacity -= plants[i]
else:
# refilling the tank once the condition does not meet the requirement
curCapacity = capacity
# here decrementing the capacity because the ith plant consumed water when we will back from river
curCapacity -= plants[i]
# counting the steps for the ith index: moving from ith index to river and coming back to the ith idx
steps += (2 * i) + 1
return steps | watering-plants | Python | easy and commented code | 90% faster | __Asrar | 0 | 28 | watering plants | 2,079 | 0.8 | Medium | 28,719 |
https://leetcode.com/problems/watering-plants/discuss/2055121/Python-Solution-oror-Extremely-Easy-oror-Time-O(n)-Space%3A-O(1) | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
cur_pos = 0 # We assume we start at the first plant
steps = 1 # Step is needed to get to first plant
can = capacity - plants[0] # Water first plant
while cur_pos < len(plants) - 1:
if can >= plants[cur_pos + 1]: # If enough water is in the can, water the next plant and step forward
can -= plants[cur_pos + 1]
steps += 1
cur_pos += 1
else: # If there is not enough water, walk back to the river to fill
steps += 2 * (1 + cur_pos)
can = capacity
return steps | watering-plants | Python Solution || Extremely Easy || Time O(n), Space: O(1) | marmenante | 0 | 13 | watering plants | 2,079 | 0.8 | Medium | 28,720 |
https://leetcode.com/problems/watering-plants/discuss/1989860/Python-Fast-simple-and-optimized-Solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
current_capacity = capacity
steps = 0
for i in range(len(plants)):
if plants[i] <= current_capacity:
steps = steps + 1
current_capacity = current_capacity - plants[i]
else:
current_capacity = capacity - plants[i]
steps = steps + (i * 2) + 1
return steps | watering-plants | Python Fast , simple and optimized Solution | hardik097 | 0 | 21 | watering plants | 2,079 | 0.8 | Medium | 28,721 |
https://leetcode.com/problems/watering-plants/discuss/1852330/python3-solution-with-explanation | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
nsteps = 0 # track the number of steps taken thus far
bucket = capacity # track the current amount in the bucket
# let's start watering each plant
for i, p in enumerate(plants):
# we can water this plant
if p <= bucket:
nsteps += 1 # just moved one step up
bucket -= p # and reduced the bucket
# we cannot water this plant
else:
# need i steps to go back to the river (from prev plant)
# and i+1 steps to come back to current plant
nsteps += i+i+1
# new bucket will be full capacity minus the current plant
bucket = capacity-p
return nsteps | watering-plants | python3 solution with explanation | bhatiaharsh | 0 | 23 | watering plants | 2,079 | 0.8 | Medium | 28,722 |
https://leetcode.com/problems/watering-plants/discuss/1760316/python-oror-O(n)-oror-Easy-Solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
cap_rem = capacity
for i in range(len(plants)):
if cap_rem < plants[i]:
steps += 2 * i + 1
cap_rem = capacity - plants[i]
else:
steps += 1
cap_rem -= plants[i]
return steps | watering-plants | python || O(n) || Easy Solution | kalyan_yadav | 0 | 63 | watering plants | 2,079 | 0.8 | Medium | 28,723 |
https://leetcode.com/problems/watering-plants/discuss/1756984/Python-oror-Easy-Solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
can = capacity #Amount of water in the can
l = len(plants)
steps = 0
for i in range(0,l):
if i == l-1:
steps += 1
else:
can -= plants[i] # water left in the can after watering the plant[i]
if can - plants[i+1] < 0:
steps += (i + 1)+(i+2) # i+1 steps to go back to river and i+2 steps to come back to (i+1)th plant.
can = capacity
else:
steps += 1
return steps | watering-plants | Python || Easy Solution | MS1301 | 0 | 21 | watering plants | 2,079 | 0.8 | Medium | 28,724 |
https://leetcode.com/problems/watering-plants/discuss/1687843/Python3-oror-O(n)-time-complexity-oror-O(1)-space-complexity | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
max_capacity = capacity
for idx in range(len(plants)):
if plants[idx] <= capacity:
#water it
capacity -= plants[idx]
steps += 1
else:
go_backward = idx
go_forward = idx + 1
steps += go_forward + go_backward
capacity = max_capacity - plants[idx] # refill the can and water the plant
return steps | watering-plants | Python3 || O(n) time complexity || O(1) space complexity | s_m_d_29 | 0 | 33 | watering plants | 2,079 | 0.8 | Medium | 28,725 |
https://leetcode.com/problems/watering-plants/discuss/1664839/Python3-simple-solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps = 0
z = capacity
for i in range(len(plants)):
if z >= plants[i]:
steps += 1
z -= plants[i]
else:
steps += 2 * (i) + 1
z = capacity - plants[i]
return steps | watering-plants | Python3 simple solution | EklavyaJoshi | 0 | 24 | watering plants | 2,079 | 0.8 | Medium | 28,726 |
https://leetcode.com/problems/watering-plants/discuss/1642982/Python3-Solution-93-faster-than-others-and-easy-to-understand. | class Solution:
def wateringPlants(self, plants: list, capacity: int) -> int:
start = -1
steps = 0
_capacity = capacity
for index, plant_capacity in enumerate(plants):
if _capacity < plant_capacity:
steps+= (abs(start-(index-1)))*2
_capacity = capacity
steps+=1
_capacity = _capacity - plant_capacity
return steps | watering-plants | [Python3] Solution 93% faster than others & easy to understand. | GauravKK08 | 0 | 21 | watering plants | 2,079 | 0.8 | Medium | 28,727 |
https://leetcode.com/problems/watering-plants/discuss/1624996/Easy-python3-solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
res=1
cap=capacity
capacity-=plants[0]
n=len(plants)
for i in range(n-1):
if capacity<plants[i+1]:
res+=(i+1)
capacity=cap
res+=(i+2)
capacity-=plants[i+1]
else:
capacity-=plants[i+1]
res+=1
return res | watering-plants | Easy python3 solution | Karna61814 | 0 | 19 | watering plants | 2,079 | 0.8 | Medium | 28,728 |
https://leetcode.com/problems/watering-plants/discuss/1614379/All-in-one-solution | class Solution(object):
def wateringPlants(self, plants, capacity):
step = 0
temp = capacity
for index, plant in enumerate(plants):
if plant <= temp:
step += 1
temp -= plant
else:
step += index + index + 1
temp = capacity - plant
return step | watering-plants | All in one solution | aaffriya | 0 | 45 | watering plants | 2,079 | 0.8 | Medium | 28,729 |
https://leetcode.com/problems/watering-plants/discuss/1614379/All-in-one-solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
step = 0
temp = capacity
for index, plant in enumerate(plants):
if plant <= temp:
step += 1
temp -= plant
else:
step += index + index + 1
temp = capacity - plant
return step | watering-plants | All in one solution | aaffriya | 0 | 45 | watering plants | 2,079 | 0.8 | Medium | 28,730 |
https://leetcode.com/problems/watering-plants/discuss/1609892/Ez-Python3-Solution | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
n=len(plants)
steps=0
tempc=capacity
for i in range(n):
if tempc < plants[i] :
steps+=2*i+1
tempc=capacity-plants[i]
else :
steps+=1
tempc-=plants[i]
return steps | watering-plants | Ez Python3 Solution | P3rf3ct0 | 0 | 21 | watering plants | 2,079 | 0.8 | Medium | 28,731 |
https://leetcode.com/problems/watering-plants/discuss/1603403/Python-O(N) | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
idx, N, count, bucket = 0, len(plants), 0, capacity
while idx < N:
if bucket >= plants[idx]:
count += 1
bucket -= plants[idx]
else:
bucket = capacity - plants[idx]
count += idx * 2 + 1
idx += 1
return count | watering-plants | Python O(N) | jlee9077 | 0 | 19 | watering plants | 2,079 | 0.8 | Medium | 28,732 |
https://leetcode.com/problems/watering-plants/discuss/1594184/Python-sol-faster-than-85-better-mem-than-93-..-O(n)-sol | class Solution:
def wateringPlants(self, plants: List[int], cap: int) -> int:
res = 0
orgCap = cap
i = 0
while i < len(plants):
if cap >= plants[i]:
res += 1
cap -= plants[i]
i += 1
continue
if cap < plants[i]:
res += (2 * (i))
cap = orgCap
elif orgCap < plants[i]:
return
return res | watering-plants | Python sol faster than 85% , better mem than 93% .. O(n) sol | elayan | 0 | 25 | watering plants | 2,079 | 0.8 | Medium | 28,733 |
https://leetcode.com/problems/watering-plants/discuss/1592034/Python-3-O(n)-time | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
water = capacity
res = len(plants) # Will take at least len(plants) steps
for i in range(res):
if water < plants[i]: # Not enough water, take 2*i steps to go back and get more
water = capacity
res += 2*i
water -= plants[i]
return res | watering-plants | Python 3, O(n) time | dereky4 | 0 | 18 | watering plants | 2,079 | 0.8 | Medium | 28,734 |
https://leetcode.com/problems/watering-plants/discuss/1591826/Simple-Python3-Solution-O(n)-time | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
k,t=1,capacity
for i in range(0,len(plants)-1):
t-=plants[i]
if t<plants[i+1]:
k+=2*i+3
t=capacity
else:
k+=1
return k | watering-plants | Simple Python3 Solution O(n) time | Cathy-mico | 0 | 12 | watering plants | 2,079 | 0.8 | Medium | 28,735 |
https://leetcode.com/problems/watering-plants/discuss/1590165/Python-Easy-Solution-beat-100 | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
ans = 0
water = capacity
river = -1
for i, cost in enumerate(plants):
if cost > water:
ans += 2*(i-river) - 1
water = capacity
water -= cost
else:
ans += 1
water -= cost
return ans | watering-plants | [Python] Easy Solution beat 100% | nightybear | 0 | 15 | watering plants | 2,079 | 0.8 | Medium | 28,736 |
https://leetcode.com/problems/watering-plants/discuss/1590061/One-pass-100-speed-100-memory | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps, pos, can = 0, -1, capacity
for i, plant in enumerate(plants):
if plant <= can:
steps += i - pos
can -= plant
else:
steps += pos + i + 2
can = capacity - plant
pos = i
return steps | watering-plants | One pass, 100% speed 100% memory | EvgenySH | 0 | 12 | watering plants | 2,079 | 0.8 | Medium | 28,737 |
https://leetcode.com/problems/watering-plants/discuss/1589281/Python-3-Simulation-Time-O(n)-Space-O(1) | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
steps, c = 0, capacity
for i in range(len(plants)):
if c >= plants[i]:
steps += 1
c -= plants[i]
else:
steps += 2*i + 1
c = capacity - plants[i]
return steps
# Time: O(n), where n = len(plants)
# Space: O(1) | watering-plants | [Python 3] Simulation, Time O(n), Space O(1) | JosephJia | 0 | 13 | watering plants | 2,079 | 0.8 | Medium | 28,738 |
https://leetcode.com/problems/watering-plants/discuss/1589229/Python3.-We-humans-teach-machines-to-water-plants. | class Solution:
def wateringPlants(self, plants: List[int], capacity: int) -> int:
current_water = capacity
res = 0
for index in range(len(plants)):
if current_water >= plants[index]:
res = res + 1
current_water = current_water - plants[index]
else:
current_water = capacity - plants[index]
res = res + index*2 + 1
return res | watering-plants | Python3. We humans teach machines to water plants. | justicesuker | 0 | 13 | watering plants | 2,079 | 0.8 | Medium | 28,739 |
https://leetcode.com/problems/watering-plants/discuss/1589098/python-simple-simulation | class Solution:
def wateringPlants(self, arr: List[int], capacity: int) -> int:
n = len(arr)
cur = capacity
steps = 0
for i in range(n):
if arr[i] <= cur:
steps += 1
else:
steps += (2*i+1) #including forward and return steps
cur = capacity
cur -= arr[i]
return steps | watering-plants | python simple simulation | abkc1221 | 0 | 28 | watering plants | 2,079 | 0.8 | Medium | 28,740 |
https://leetcode.com/problems/sum-of-k-mirror-numbers/discuss/1589048/Python3-enumerate-k-symmetric-numbers | class Solution:
def kMirror(self, k: int, n: int) -> int:
def fn(x):
"""Return next k-symmetric number."""
n = len(x)//2
for i in range(n, len(x)):
if int(x[i])+1 < k:
x[i] = x[~i] = str(int(x[i])+1)
for ii in range(n, i): x[ii] = x[~ii] = '0'
return x
return ["1"] + ["0"]*(len(x)-1) + ["1"]
x = ["0"]
ans = 0
for _ in range(n):
while True:
x = fn(x)
val = int("".join(x), k)
if str(val)[::-1] == str(val): break
ans += val
return ans | sum-of-k-mirror-numbers | [Python3] enumerate k-symmetric numbers | ye15 | 41 | 2,700 | sum of k mirror numbers | 2,081 | 0.421 | Hard | 28,741 |
https://leetcode.com/problems/sum-of-k-mirror-numbers/discuss/1589255/Python3-Easy-Python-Brute-Force-with-comment | class Solution:
def kMirror(self, k: int, n: int) -> int:
def numberToBase(n, b):
if n == 0:
return [0]
digits = []
while n:
digits.append(n % b)
n //= b
return digits[::-1]
# not used
def baseToNumber(arr, b):
ans = 0
for x in arr:
ans = ans * b + int(x)
return ans
def is_mirror(s):
l, r = 0, len(s)-1
while l <= r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True
def gen():
'''
generate for value with different length
when i == 0: numοΌ[1, 10)
size of num: 1, 2 -> 1 or 11
when i == 1: [10, 100)
size of num: 3, 4 -> 10 or 101
when i == 2: [100, 1000)
size of num: 5, 6 -> 10001 or 100001
the num will be increasing
'''
for i in range(30):
for num in range(10**i, 10**(i+1)):
s = str(num) + str(num)[::-1][1:]
yield int(s)
for num in range(10**i, 10**(i+1)):
s = str(num) + str(num)[::-1]
yield int(s)
ans = 0
left = n
for num in gen():
base = numberToBase(num, k)
# if is_mirror(base):
if base == base[::-1]:
ans += num
left -= 1
if left == 0:
break
return ans | sum-of-k-mirror-numbers | [Python3] Easy Python Brute Force with comment | nightybear | 9 | 482 | sum of k mirror numbers | 2,081 | 0.421 | Hard | 28,742 |
https://leetcode.com/problems/sum-of-k-mirror-numbers/discuss/1591319/Recursive-Generator%2BBacktracking-Python-Solution | class Solution:
def kMirror(self, k: int, n: int) -> int:
def backtrack(uptonow: str, remaining_count: int):
'''Generator for yielding next base k symmetric string'''
if not uptonow:
yield from itertools.chain.from_iterable(backtrack(str(i), remaining_count - 1) for i in range(1, k))
elif not remaining_count:
yield uptonow
elif remaining_count > len(uptonow):
yield from itertools.chain.from_iterable(
backtrack(uptonow + str(i), remaining_count - 1) for i in range(k))
else:
yield from backtrack(uptonow + uptonow[remaining_count - 1], remaining_count - 1)
def generate_next_symmetric_base_k():
'''Generating Symmetric base k string with different lengths'''
number_length = 1
while True:
yield from backtrack(remaining_count=number_length, uptonow='')
number_length += 1
res = 0
base_k_symmetric_generator = generate_next_symmetric_base_k()
for _ in range(n):
while True:
next_symmetric_base_k = next(base_k_symmetric_generator)
base10 = int(next_symmetric_base_k, k)
base10_string = str(base10)
if base10_string == base10_string[::-1]:
break
res += base10
return res | sum-of-k-mirror-numbers | Recursive Generator+Backtracking Python Solution | msalarkia3 | 0 | 90 | sum of k mirror numbers | 2,081 | 0.421 | Hard | 28,743 |
https://leetcode.com/problems/sum-of-k-mirror-numbers/discuss/1590641/python3-two-palindrome-generators | class Solution:
def kMirror(self, k: int, n: int) -> int:
def createPalindromeBase(b: int):
l = 1
while True:
for i in range(b ** ((l - 1) // 2), b ** ((l + 1) // 2)):
n = i
pal = i
if l % 2 == 1:
n //= b
while n:
pal = pal * b + (n % b)
n //= b
yield pal
l += 1
pk = createPalindromeBase(k)
p10 = createPalindromeBase(10)
numbers = []
n10 = next(p10)
nk = next(pk)
while numbers.__len__() < n:
if n10 == nk:
numbers.append(n10)
if n10 < nk:
n10 = next(p10)
else:
nk = next(pk)
return sum(numbers) | sum-of-k-mirror-numbers | python3 two palindrome generators | hfweu | 0 | 71 | sum of k mirror numbers | 2,081 | 0.421 | Hard | 28,744 |
https://leetcode.com/problems/sum-of-k-mirror-numbers/discuss/1590101/Python-3-Traverse-k-base-numbers-and-iterative-building-(3020ms) | class Solution:
def kMirror(self, k: int, n: int) -> int:
# start from single digit base k
cands = [str(i) for i in range(1, k)]
ans = 0
while n > 0:
# check current canddiates to see if base 10 is also mirroring
for cand in cands:
b10 = str(int(cand, k))
if b10 == b10[::-1]:
ans += int(b10)
n -= 1
if n == 0: return ans
# construct new candidates
# if previous cand length is even just insert number between 0 and k - 1 into the middle
# if previous cand length is odd just insert number after len//2+1 and should be the same with left part end digit
new_cands = []
for cand in cands:
m = len(cand)
for i in range(k):
if m % 2 == 0:
new_cands.append(cand[:m//2] + str(i) + cand[m//2:])
else:
left, right = cand[:m//2+1], cand[m//2+1:]
if str(i) == left[-1]:
new_cands.append(left + str(i) + right)
cands = new_cands
return ans | sum-of-k-mirror-numbers | [Python 3] Traverse k base numbers and iterative building (3020ms) | chestnut890123 | 0 | 55 | sum of k mirror numbers | 2,081 | 0.421 | Hard | 28,745 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1598944/Python3-2-line-freq-table | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
freq1, freq2 = Counter(words1), Counter(words2)
return len({w for w, v in freq1.items() if v == 1} & {w for w, v in freq2.items() if v == 1}) | count-common-words-with-one-occurrence | [Python3] 2-line freq table | ye15 | 10 | 1,200 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,746 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1777937/2-Liner-Python-Solution-oror-110ms-(50-)-oror-Memory-(82-) | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
count = Counter(words1 + words2)
return len([word for word in count if count[word] == 2 and word in words1 and word in words2]) | count-common-words-with-one-occurrence | 2 Liner Python Solution || 110ms (50 %) || Memory (82 %) | Taha-C | 3 | 286 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,747 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2671493/Python-3 | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
"""d = {}
for xx in words1:
d[xx] = 1 + d.get(xx, 0)
count=0
for i,j in enumerate(d):
print(d[j])
if j in words2 and d[j]==1:
count+=1
d = {}
for xx in words2:
d[xx] = 1 + d.get(xx, 0)
coun=0
for i,j in enumerate(d):
print(d[j])
if j in words1 and d[j]==1:
coun+=1
return min(count,coun)"""
freq1, freq2 = Counter(words1), Counter(words2)
return len({w for w, v in freq1.items() if v == 1} & {w for w, v in freq2.items() if v == 1}) | count-common-words-with-one-occurrence | Python 3 | Sneh713 | 1 | 142 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,748 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2666156/Python3-solution-clean-code-with-full-comments. | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
counter = 0
count_words_1 = convert(words1)
count_words_2 = convert(words2)
count_words_1 = remove_multipel_occurences(count_words_1)
count_words_2 = remove_multipel_occurences(count_words_2)
# Check if two dictionarys have same keys and if so, counts them.
for i in count_words_2.keys():
if i in count_words_1:
counter += 1
return counter
# Helper function that will convert a list into a dictionary.
# List elements represented as KEYS and list element's occurence represented as VALUES.
def convert(array: List[str]):
d = dict()
for i in array:
if i in d:
d[i] += 1
else:
d[i] = 1
return d
# Helper method that search and deletes keys that have a value that is not 1.
def remove_multipel_occurences(dict):
for key in list(dict.keys()):
if dict[key] != 1:
del dict[key]
return dict
# If you like my work, then I'll appreciate your like. Thanks.
``` | count-common-words-with-one-occurrence | Python3 solution, clean code with full comments. | 375d | 1 | 110 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,749 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2513017/Python-or-2-liner-solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
fix = lambda w: set(filter(lambda x: x[1] == 1, Counter(w).items()))
return len(fix(words1) & fix(words2)) | count-common-words-with-one-occurrence | Python | 2-liner solution | Wartem | 1 | 64 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,750 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2056109/Count-Common-Words-With-One-Occurrence | class Solution(object):
def countWords(self, words1, words2):
"""
:type words1: List[str]
:type words2: List[str]
:rtype: int
"""
freq1, freq2 = Counter(words1), Counter(words2)
return len({w for w, v in freq1.items() if v == 1} & {w for w, v in freq2.items() if v == 1}) | count-common-words-with-one-occurrence | Count Common Words With One Occurrence | Muggles102 | 1 | 51 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,751 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2027197/Python-2-Lines!-Clean-and-Concise-Counter | class Solution:
def countWords(self, words1, words2):
c1, c2 = Counter(words1), Counter(words2)
return len([k for k,v in c1.items() if v==1 and c2[k]==1]) | count-common-words-with-one-occurrence | Python - 2 Lines! Clean and Concise - Counter | domthedeveloper | 1 | 84 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,752 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1697599/Python3-accepted-one-liner-solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
return len([i for i in words1 if(words1.count(i)==1 and words2.count(i)==1)]) | count-common-words-with-one-occurrence | Python3 accepted one-liner solution | sreeleetcode19 | 1 | 90 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,753 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2851452/python-easy-solution-2-line-greater | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
z =[];
x = [i for i in words1 if words1.count(i)==1];
y = [j for j in words2 if words2.count(j)==1];
for i in x :
if i in y:
z.append(i);
return len(z); | count-common-words-with-one-occurrence | python easy solution 2 line --> | seifsoliman | 0 | 1 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,754 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2820164/Simple-counter-solutions-beats-90 | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
count1 = Counter(words1)
count2 = Counter(words2)
count = 0
for k,v in count1.items():
if v == 1 and count2[k] == 1:
count += 1
return count | count-common-words-with-one-occurrence | Simple counter solutions beats 90% | aruj900 | 0 | 3 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,755 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2812917/Simple-Python-Solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
count = 0
for word in words1:
if words1.count(word) == words2.count(word) == 1:
count = count + 1
return count | count-common-words-with-one-occurrence | Simple Python Solution | danishs | 0 | 4 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,756 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2812736/Easy-set-intersection-Python | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
res =0
for i in set(words1).intersection(set(words2)):
if words1.count(i)==1 and words2.count(i)==1:
res +=1
return res | count-common-words-with-one-occurrence | Easy set intersection Python | ben_wei | 0 | 3 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,757 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2811771/Python-Solution-EASY-TO-UNDERSTAND | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
d1={}
for i in words1:
if i in d1:
d1[i]+=1
else:
d1[i]=1
d2={}
for i in words2:
if i in d2:
d2[i]+=1
else:
d2[i]=1
c=0
for i in d1.keys():
if i in d2 and d1[i]==1 and d2[i]==1:
c+=1
return c | count-common-words-with-one-occurrence | Python Solution - EASY TO UNDERSTANDβ | T1n1_B0x1 | 0 | 3 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,758 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2793109/Python-Basic-Solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
d={}
ans=0
for i in words1:
if i not in d:
d[i]=1
else:
d[i]+=1
for k,v in d.items():
if k in words2 and words2.count(k) == 1 and v == 1:
ans+=1
return ans | count-common-words-with-one-occurrence | Python Basic Solution | beingab329 | 0 | 3 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,759 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2786389/Python-solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
_map = {}
for i in words1:
_map[i] = _map.get(i, 0) + 1
_map = {key:value for (key, value) in _map.items() if value == 1}
ans = []
for i in words2:
if i in _map:
_map[i] -= 1
ans = [key for key in _map if _map[key] == 0]
return len(ans) | count-common-words-with-one-occurrence | Python solution | daniyar99 | 0 | 12 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,760 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2713633/Python3-Count-words-and-loop-over-shorter-Counter | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
# make two counters
cn1 = collections.Counter(words1)
cn2 = collections.Counter(words2)
# choose the shorter one for iteration
shorter, longer = (cn1, cn2) if len(cn1) <= len(cn2) else (cn2, cn1)
# make the computation
return sum(1 if amount == 1 and longer[word] == 1 else 0 for word, amount in shorter.items())
def countWords2(self, words1: List[str], words2: List[str]) -> int:
# make two counters
cn1 = collections.Counter(words1)
cn2 = collections.Counter(words2)
return sum(1 if amount == 1 and cn2[word] == 1 else 0 for word, amount in cn1.items())
def countWords3(self, words1: List[str], words2: List[str]) -> int:
# make two counters
cn1 = collections.Counter(words1)
cn2 = collections.Counter(words2)
# go through one count, check occurence in other counter
result = 0
for word, amount in cn1.items():
if amount == 1 and cn2[word] == 1:
result += 1
return result | count-common-words-with-one-occurrence | [Python3] - Count words and loop over shorter Counter | Lucew | 0 | 4 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,761 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2697713/Python-Simple-Python-Solution-Using-Count-Approach | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
result = 0
for word in words1:
if words1.count(word) == 1 and words2.count(word) == 1:
result = result + 1
return result | count-common-words-with-one-occurrence | [ Python ] β
β
Simple Python Solution Using Count Approach π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 0 | 15 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,762 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2668041/Easy-Python-solution-with-explanation | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
if len(words1) >= len(words2):
arr1 = words2
arr2 = words1
else:
arr1 = words1
arr2 = words2
count = 0
for x in arr1:
if arr1.count(x) == 1:
if arr2.count(x) == 1:
count += 1
return count | count-common-words-with-one-occurrence | Easy Python solution with explanation | anandanshul001 | 0 | 4 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,763 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2588460/Easy-python-solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
ans=0
temp={}
temp1={}
for x in words1:
temp[x]=temp[x]+1 if x in temp else 1
for x in words2:
temp1[x]=temp1[x]+1 if x in temp1 else 1
for x in temp.keys():
if x in temp1 and temp[x]==1 and temp1[x]==1:
ans+=1
return ans | count-common-words-with-one-occurrence | Easy python solution | I_am_SOURAV | 0 | 26 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,764 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2572986/python-solution-99.53-faster-using-collections! | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
dict_words1 = collections.Counter(words1)
dict_words2 = collections.Counter(words2)
count = 0
for key, val in dict_words1.items():
if val == 1 and key in dict_words2 and dict_words2[key] == 1:
count += 1
return count | count-common-words-with-one-occurrence | python solution 99.53% faster using collections! | samanehghafouri | 0 | 15 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,765 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2278715/Python3-oror-Simple-oror-Easy-to-understand | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
if len(words1) > len(words2):
d = dict(Counter(words2))
d2 = dict(Counter(words1))
else:
d = dict(Counter(words1))
d2 = dict(Counter(words2))
c = 0
for i in d:
if d[i] == 1:
if i in d2:
if d2[i] == 1:
c += 1
return c | count-common-words-with-one-occurrence | Python3 || Simple || Easy to understand | Poorti_maheshwari | 0 | 67 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,766 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2176987/Using-Set-and-Dictionary-Python | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
w1, w2 = {}, {}
for i in words1:
w1[i] = 1 + w1.get(i, 0)
for j in words2:
w2[j] = 1 + w2.get(j, 0)
# common keys
ck = set(w1) & set(w2)
res = 0
for c in ck:
if w1[c] == 1 and w2[c] == 1:
res += 1
return res | count-common-words-with-one-occurrence | Using Set and Dictionary Python | ankurbhambri | 0 | 46 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,767 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2129706/PYTHON-or-Simple-python-solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
map1 = {}
map2 = {}
for i in words1:
map1[i] = 1 + map1.get(i, 0)
for i in words2:
map2[i] = 1 + map2.get(i, 0)
res = 0
for i in map1:
if i in map2 and map2[i] == 1 and map1[i] == 1:
res += 1
return res | count-common-words-with-one-occurrence | PYTHON | Simple python solution | shreeruparel | 0 | 78 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,768 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2099766/Python-simple-solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
all_words = set(words1 + words2)
ans = 0
for i in all_words:
if words1.count(i) == 1 and words2.count(i) == 1:
ans += 1
return ans | count-common-words-with-one-occurrence | Python simple solution | StikS32 | 0 | 50 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,769 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/2035686/Python-Solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
w1, w2 = Counter(words1), Counter(words2)
return reduce(lambda x, y: x + (w1[y] == 1 and w2[y] == 1), w1.keys(), 0) | count-common-words-with-one-occurrence | Python Solution | hgalytoby | 0 | 65 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,770 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1990481/Python3-90-faster-with-explanation | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
mapper1, mapper2 = {}, {}
for x in words1:
if x in mapper1:
mapper1[x] += 1
else:
mapper1[x] = 1
for x in words2:
if x in mapper2:
mapper2[x] += 1
else:
mapper2[x] = 1
counter = 0
for x in mapper1:
if mapper1[x] == 1 and x in mapper2:
if mapper2[x] == 1:
counter += 1
return counter | count-common-words-with-one-occurrence | Python3, 90% faster with explanation | cvelazquez322 | 0 | 66 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,771 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1969184/Python-Easy-Soluction-or-Beats-90-submits | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
dict1 = collections.Counter(words1)
dict2 = collections.Counter(words2)
set1 = set()
set2 = set()
ans = 0
for d1 in dict1 :
if dict1[d1] == 1 :
set1.add(d1)
for d2 in dict2 :
if dict2[d2] == 1 :
set2.add(d2)
for s1 in set1 :
if s1 in set2 : ans += 1
return ans | count-common-words-with-one-occurrence | [ Python ] Easy Soluction | Beats 90% submits | crazypuppy | 0 | 42 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,772 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1930451/Python-dollarolution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
count = 0
for i in words1:
if words1.count(i) == 1:
if words2.count(i) == 1:
count += 1
return count | count-common-words-with-one-occurrence | Python $olution | AakRay | 0 | 37 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,773 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1846930/Easiest-and-Smallest-O(N)-Python3-Solution-oror-100-Faster-oror-Easy-to-Understand-oror-Explained | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
res=0
for i in words1:
c=words1.count(i)
if c==1:
if i in words2:
ct=words2.count(i)
if ct==1:
res=res+1
return res | count-common-words-with-one-occurrence | Easiest & Smallest O(N) Python3 Solution || 100% Faster || Easy to Understand || Explained | RatnaPriya | 0 | 47 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,774 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1837693/Python-easy-to-read-and-understand-or-hashmap | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
d = {}
for word in words1:
d[word] = d.get(word, 0) + 1
d_copy = d.copy()
for word in words2:
if word in d_copy:
d_copy[word] -= 1
ans = 0
for key in d_copy:
if d_copy[key] == 0 and d[key] == 1:
ans += 1
return ans | count-common-words-with-one-occurrence | Python easy to read and understand | hashmap | sanial2001 | 0 | 56 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,775 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1800762/Python3-98.8-faster-and-94-less-memory | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
count = 0
def array_to_dict(array):
"""Returns a dict with keys being the word and value being the number of occurences of the word in the array."""
output = {}
for word in array:
if word not in output:
output[word] = 1
else:
output[word] += 1
return output
word1_dict = array_to_dict(words1)
word2_dict = array_to_dict(words2)
for word, occurences in word1_dict.items():
if word not in word2_dict:
continue
if occurences == 1 and word2_dict[word] == 1:
count += 1
return count
``` | count-common-words-with-one-occurrence | Python3 - 98.8% faster & 94% less memory | jimmy605 | 0 | 73 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,776 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1760592/Python3-simple-solution-using-dictionary | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
d1 = {}
for i in range(len(words1)):
d1[words1[i]] = d1.get(words1[i],0) + 1
d2 = {}
for i in range(len(words2)):
d2[words2[i]] = d2.get(words2[i],0) + 1
count = 0
for i in range(len(words2)):
if words2[i] in d1 and d1[words2[i]] == 1 and d2[words2[i]] == 1:
count += 1
return count | count-common-words-with-one-occurrence | Python3 simple solution using dictionary | EklavyaJoshi | 0 | 66 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,777 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1709333/Python-Easy-approach-(list-in-dict) | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
s = {}
for w in words1:
if w not in s:
s[w] = [1, 0]
else:
s[w][0] += 1
for w in words2:
if w not in s:
s[w] = [0, 1]
else:
s[w][1] += 1
count = 0
for ls in s.values():
if ls[0] == 1 and ls[1] == 1:
count += 1
return count | count-common-words-with-one-occurrence | [Python] Easy approach (list in dict) | casshsu | 0 | 93 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,778 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1697319/Easy-solution(beginner) | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
count = 0
a=0
d={}
l={}
for i in words1:
d[i]=d.get(i,0)+1
print(d)
for j in words2:
l[j]=l.get(j,0)+1
print(l)
for i in d:
if i in l and d[i]==1 and l[i] == 1:
count += 1
return count
``` | count-common-words-with-one-occurrence | Easy solution(beginner) | Asselina94 | 0 | 52 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,779 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1634562/Python-Easy-Solution-or-Two-Approaches | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
# Approach 1:
count = 0
for ele in words1:
if words1.count(ele) == 1 and words2.count(ele) == 1:
count += 1
return count
# Approach 2:
lst1, lst2 = Counter(words1), Counter(words2)
return len({ele for ele, fre in lst1.items() if fre == 1} & {ele for ele, fre in lst2.items() if fre == 1}) | count-common-words-with-one-occurrence | Python Easy Solution | Two Approaches β | leet_satyam | 0 | 128 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,780 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1606756/Python3-One-liner-tuples-intersection | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
return len(set([(k,v) for k,v in Counter(words1).items() if v==1]) &\
set([(k,v) for k,v in Counter(words2).items() if v==1])) | count-common-words-with-one-occurrence | [Python3] - One-liner, tuples intersection | patefon | 0 | 43 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,781 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1601777/Python3-Solution-or-100-Less-Memory-Usage | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
a=0
for x in words1:
if words1.count(x)==1:
if words2.count(x)==1:
if x in words2:
a+=1
return a | count-common-words-with-one-occurrence | Python3 Solution | 100% Less Memory Usage | Captain_Leo | 0 | 66 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,782 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1601712/Python-3-beats-100-time-and-100-memory | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
c1 = collections.Counter(words1)
c2 = collections.Counter(words2)
if len(words1) > len(words2):
words1 = words2
return sum(c1[word] == 1 == c2[word] for word in words1) | count-common-words-with-one-occurrence | Python 3, beats 100% time and 100% memory | dereky4 | 0 | 81 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,783 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1600862/Two-Counters-100-speed | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
cnt1, cnt2 = Counter(words1), Counter(words2)
return sum(cnt1[w] == 1 == cnt2[w] for w in cnt1) | count-common-words-with-one-occurrence | Two Counters, 100% speed | EvgenySH | 0 | 44 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,784 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1599267/Python3-Simple-Solution | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
dct1, dct2 = collections.Counter(words1), collections.Counter(words2)
new1 = {el:freq for el, freq in dct1.items() if freq == 1}
new2 = {el:freq for el, freq in dct2.items() if freq == 1}
intrsct = new1.keys() & new2.keys()
return len(intrsct) | count-common-words-with-one-occurrence | Python3 Simple Solution | pedro_curto | 0 | 68 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,785 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1599116/Python3-or-2-Counters | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
counter=0
counter1=Counter(words1)
counter2=Counter(words2)
print(counter1)
print(counter2)
for key in counter1:
if counter1.get(key,0)==1 and counter2.get(key, 0)==1:
counter+=1
return counter | count-common-words-with-one-occurrence | Python3 | 2 Counters | sabrinawang4835 | 0 | 25 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,786 |
https://leetcode.com/problems/count-common-words-with-one-occurrence/discuss/1599327/Python-Counter-and-set | class Solution:
def countWords(self, words1: List[str], words2: List[str]) -> int:
w1 = set(w for w, c in Counter(words1).items() if c == 1)
w2 = set(w for w, c in Counter(words2).items() if c == 1)
return len(w1 & w2) | count-common-words-with-one-occurrence | Python, Counter and set | blue_sky5 | -1 | 55 | count common words with one occurrence | 2,085 | 0.697 | Easy | 28,787 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1598954/Python3-greedy | class Solution:
def minimumBuckets(self, street: str) -> int:
street = list(street)
ans = 0
for i, ch in enumerate(street):
if ch == 'H' and (i == 0 or street[i-1] != '#'):
if i+1 < len(street) and street[i+1] == '.': street[i+1] = '#'
elif i and street[i-1] == '.': street[i-1] = '#'
else: return -1
ans += 1
return ans | minimum-number-of-food-buckets-to-feed-the-hamsters | [Python3] greedy | ye15 | 10 | 541 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,788 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1598861/Python3-pattern-match | class Solution:
def minimumBuckets(self, street: str) -> int:
patterns = ['H.H', '.H', 'H.', 'H'] # 4 patterns (excluding '.' cuz it costs 0 )
costs = [1, 1, 1, -1] # corresponding costs
res = 0
for p, c in zip(patterns, costs): # firstly, detect 'H.H'; secondly, detect '.H' and 'H.'; ...
t = street.count(p) # occurences
if (t >= 1):
if (c == -1): # if found an impossible case...
return -1
res += c * t # accumulate the cost
street = street.replace(p, '#') # erase such arrangement
return res | minimum-number-of-food-buckets-to-feed-the-hamsters | [Python3] pattern match | macroway | 2 | 147 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,789 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/2247559/python-3-or-one-pass-greedy-solution-or-O(n)O(1) | class Solution:
def minimumBuckets(self, street: str) -> int:
n = len(street)
buckets = 0
prevBucket = -2
for i, c in enumerate(street):
if c == '.' or prevBucket == i - 1:
continue
buckets += 1
if i != n - 1 and street[i + 1] == '.':
prevBucket = i + 1
elif not i or street[i - 1] == 'H':
return -1
return buckets | minimum-number-of-food-buckets-to-feed-the-hamsters | python 3 | one pass greedy solution | O(n)/O(1) | dereky4 | 1 | 123 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,790 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1969446/Simple-Python-Solution | class Solution:
def minimumBuckets(self, street: str) -> int:
done = set()
n = len(street)
for i, c in enumerate(street):
if c == 'H':
if i - 1 in done or i + 1 in done:
continue
if i + 1 <= n - 1:
if street[i + 1] == ".":
done.add(i + 1)
continue
if i - 1 >= 0:
if street[i - 1] == ".":
done.add(i - 1)
continue
return -1
return len(done) | minimum-number-of-food-buckets-to-feed-the-hamsters | Simple Python Solution | user6397p | 1 | 123 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,791 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1851230/Python-easy-to-understand | class Solution:
def minimumBuckets(self, street: str) -> int:
c=0
ls=list(street)
for i in range(len(ls)):
if ls[i]=="H":
if i > 0 and ls[i-1]== "B":
continue
if i+1<len(ls) and ls[i+1]==".":
ls[i+1]="B"
c+=1
elif ls[i-1]=="." and i-1>=0:
ls[i-1]="B"
c+=1
else:
return -1
return c | minimum-number-of-food-buckets-to-feed-the-hamsters | Python easy to understand | nileshporwal | 1 | 119 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,792 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1598894/python-O(n)-solution | class Solution:
def minimumBuckets(self, street: str) -> int:
s = ['H'] + list(street) + ['H'] #for ensuring consistency in logic
n = len(s)
adj = 0 # counts parts like "H.H"
for i in range(1, n-1): #if 3 H are consecutive then impossible to fill
if s[i] == s[i-1] == s[i+1] == 'H':
return -1
for i in range(1, n-1):
if i not in (1, n-2) and s[i] == '.' and s[i-1] == s[i+1] == 'H':
s[i-1] = s[i+1] = '_' #change to any string for those adjacent cases
adj += 1
return street.count('H') - adj #simplified from adj(bucket b/w 2 houses) + n - 2*adj (single house with 1 bucket) | minimum-number-of-food-buckets-to-feed-the-hamsters | python O(n) solution | abkc1221 | 1 | 129 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,793 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/2813550/Python-(Simple-Maths) | class Solution:
def minimumBuckets(self, hamsters):
count, ans = 0, list(hamsters)
for i in range(len(ans)):
if ans[i] == "H":
if i > 0 and ans[i-1] == "B":
continue
if i+1 < len(ans) and ans[i+1] == ".":
ans[i+1] = "B"
count += 1
elif i-1 >= 0 and ans[i-1] == ".":
ans[i-1] = "B"
count += 1
else:
return -1
return count | minimum-number-of-food-buckets-to-feed-the-hamsters | Python (Simple Maths) | rnotappl | 0 | 3 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,794 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1629397/Python-two-lines | class Solution:
def minimumBuckets(self, street: str) -> int:
if street == "H" or street.startswith("HH") or street.endswith("HH") or "HHH" in street: return -1
return street.count("H") - street.count("H.H") | minimum-number-of-food-buckets-to-feed-the-hamsters | Python two lines | haruhiui | 0 | 138 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,795 |
https://leetcode.com/problems/minimum-number-of-food-buckets-to-feed-the-hamsters/discuss/1599200/Python-with-explanation-O(N)-Time-O(1)-space | class Solution:
def minimumBuckets(self, street: str) -> int:
l=list(street.strip())
c=0
n=len(street)
i=1
while i < len(street)-1:#fill the buckets containg houses on both sides and change H to T
if l[i-1]=="H" and l[i+1]=="H" and l[i]==".":
l[i-1]="T"
l[i+1]="T"
l[i]="B"
c+=1
i+=1
i=0
while i<len(street):#fill buckets for remaining houses
if i-1>=0:
if l[i-1]=="H":
if l[i]==".":
c+=1
l[i-1]="T"
l[i]="B"
if i+1<len(l):
if l[i+1]=="H":
if l[i]==".":
c+=1
l[i]="B"
l[i+1]="T"
i+=1
if "H" in l:
return -1
return c | minimum-number-of-food-buckets-to-feed-the-hamsters | Python with explanation O(N) Time O(1) space | nmk0462 | 0 | 78 | minimum number of food buckets to feed the hamsters | 2,086 | 0.451 | Medium | 28,796 |
https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/1598918/Greedy-Approach-oror-Well-Coded-and-Explained-oror-95-faster | class Solution:
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
src_x,src_y = startPos[0],startPos[1]
end_x,end_y = homePos[0], homePos[1]
if src_x < end_x:
rc = sum(rowCosts[src_x+1:end_x+1])
elif src_x > end_x:
rc = sum(rowCosts[end_x:src_x])
else:
rc=0
if src_y < end_y:
cc = sum(colCosts[src_y+1:end_y+1])
elif src_y > end_y:
cc = sum(colCosts[end_y:src_y])
else:
cc=0
return cc+rc | minimum-cost-homecoming-of-a-robot-in-a-grid | ππ Greedy Approach || Well-Coded and Explained || 95% faster π | abhi9Rai | 3 | 276 | minimum cost homecoming of a robot in a grid | 2,087 | 0.513 | Medium | 28,797 |
https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/1604280/Linear-solution-95-speed | class Solution:
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
cost = 0
if startPos[0] < homePos[0]:
cost += sum(rowCosts[r] for r in range(startPos[0] + 1,
homePos[0] + 1))
elif startPos[0] > homePos[0]:
cost += sum(rowCosts[r] for r in range(startPos[0] - 1,
homePos[0] - 1, -1))
if startPos[1] < homePos[1]:
cost += sum(colCosts[c] for c in range(startPos[1] + 1,
homePos[1] + 1))
elif startPos[1] > homePos[1]:
cost += sum(colCosts[c] for c in range(startPos[1] - 1,
homePos[1] - 1, -1))
return cost | minimum-cost-homecoming-of-a-robot-in-a-grid | Linear solution, 95% speed | EvgenySH | 1 | 110 | minimum cost homecoming of a robot in a grid | 2,087 | 0.513 | Medium | 28,798 |
https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/1598866/Python3-sum-on-path | class Solution:
def minCost(self, startPos: List[int], homePos: List[int], rowCosts: List[int], colCosts: List[int]) -> int:
ans = 0
if startPos[0] < homePos[0]: ans = sum(rowCosts[startPos[0]+1:homePos[0]+1])
elif startPos[0] > homePos[0]: ans = sum(rowCosts[homePos[0]:startPos[0]])
if startPos[1] < homePos[1]: ans += sum(colCosts[startPos[1]+1:homePos[1]+1])
elif startPos[1] > homePos[1]: ans += sum(colCosts[homePos[1]:startPos[1]])
return ans | minimum-cost-homecoming-of-a-robot-in-a-grid | [Python3] sum on path | ye15 | 1 | 93 | minimum cost homecoming of a robot in a grid | 2,087 | 0.513 | Medium | 28,799 |
Subsets and Splits