post_href
stringlengths 57
213
| python_solutions
stringlengths 71
22.3k
| slug
stringlengths 3
77
| post_title
stringlengths 1
100
| user
stringlengths 3
29
| upvotes
int64 -20
1.2k
| views
int64 0
60.9k
| problem_title
stringlengths 3
77
| number
int64 1
2.48k
| acceptance
float64 0.14
0.91
| difficulty
stringclasses 3
values | __index_level_0__
int64 0
34k
|
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2650405/O(n)-Time-O(1)-Space
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
b=0
for i in brokenLetters:
b= b | 1<<(ord(i)-97)
r=0
f=1
for i in text:
if i==' ':
r+=f
f=1
continue
if 1<<(ord(i)-97) & b :
f=0
return r+f
|
maximum-number-of-words-you-can-type
|
O(n) Time , O(1) Space
|
saianirudh_me
| 0
| 2
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,300
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2584982/Python-oror-Easy-and-well-explain-solution
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
count = 0
checker = 0
words = text.split()
for word in words:
for i in word:
if i in brokenLetters:
checker = 1
break
if checker == 0:
count +=1
checker = 0
return count
|
maximum-number-of-words-you-can-type
|
Python || Easy and well explain solution
|
ride-coder
| 0
| 25
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,301
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2575451/Simple-python-solution
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
text_list = text.split(" ")
res = 0
for word in text_list:
for ch in word:
if ch in brokenLetters:
break
else:
res += 1
return res
|
maximum-number-of-words-you-can-type
|
Simple python solution
|
aruj900
| 0
| 14
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,302
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2567377/Maximum-Number-of-words-you-can-type
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
word=text.split(" ")
fullyTyped=len(word)
for word1 in word:
for letter in word1:
if letter in brokenLetters:
fullyTyped-=1
break
return fullyTyped
|
maximum-number-of-words-you-can-type
|
Maximum Number of words you can type
|
KavitaPatel966
| 0
| 16
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,303
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2505524/Python3-No-hash-99-runtime-with-explanation
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
# Split the input into distinct words, and set the output equal to the number of words created
words = text.split(' ')
output = len(words)
# Loop through each word, and then loop through each letter of each word. If a broken letter is found in a word,
# remove one from the output, and move on to the next word
for word in words:
for letter in word:
if brokenLetters.find(letter) != -1:
output -= 1
break
# The output is the initial number of words, minus 1 for each word that contains broken letters
return output
|
maximum-number-of-words-you-can-type
|
[Python3] No hash - 99% runtime with explanation
|
connorthecrowe
| 0
| 27
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,304
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/2155068/Operations-over-sets
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
words = list(map(set, text.split()))
res = 0
lts = set(brokenLetters)
for w in words:
if not w & lts:
res += 1
return res
|
maximum-number-of-words-you-can-type
|
Operations over sets
|
dima62
| 0
| 23
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,305
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1846938/Easiest-and-Smallest-Python3-Solution-oror-100-Faster-oror-Easy-to-Understand-oror-Explained
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
ct=0
t=0
s=text.split(" ")
for i in s:
for j in i:
if j in brokenLetters:
ct=ct+1
if ct==0:
t=t+1
ct=0
return t
|
maximum-number-of-words-you-can-type
|
Easiest & Smallest Python3 Solution || 100% Faster || Easy to Understand || Explained
|
RatnaPriya
| 0
| 81
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,306
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1826915/easy-python-code
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
w = text.split(" ")
count = 0
for i in w:
flag = False
for j in brokenLetters:
if j in i:
flag = True
break
if flag == False:
count += 1
return count
|
maximum-number-of-words-you-can-type
|
easy python code
|
dakash682
| 0
| 60
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,307
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1755726/Simple-Python-Solution
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
arr=text.split(' ')
print(arr)
count=0
for word in arr:
flag=0
for char in word:
if char in brokenLetters:
flag=1
break
if flag==0:
count+=1
return count
|
maximum-number-of-words-you-can-type
|
Simple Python Solution
|
Siddharth_singh
| 0
| 42
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,308
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1754201/Idk-why-mine-is-still-slow
|
class Solution:
def canBeTypedWords(self, txt: str, bl: str) -> int:
bl=set(bl)
cnt=len(txt.split(' '))
for i in range(cnt):
s_txt = set(txt.split(' ')[i])
if bl.intersection(s_txt) != set() :
cnt -= 1
return cnt
|
maximum-number-of-words-you-can-type
|
Idk why mine is still slow
|
flame91
| 0
| 11
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,309
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1692621/Python3-Map-Reduce-and-sets
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
def helper(w):
return int(len(set(w) & set(brokenLetters)) == 0)
return reduce(lambda acc, x: acc+is_valid(x), text.split(), 0)
|
maximum-number-of-words-you-can-type
|
[Python3] - Map / Reduce and sets
|
patefon
| 0
| 37
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,310
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1620630/Easy-Python.-28ms-runtime
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
broken = set(list(brokenLetters))
res = 0
for word in text.split(" "):
validWord = True
for w in word:
if w in broken:
validWord = False
break
if validWord:
res+=1
return res
|
maximum-number-of-words-you-can-type
|
Easy Python. 28ms runtime
|
manassehkola
| 0
| 61
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,311
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1407160/Python3-Faster-than-76.50-of-the-Solutions
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
words = text.split()
result = []
for word in words:
flag = True
for c in word:
if c in brokenLetters:
flag = False
break
if flag:
result.append(word)
return len(result)
|
maximum-number-of-words-you-can-type
|
Python3 - Faster than 76.50% of the Solutions
|
harshitgupta323
| 0
| 61
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,312
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1392527/Python3-Faster-Than-97.50
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
b = set(brokenLetters)
c = len(text.split())
broken = False
for i in text:
if i in b:
broken = True
if i == ' ':
if broken:
broken = False
c -= 1
return c - 1 if broken else c
|
maximum-number-of-words-you-can-type
|
Python3 Faster Than 97.50%
|
Hejita
| 0
| 102
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,313
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1366572/Easy-Python-Solution
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
text = text.split()
length = len(text)
brokenLetters = set(brokenLetters)
for word in text:
for char in word:
if char in brokenLetters:
length -= 1
break
return length
|
maximum-number-of-words-you-can-type
|
Easy Python Solution
|
sangam92
| 0
| 78
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,314
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1357412/Python-fast-and-pythonic
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
def _checker(word):
return all([i not in brokenLetters for i in set(word)])
return len([True for word in text.split() if _checker(word)])
|
maximum-number-of-words-you-can-type
|
[Python] fast and pythonic
|
cruim
| 0
| 93
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,315
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1355900/Python3-hash-set
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
ans = 0
brokenLetters = set(brokenLetters)
for word in text.split():
if not set(word) & brokenLetters: ans += 1
return ans
|
maximum-number-of-words-you-can-type
|
[Python3] hash set
|
ye15
| 0
| 50
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,316
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1346290/Python-3-Simple-and-Fast-Solution-(24ms)
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
count = 0
broken_chars = set(brokenLetters)
for word in text.split(" "):
unique_chars = set(word)
count += (len(unique_chars - broken_chars) == len(unique_chars))
return count
|
maximum-number-of-words-you-can-type
|
[Python 3] Simple & Fast Solution (24ms)
|
wasi0013
| 0
| 51
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,317
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1345371/easy-python-solution-with-comments-and-explanation
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
# split the words by space delimiter
words = text.split()
#end result variable
count=0
for word in words:
flag=1 #boolean for adding current word to final list or not
for char in word:
if char in brokenLetters: #if char in the word is in brokenLetter list mark flag as 0 and break
flag=0
break
if flag:
count+=1
return count
|
maximum-number-of-words-you-can-type
|
easy python solution with comments and explanation
|
naveen_29
| 0
| 42
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,318
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1344979/Python-Easy
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
res, cnt = 0, 0
for val in text.split():
for t in brokenLetters:
if t not in val:
cnt += 1
else:
break
if cnt == len(brokenLetters):
res += 1
cnt = 0
return res
|
maximum-number-of-words-you-can-type
|
Python Easy
|
torch7
| 0
| 39
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,319
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1344899/Easy-Python-Solution
|
class Solution:
def canBeTypedWords(self, text: str, bL: str) -> int:
count=0
arr=text.split(" ")
for a in arr:
flag=0
for b in bL:
if b in a:
flag=1
break
if not flag:
count+=1
return count
|
maximum-number-of-words-you-can-type
|
Easy Python Solution
|
deleted_user
| 0
| 83
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,320
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1788795/98.85-lesser-memory-or-Simple-Python3-solution-or-O(n)-solution
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
text = text.split()
n = len(text)
for i in text:
for j in brokenLetters:
if j in i:
n -= 1
break
return n
|
maximum-number-of-words-you-can-type
|
✔98.85% lesser memory | Simple Python3 solution | O(n) solution
|
Coding_Tan3
| -1
| 62
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,321
|
https://leetcode.com/problems/maximum-number-of-words-you-can-type/discuss/1346611/Easy-Python-Solution(100)
|
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
t=text.split(" ")
b=list(brokenLetters)
print(t,b)
ans=[]
for i in range(len(t)):
c=0
for j in range(len(b)):
if(b[j] in t[i]):
c+=1
break
if(c==0):
ans.append(t[i])
return len(ans)
|
maximum-number-of-words-you-can-type
|
Easy Python Solution(100%)
|
Sneh17029
| -1
| 149
|
maximum number of words you can type
| 1,935
| 0.71
|
Easy
| 27,322
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1344878/Divide-gaps-by-dist
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
return sum((a - b - 1) // dist for a, b in zip(rungs, [0] + rungs))
|
add-minimum-number-of-rungs
|
Divide gaps by dist
|
votrubac
| 37
| 1,800
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,323
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1345016/O(n)-python-solution-with-explanation-for-(-1)
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
newrungs = 0
prev = 0
for rung in rungs:
diff = rung - prev
if diff > dist:
add = diff / dist # Number of rungs we need to add
if add % 1 == 0:
add = int(add) - 1
else:
add = int(add)
newrungs += add
prev = rung
return newrungs
|
add-minimum-number-of-rungs
|
O(n) python solution, with explanation for (-1)
|
kkochhar9
| 3
| 214
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,324
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1345016/O(n)-python-solution-with-explanation-for-(-1)
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
newrungs = 0
prev = 0
for rung in rungs:
diff = rung - prev - 1
newrungs += diff // dist
prev = rung
return newrungs
|
add-minimum-number-of-rungs
|
O(n) python solution, with explanation for (-1)
|
kkochhar9
| 3
| 214
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,325
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1364199/Python-easy-to-understand-oror-Faster-than-87.16
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
count = 0
if rungs[0] > dist:
m = rungs[0]
m = (m - 1) // dist
count += m
for i in range (len(rungs) - 1):
k = rungs[i+1] - rungs[i]
if k > dist:
n = (k-1) // dist
count += n
return count
|
add-minimum-number-of-rungs
|
Python easy to understand || Faster than 87.16%
|
adityarichhariya7879
| 2
| 136
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,326
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1361117/python3-or-Easy-or-For-beginners
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
h = 0
solution = 0
for r in rungs:
if (r-h)%dist==0:
solution+=(r-h)//dist-1
else:
solution+=(r-h)//dist
h=r
return solution
|
add-minimum-number-of-rungs
|
python3 | Easy | For beginners
|
charukalyani1098
| 1
| 112
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,327
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1344915/Best-Simple-one-pass-Solution-in-Python
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
height=0
sol=0
for r in rungs:
if (r-height)%dist==0:
sol+=(r-height)//dist-1
else:
sol+=(r-height)//dist
height=r
return sol
|
add-minimum-number-of-rungs
|
Best Simple one pass Solution in Python
|
deleted_user
| 1
| 113
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,328
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/2782614/Simply-syntax-on-Python-O(n)
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
curr, preSum = 0, []
for run in rungs:
preSum.append(run - curr)
curr = run
result = sum([ (k-1 if dist == 1 else (k//dist if k%dist > 0 else (k//dist) - 1) ) for k in preSum if k > dist])
return result
|
add-minimum-number-of-rungs
|
Simply syntax on Python O(n)
|
DNST
| 0
| 1
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,329
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/2670251/Greedy-oror-distance-oror-python-oror-TC-O(N)
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
ans = 0
if rungs[0] - 0 > dist:
ans += (rungs[0]-1)//dist
for i in range(len(rungs)):
diff = rungs[i] - rungs[i-1]
if diff > dist:
ans += (diff-1)//dist
return ans
|
add-minimum-number-of-rungs
|
Greedy || distance || python || TC - O(N)
|
mihirshah0114
| 0
| 4
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,330
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1808105/python-very-simple-solution
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
n = len(rungs)
res = 0
for i in range(n-1):
a, b = rungs[i], rungs[i+1]
s = b - a
if s > dist:
res += (s-1)//dist
if rungs[0] > dist:
res += (rungs[0]-1)//dist
return res
|
add-minimum-number-of-rungs
|
python very simple solution
|
byuns9334
| 0
| 82
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,331
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1804687/Python-O(n)-Easy-Understanding-clean-solution
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
count = 0
if dist < rungs[0]:
count += int((rungs[0]-1)/dist)
for i in range(len(rungs)-1):
if rungs[i]+dist < rungs[i+1]:
count += (rungs[i+1]-rungs[i]-1)//dist
return count
Upvote If you like this solution
|
add-minimum-number-of-rungs
|
Python O(n), Easy Understanding clean solution
|
Nish786
| 0
| 38
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,332
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1367310/Walrus-operator
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
rungs.insert(0, 0)
return sum((diff := b - a) // dist - (diff % dist == 0)
for a, b in zip(rungs, rungs[1:]))
|
add-minimum-number-of-rungs
|
Walrus operator
|
EvgenySH
| 0
| 59
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,333
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1367310/Walrus-operator
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
rungs.insert(0, 0)
return sum((b - a - 1) // dist for a, b in zip(rungs, rungs[1:]))
|
add-minimum-number-of-rungs
|
Walrus operator
|
EvgenySH
| 0
| 59
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,334
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1360277/Python3-Straightforward-O(n)-Solution-with-Explanation
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
rungs.insert(0,0) #Firstly, we add 0 to the start of the array. Because in the question, it says that we should start from ground.
fin = 0
for number in range(len(rungs)-1): #we iterate over the array, excluding last element.
if rungs[number+1]-rungs[number]>dist: #if the distance of the next element from the current element is greater than dist, we will add another element(s) between them
fin+= (rungs[number+1]-rungs[number]-1)//dist #the trick is, we extract 1 from the difference of these two elements.
#example: number1 is 2, number2 is 4, and dist is 1. (4-2) equal to 2. However, we can not add 2 elements between 2 and 4. this is why we extract 1 from the difference.
return fin
|
add-minimum-number-of-rungs
|
Python3 Straightforward O(n) Solution with Explanation
|
selimtanriverdien
| 0
| 76
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,335
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1355903/Python3-greedy
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
ans = prev = 0
for x in rungs:
ans += (x - prev - 1) // dist
prev = x
return ans
|
add-minimum-number-of-rungs
|
[Python3] greedy
|
ye15
| 0
| 55
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,336
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1346199/Python-3-O(n)-solution-easy-to-understand
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
current_rung = 0
count = 0
for next_rung in rungs:
if current_rung + dist < next_rung:
div, mod = divmod(next_rung - current_rung, dist)
count += div - (mod == 0)
current_rung = next_rung
return count
|
add-minimum-number-of-rungs
|
[Python 3] O(n) solution easy to understand
|
wasi0013
| 0
| 56
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,337
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1345014/Python3-easy-solution-with-explanation
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
rungsDiff = [rungs[0]] + [ rungs[i] - rungs[i-1] for i in range(1,len(rungs))]
addRung = 0
# Find elements in rungsDiff that are larger than dist
for i, diff in enumerate(rungsDiff):
if diff > dist:
addRung += (diff + dist-1) // dist - 1
return addRung
|
add-minimum-number-of-rungs
|
Python3 easy solution with explanation
|
wu1meng2
| 0
| 44
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,338
|
https://leetcode.com/problems/add-minimum-number-of-rungs/discuss/1344952/Python-Straight-forward
|
class Solution:
def addRungs(self, rungs: List[int], dist: int) -> int:
n = len(rungs)
count = 0
if dist == 1:
if rungs[0]>1:
count += rungs[0]-1
for i in range(n-1):
if rungs[i+1]-rungs[i] > 1:
count+= rungs[i+1]-rungs[i] - 1
return count
else:
if rungs[0] > dist:
count += (rungs[0]-1)//dist
for i in range(n-1):
if rungs[i+1] - rungs[i] > dist:
count += (rungs[i+1] - rungs[i] -1)//dist
return count
|
add-minimum-number-of-rungs
|
[Python] Straight forward
|
error504
| 0
| 40
|
add minimum number of rungs
| 1,936
| 0.429
|
Medium
| 27,339
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/2119013/Python%3A-Dynamic-Programming-O(mn)-Solution
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
m, n = len(points), len(points[0])
dp = points[0]
left = [0] * n ## left side contribution
right = [0] * n ## right side contribution
for r in range(1, m):
for c in range(n):
if c == 0:
left[c] = dp[c]
else:
left[c] = max(left[c - 1] - 1, dp[c])
for c in range(n - 1, -1, -1):
if c == n-1:
right[c] = dp[c]
else:
right[c] = max(right[c + 1] - 1, dp[c])
for c in range(n):
dp[c] = points[r][c] + max(left[c], right[c])
return max(dp)
|
maximum-number-of-points-with-cost
|
Python: Dynamic Programming O(mn) Solution
|
dadhania
| 14
| 784
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,340
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/2058133/python-3-oror-O(mn)-time-O(1)-space
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
m, n = len(points), len(points[0])
for i in range(m - 1):
for j in range(1, n):
points[i][j] = max(points[i][j], points[i][j - 1] - 1)
for j in range(n - 2, -1, -1):
points[i][j] = max(points[i][j], points[i][j + 1] - 1)
for j in range(n):
points[i + 1][j] += points[i][j]
return max(points[m - 1])
|
maximum-number-of-points-with-cost
|
python 3 || O(mn) time, O(1) space
|
dereky4
| 4
| 546
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,341
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/1704260/Python3-Solution
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
M = len(points)
N = len(points[0])
dp = [[0]*N for _ in range(M)]
for i in range(N):
dp[0][i] = points[0][i]
for i in range(1, M):
rollingMax = float('-inf')
for j in range(N):
rollingMax = max(rollingMax, dp[i-1][j] + j)
dp[i][j] = points[i][j] + rollingMax - j
rollingMax = float('-inf')
for j in range(N-1, -1, -1):
rollingMax = max(rollingMax, dp[i-1][j] - j)
dp[i][j] = max(dp[i][j], points[i][j] + rollingMax + j)
return max(dp[M-1])
|
maximum-number-of-points-with-cost
|
Python3 Solution
|
ruifate
| 3
| 738
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,342
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/1345120/Well-Explained-oror-88-faster-oror-DP-oror-pre-%2B-suff
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
m = len(points)
n = len(points[0])
if m==1:
return max(points[0])
if n==1:
s=0
for j in range(m):
s+=points[j][0]
return s
def lt(row):
left = [ele for ele in row]
for i in range(1,len(left)):
left[i] = max(left[i], left[i-1]-1)
return left
def rt(row):
right = [ele for ele in row]
for i in range(len(row)-2,-1,-1):
right[i] = max(right[i],right[i+1]-1)
return right
pre = points[0]
for i in range(1,m):
left = lt(pre)
right= rt(pre)
curr = [0 for _ in range(n)]
for j in range(n):
curr[j] = points[i][j]+max(left[j],right[j])
pre = curr[:]
return max(pre)
|
maximum-number-of-points-with-cost
|
🐍 Well-Explained || 88% faster || DP || pre + suff 📌
|
abhi9Rai
| 3
| 1,100
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,343
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/1542901/Intuitive-Python-DP-Solution
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
if not points:
return 0
res = max(points[0])
for r in range(1, len(points)):
dp = points[r-1]
for d in range(1, len(points[0])):
dp[d] = max(dp[d], dp[d-1]-1)
for e in range(len(points[0])-2, -1, -1):
dp[e] = max(dp[e], dp[e+1]-1)
for c in range(len(points[0])):
points[r][c] += dp[c]
res = max(res, points[r][c])
return res
|
maximum-number-of-points-with-cost
|
Intuitive Python DP Solution
|
tohbaino
| 2
| 1,100
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,344
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/1346338/Python-3-Priority-queue-O(m*n*log(n))
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
m, n = len(points), len(points[0])
for i in range(1, m):
right = []
for j, x in enumerate(points[i-1]):
heappush(right, (-(x - j), j))
left = []
for j in range(n):
tmp = points[i][j] + points[i-1][j]
while right and j >= right[0][1]:
heappop(right)
if j > 0:
heappush(left, (-points[i-1][j-1] - (j-1), j-1))
if right:
tmp = max(tmp, points[i][j] + j - right[0][0])
if left:
tmp = max(tmp, points[i][j] - j - left[0][0])
points[i][j] = tmp
return max(points[-1])
|
maximum-number-of-points-with-cost
|
[Python 3] Priority queue O(m*n*log(n))
|
chestnut890123
| 1
| 404
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,345
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/2807821/Python-(Simple-Dynamic-Programming)
|
class Solution:
def maxPoints(self, points):
m, n = len(points), len(points[0])
dp = points[0]
left, right = [0]*n, [0]*n
for i in range(1,m):
for j in range(n):
if j == 0:
left[j] = dp[j]
else:
left[j] = max(left[j-1]-1,dp[j])
for j in range(n-1,-1,-1):
if j == n-1:
right[j] = dp[j]
else:
right[j] = max(right[j+1]-1,dp[j])
for j in range(n):
dp[j] = points[i][j] + max(left[j],right[j])
return max(dp)
|
maximum-number-of-points-with-cost
|
Python (Simple Dynamic Programming)
|
rnotappl
| 0
| 6
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,346
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/2718103/Python3-or-Prefix%2BSuffix-Sum-%2B-Tabular-DP
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
row,col=len(points),len(points[0])
dp=[[0 for i in range(col)] for j in range(row)]
for i in range(col):
dp[0][i]=points[0][i]
prefix,suffix=[0]*col,[0]*col
prefix[0]=points[0][0]
suffix[col-1]=points[0][col-1]
for i in range(1,col):
prefix[i]=max(prefix[i-1]-1,points[0][i])
for j in range(col-2,-1,-1):
suffix[j]=max(suffix[j+1]-1,points[0][j])
for r in range(1,row):
for c in range(col):
dp[r][c]=points[r][c]+max(prefix[c],suffix[c])
prefix[0],suffix[col-1]=dp[r][0],dp[r][col-1]
for i in range(1,col):
prefix[i]=max(prefix[i-1]-1,dp[r][i])
for j in range(col-2,-1,-1):
suffix[j]=max(suffix[j+1]-1,dp[r][j])
return max(dp[row-1])
|
maximum-number-of-points-with-cost
|
[Python3] | Prefix+Suffix Sum + Tabular DP
|
swapnilsingh421
| 0
| 23
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,347
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/2104187/Python3-or-DP
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
m, n = len(points), len(points[0])
dp = [points[0]] + [[0] * n for _ in range(m - 1)]
for i in range(1,m):
prev_points = dp[i-1]
lft = dp[i - 1][::]
rht = dp[i - 1][::]
for j in range(1, n):
lft[j] = max(prev_points[j], lft[j - 1] - 1)
rht[- j - 1] = max(prev_points[- j - 1], rht[- j] - 1)
for j in range(n):
dp[i][j] = points[i][j] + max(lft[j], rht[j])
return max(dp[-1])
|
maximum-number-of-points-with-cost
|
Python3 | DP
|
yzhao156
| 0
| 266
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,348
|
https://leetcode.com/problems/maximum-number-of-points-with-cost/discuss/1355912/Python3-dp
|
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
m, n = len(points), len(points[0])
for i in range(1, m):
for j in range(n-2, -1, -1):
points[i-1][j] = max(points[i-1][j], points[i-1][j+1]-1)
prefix = 0
for j in range(n):
points[i][j] += max(prefix, points[i-1][j])
prefix = max(prefix, points[i-1][j]) - 1
return max(points[-1])
|
maximum-number-of-points-with-cost
|
[Python3] dp
|
ye15
| 0
| 369
|
maximum number of points with cost
| 1,937
| 0.362
|
Medium
| 27,349
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1359715/Python3-1-line
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
[Python3] 1-line
|
ye15
| 35
| 2,100
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,350
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1682769/93.6-accuracy-easiest-python-solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
a = set(s)
d = set()
for i in a:
d.add(s.count(i))
if len(d) == 1:
return True
else:
False
|
check-if-all-characters-have-equal-number-of-occurrences
|
93.6% accuracy easiest python solution
|
ebrahim007
| 2
| 133
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,351
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2818754/Python-oror-Easy-oror-97.05-Faster-oror-Dictionary-oror-O(n)-Solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
d={}
for i in s:
if i in d:
d[i]+=1
else:
d[i]=1
t=d[s[0]]
for v in d.values():
if v!=t:
return False
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python || Easy || 97.05% Faster || Dictionary || O(n) Solution
|
DareDevil_007
| 1
| 43
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,352
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2656488/Simple-oror-Python-O(N)
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
dicta={}
for i in s:
dicta[i]=s.count(i)
d=dicta[s[0]]
print(d)
for i,j in enumerate(dicta):
print(j)
if dicta[j]!=d:
return False
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
[Simple || Python O(N)]
|
Sneh713
| 1
| 101
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,353
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1980559/Python-(Simple-Approach-and-Beginner-Friendly)
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
a = []
for i in s:
a.append(s.count(i))
print(a)
if a.count(a[0]) == len(a):
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python (Simple Approach and Beginner-Friendly)
|
vishvavariya
| 1
| 77
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,354
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1894094/Python-Solution-or-100-Faster-or-HashMap-Based-or-Two-Solutions-Clean-Code-1-Liner
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
store = defaultdict()
for char in s:
store[char] += 1
if len(set(store.values())) == 1: return True
return False
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python Solution | 100% Faster | HashMap Based | Two Solutions - Clean Code / 1 Liner
|
Gautam_ProMax
| 1
| 116
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,355
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1894094/Python-Solution-or-100-Faster-or-HashMap-Based-or-Two-Solutions-Clean-Code-1-Liner
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python Solution | 100% Faster | HashMap Based | Two Solutions - Clean Code / 1 Liner
|
Gautam_ProMax
| 1
| 116
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,356
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1806924/Python-3-Solution-or-93-Faster-runtime-or-2-Lines-solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
lst = [s.count(i) for i in set(s)]
return len(set(lst)) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
✔Python 3 Solution | 93% Faster runtime | 2 Lines solution
|
Coding_Tan3
| 1
| 65
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,357
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1389203/Simple-Python-Solution-Using-Dictionary
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
if len(s)==1:
return True
dic={}
for i in s:
if i not in dic:
dic[i]=1
else:
dic[i]+=1
firs= dic.get(s[1])
for key,val in dic.items():
if val!=firs:
return False
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
Simple Python Solution Using Dictionary
|
sangam92
| 1
| 82
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,358
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2819241/Python-solution-using-set-and-Counter-one-line
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(collections.Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python solution using set and Counter one line
|
samanehghafouri
| 0
| 1
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,359
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2773821/Python-or-DictionaryorSetor-O(N)-time
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
d={}
for i in s:
d[i]=d.get(i,0)+1
s=set()
for i in d.values():
s.add(i)
return len(s)==1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python | Dictionary|Set| O(N) time
|
ankit0702
| 0
| 2
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,360
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2666167/Python-Three-liner-Solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
counts = collections.Counter(s)
reference = counts[s[0]]
return all(val == reference for val in counts.values())
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python Three-liner Solution
|
kcstar
| 0
| 4
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,361
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2604739/Solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
dtc = {}
for i in range(len(s)):
if s[i] in dtc:
dtc[s[i]] += 1
else:
dtc[s[i]] = 1
return (len(set(dtc.values())) < 2)
|
check-if-all-characters-have-equal-number-of-occurrences
|
Solution
|
fiqbal997
| 0
| 6
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,362
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2514081/Simple-python-solution-using-set
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
dic = collections.Counter(s)
count = set()
for k,v in dic.items():
count.add(v)
return len(count) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Simple python solution using set
|
aruj900
| 0
| 43
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,363
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2504510/Python-or-One-liner-with-Counter()-and-set()
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python | One-liner with Counter() and set()
|
Wartem
| 0
| 18
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,364
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2426757/1941.-Check-if-All-Characters-Have-Equal-Number-of-Occurrences%3A-One-Liner
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set([s.count(element)for element in set(s)]))==1
|
check-if-all-characters-have-equal-number-of-occurrences
|
1941. Check if All Characters Have Equal Number of Occurrences: One Liner
|
rogerfvieira
| 0
| 10
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,365
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2419255/Python-Solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python Solution
|
ethanroy
| 0
| 13
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,366
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2293619/Simple-Python3-Solution-without-using-collections.Counter()
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
distinct_chr = set(s)
num = set()
for c in distinct_chr:
num.add(s.count(c))
if len(num) == 1:
return True
else:
return False
|
check-if-all-characters-have-equal-number-of-occurrences
|
Simple Python3 Solution without using collections.Counter()
|
vem5688
| 0
| 35
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,367
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2239090/Beginner-Friendly-Solution-oror-36ms-Faster-Than-91-oror-Python
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
# Initialize a Python dictionary to store each character and the corresponding number of occurences.
ds = {}
# Build our dictionary.
for char in s:
if char not in ds:
ds.setdefault(char, 1)
else:
ds[char] += 1
# Initialize a list to store each unique value in ds.values().
# If len(ls) != 1 then s is not a "good string".
ls = []
for i in ds.values():
if i not in ls:
ls.append(i)
return len(ls) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Beginner Friendly Solution || 36ms, Faster Than 91% || Python
|
cool-huip
| 0
| 32
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,368
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2207528/Python-1-line
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(collections.Counter(s).values()))==1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python 1-line
|
XRFXRF
| 0
| 37
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,369
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2095015/PYTHON-or-Simple-python-solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
charMap = {}
for i in s:
charMap[i] = 1 + charMap.get(i, 0)
occurrence = charMap[s[0]]
for i in charMap:
if charMap[i] != occurrence:
return False
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
PYTHON | Simple python solution
|
shreeruparel
| 0
| 52
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,370
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2045073/Check-if-All-Characters-Have-Equal-Number-of-Occurrences
|
class Solution(object):
def areOccurrencesEqual(self, s):
"""
:type s: str
:rtype: bool
"""
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Check if All Characters Have Equal Number of Occurrences
|
Muggles102
| 0
| 26
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,371
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/2044854/Python-simple-solution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
a = s[0]
for i in set(s):
if s.count(i) != s.count(a):
return False
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python simple solution
|
StikS32
| 0
| 60
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,372
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1949283/Python3-Solution-with-using-counting
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
c = collections.Counter(s)
prev_cnt = 0
for key in c:
if prev_cnt != 0 and c[key] != prev_cnt:
return False
prev_cnt = c[key]
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
[Python3] Solution with using counting
|
maosipov11
| 0
| 34
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,373
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1930510/Python-One-Line-or-Clean-and-Simple!
|
class Solution:
def areOccurrencesEqual(self, s):
counts = list(Counter(s).values())
return all(count == counts[0] for count in counts)
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python - One Line | Clean and Simple!
|
domthedeveloper
| 0
| 55
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,374
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1930510/Python-One-Line-or-Clean-and-Simple!
|
class Solution:
def areOccurrencesEqual(self, s):
return (lambda cts : all(count == cts[0] for count in cts))(list(Counter(s).values()))
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python - One Line | Clean and Simple!
|
domthedeveloper
| 0
| 55
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,375
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1930510/Python-One-Line-or-Clean-and-Simple!
|
class Solution:
def areOccurrencesEqual(self, s):
counts = Counter(s).values()
return len(set(counts)) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python - One Line | Clean and Simple!
|
domthedeveloper
| 0
| 55
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,376
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1930510/Python-One-Line-or-Clean-and-Simple!
|
class Solution:
def areOccurrencesEqual(self, s):
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python - One Line | Clean and Simple!
|
domthedeveloper
| 0
| 55
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,377
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1875267/Python-dollarolution
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
s = Counter(s)
return len(set(s.values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python $olution
|
AakRay
| 0
| 34
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,378
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1701072/Python3-Solution-oror-Easy-way
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
lst = []
for x in s:
lst.append(s.count(x))
return len(set(lst)) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
[Python3] Solution || Easy way
|
Cheems_Coder
| 0
| 50
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,379
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1687284/No-frill
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
char_map = {}
for c in s:
count = char_map.setdefault(c, 0)
char_map[c] = count + 1
counts = char_map.values()
return len(set(counts)) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
No frill
|
snagsbybalin
| 0
| 23
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,380
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1560568/simple-solution-or-faster-than-~100
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
list_of_set_of_s = list(set(s))
i_index = 0
j_index = i_index + 1
while j_index <= len(list_of_set_of_s):
if len(list_of_set_of_s) < 2:
return True
break
elif s.count(list_of_set_of_s[i_index]) == s.count(list_of_set_of_s[j_index]):
if i_index == len(list_of_set_of_s)-1:
return True
break
else: i_index += 1
elif s.count(list_of_set_of_s[i_index]) != s.count(list_of_set_of_s[j_index]):
return False
break
|
check-if-all-characters-have-equal-number-of-occurrences
|
simple solution | faster than ~100%
|
anandanshul001
| 0
| 109
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,381
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1427351/Python3-or-easy-solution-or-set
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
d = set(collections.Counter(s).values())
if len(d) == 1:
return True
else:
return False
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python3 | easy solution | set
|
FlorinnC1
| 0
| 132
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,382
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1425624/python-sol-faster-than-98
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
dicc = {}
for i in s:
if i in dicc:
dicc[i] += 1
else:
dicc[i] = 1
for i in range(len(dicc.values()) - 1):
if list(dicc.values())[i] != list(dicc.values())[i + 1]:
return False
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
python sol faster than 98%
|
elayan
| 0
| 108
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,383
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1424161/Python-one-liner-and-one-more
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return len(set(Counter(s).values())) == 1
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python, one-liner and one more
|
blue_sky5
| 0
| 49
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,384
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1424161/Python-one-liner-and-one-more
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
counts = Counter(s).values()
return max(counts) == min(counts)
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python, one-liner and one more
|
blue_sky5
| 0
| 49
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,385
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1406449/Python3-Faster-than-83-of-the-Solutions
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
counts = [0]*26
for i in s:
index = ord(i)-97
counts[index] += 1
x = set(counts)
return len(x) <= 2
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python3 - Faster than 83% of the Solutions
|
harshitgupta323
| 0
| 49
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,386
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1396113/Python-Solution-oror-14.1-MB-less-than-86.94-of-Python3-online-submissions
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
new = []
for i in s:
new.append(s.count(i))
return 1 == len(list(set(new)))
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python Solution || 14.1 MB, less than 86.94% of Python3 online submissions
|
mzeli99
| 0
| 90
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,387
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1395521/python-oror-94-fast-oror-easy-oror-dictionary
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
d={}
lst=[]
for i in range (0,len(s)):
if s[i] not in d:
d[s[i]]=1
else:
d[s[i]]+=1
for value in d.values():
lst.append(value)
if len(set(lst)) > 1:
return False
else:
return True
|
check-if-all-characters-have-equal-number-of-occurrences
|
python || 94% fast || easy || dictionary
|
minato_namikaze
| 0
| 90
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,388
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1375260/Python3-1-line-Reduce
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
return reduce(lambda a,b : a&b, [set([val]) for val in collections.Counter(s).values()])
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python3 1-line [Reduce]
|
mikekaufman4
| 0
| 43
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,389
|
https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/discuss/1361043/Python-or-2-Lines-or-Counter
|
class Solution:
def areOccurrencesEqual(self, s: str) -> bool:
counts = list(Counter(s).values())
return all(count == counts[0] for count in counts)
|
check-if-all-characters-have-equal-number-of-occurrences
|
Python | 2 Lines | Counter
|
leeteatsleep
| 0
| 51
|
check if all characters have equal number of occurrences
| 1,941
| 0.768
|
Easy
| 27,390
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1359713/Python-Simple-Heap-Solution-with-Explanation
|
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
arrivals = []
departures = []
for ind, (x, y) in enumerate(times):
heappush(arrivals, (x, ind))
heappush(departures, (y, ind))
d = {}
occupied = [0] * len(times)
while True:
if arrivals and departures and arrivals[0][0] < departures[0][0]:
_, ind = heappop(arrivals)
d[ind] = occupied.index(0)
occupied[d[ind]] = 1
if ind == targetFriend:
return d[ind]
elif arrivals and departures and arrivals[0][0] >= departures[0][0]:
_, ind = heappop(departures)
occupied[d[ind]] = 0
|
the-number-of-the-smallest-unoccupied-chair
|
Python - Simple Heap Solution with Explanation
|
ajith6198
| 20
| 1,100
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,391
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1359722/Python3-sweeping
|
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
vals = []
for i, (arrival, leaving) in enumerate(times):
vals.append((arrival, 1, i))
vals.append((leaving, 0, i))
k = 0
pq = [] # available seats
mp = {} # player-to-seat mapping
for _, arrival, i in sorted(vals):
if arrival:
if pq: s = heappop(pq)
else:
s = k
k += 1
if i == targetFriend: return s
mp[i] = s
else: heappush(pq, mp[i]) # new seat available
|
the-number-of-the-smallest-unoccupied-chair
|
[Python3] sweeping
|
ye15
| 19
| 608
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,392
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1361964/python-heapq-(Beats-100)
|
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
n=len(times)
times=[(a,l,idx) for idx,(a,l) in enumerate(times)]
times.sort()
available=list(range(n)) #available chair no
used=[] #used chair (leaving,index)
heapify(available)
for a,l,i in times:
while used and used[0][0]>=a:
_,idx=heappop(used)
heappush(available,idx)
curr=heappop(available)
if i==targetFriend:
return curr
heappush(used,curr)
|
the-number-of-the-smallest-unoccupied-chair
|
python heapq (Beats 100%)
|
ketan_raut
| 1
| 92
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,393
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1360890/Faster-than-100-or-python
|
class Solution:
def smallestChair(self, times: List[List[int]], target: int) -> int:
x,y=times[target]
n=len(times)
new=[]
for i in range(0,n):
heapq.heappush(new,i)
heap=[]
times.sort()
min_heap=[]
#print(times)
for i,j in times:
#print(heap,i,j)
while(heap and heap[0][0]<=i):
heapq.heappush(new,heapq.heappop(heap)[1])
#heapq.heappop(heap)
mini=heapq.heappop(new)
if i==x and j==y:
if heap==[]:
return 0
else:
return mini
heapq.heappush(heap,(j,mini))
|
the-number-of-the-smallest-unoccupied-chair
|
Faster than 100% | python
|
heisenbarg
| 1
| 63
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,394
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1390653/Python-3-or-Heap-Sorting-Hash-Table-or-Explanation
|
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
n, d = len(times), dict()
heap = [i for i in range(n)]
cur = []
for i, (s, e) in enumerate(times):
cur += [(s, i), (-e, i)]
for idx, friend in sorted(cur, key=lambda x: (abs(x[0]), x[0])):
if idx > 0:
d[friend] = heapq.heappop(heap)
if friend == targetFriend: return d[friend]
else:
heapq.heappush(heap, d[friend])
return -1
|
the-number-of-the-smallest-unoccupied-chair
|
Python 3 | Heap, Sorting, Hash Table | Explanation
|
idontknoooo
| 0
| 105
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,395
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1368976/Readable-solution-two-heaps
|
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
arrivals = sorted((arrival, leave, friend) for friend, (arrival, leave)
in enumerate(times))
max_free_chair = 0
free_chairs = []
busy_chairs = []
for arrival, leave, friend in arrivals:
if busy_chairs:
time_free, chair = heappop(busy_chairs)
while time_free <= arrival:
heappush(free_chairs, chair)
if busy_chairs:
time_free, chair = heappop(busy_chairs)
else:
break
if time_free > arrival:
heappush(busy_chairs, (time_free, chair))
if free_chairs:
chair = heappop(free_chairs)
if friend == targetFriend:
return chair
heappush(busy_chairs, (leave, chair))
else:
if friend == targetFriend:
return max_free_chair
heappush(busy_chairs, (leave, max_free_chair))
max_free_chair += 1
return max_free_chair # we should not reach this line
|
the-number-of-the-smallest-unoccupied-chair
|
Readable solution, two heaps
|
EvgenySH
| 0
| 74
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,396
|
https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1360597/Python3-two-priority-queue-solution
|
class Solution:
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
#track the friend of interest since we are going to
#sort the array later
target_start, target_end = times[targetFriend]
#sort array by the time each friend arrive
times.sort(key = lambda x : x[0])
#preallocate the maximum num of chairs needed
#priority queue 1 - chairs, since we always want to
#get the chair with smallest index
chairs = list(range(len(times)+1))
heapify(chairs)
#priority queue 2 - occupied, we release chairs by
#the time they are done occupied
occupied = []
heapify(occupied)
for start, end in times:
#release the chairs that has no one sitting on
while occupied and occupied[0][0] <= start:
time, release = heappop(occupied)
heappush(chairs, release)
#get the chair with smallest index
target_chair = heappop(chairs)
#if friend of interest if found, return chair index
if start == target_start and end == target_end:
return target_chair
#otherwise, label this chair as occupied
heappush(occupied, (end, target_chair))
|
the-number-of-the-smallest-unoccupied-chair
|
Python3 two priority queue solution
|
xxHRxx
| 0
| 38
|
the number of the smallest unoccupied chair
| 1,942
| 0.406
|
Medium
| 27,397
|
https://leetcode.com/problems/describe-the-painting/discuss/1359717/Python-Easy-solution-in-O(n*logn)-with-detailed-explanation
|
class Solution:
def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
# via this mapping, we can easily know which coordinates should be took into consideration.
mapping = defaultdict(int)
for s, e, c in segments:
mapping[s] += c
mapping[e] -= c
res = []
prev, color = None, 0
for now in sorted(mapping):
if color: # if color == 0, it means this part isn't painted.
res.append((prev, now, color))
color += mapping[now]
prev = now
return res
|
describe-the-painting
|
[Python] Easy solution in O(n*logn) with detailed explanation
|
fishballLin
| 129
| 2,000
|
describe the painting
| 1,943
| 0.48
|
Medium
| 27,398
|
https://leetcode.com/problems/describe-the-painting/discuss/1359720/Line-Sweep
|
class Solution:
def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
mix, res, last_i = DefaultDict(int), [], 0
for start, end, color in segments:
mix[start] += color
mix[end] -= color
for i in sorted(mix.keys()):
if last_i in mix and mix[last_i]:
res.append([last_i, i, mix[last_i]])
mix[i] += mix[last_i]
last_i = i
return res
|
describe-the-painting
|
Line Sweep
|
votrubac
| 67
| 3,900
|
describe the painting
| 1,943
| 0.48
|
Medium
| 27,399
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.