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/string-matching-in-an-array/discuss/575116/Python-sol-sharing-85%2B-w-Comment | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
size = len( words )
substr = set( words[i] for i in range(size) for j in range(size) if i != j and words[i] in words[j] )
return [ *substr ] | string-matching-in-an-array | Python sol sharing 85%+ [w/ Comment] | brianchiang_tw | 2 | 404 | string matching in an array | 1,408 | 0.639 | Easy | 21,100 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1789873/1-Line-Python-Solution-oror-98-Faster-(32ms)-oror-Memory-Less-than-90 | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
return set([x for x in words for y in words if x != y and x in y]) | string-matching-in-an-array | 1-Line Python Solution || 98% Faster (32ms) || Memory Less than 90% | Taha-C | 1 | 124 | string matching in an array | 1,408 | 0.639 | Easy | 21,101 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1200561/Simple-Python-Self-Explanatory-Solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans = []
for i in range(0,len(words)):
for j in range(0,len(words)):
if (words[j] in words[i] and words[i]!=words[j]):
ans.append(words[j])
return list(set(ans)) | string-matching-in-an-array | Simple Python Self Explanatory Solution | ekagrashukla | 1 | 268 | string matching in an array | 1,408 | 0.639 | Easy | 21,102 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2812082/python-two-easy-to-read-solutions | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
wordAll = ' '.join(words)
output = [word for word in words if wordAll.count(word) > 1]
return output | string-matching-in-an-array | python - two easy to read solutions | IAMdkk | 0 | 6 | string matching in an array | 1,408 | 0.639 | Easy | 21,103 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2812082/python-two-easy-to-read-solutions | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
output = []
for i in range(len(words)):
for j in range(len(words)):
if words[i] in words[j] and i != j:
if words[i] not in output:
output.append(words[i])
return output | string-matching-in-an-array | python - two easy to read solutions | IAMdkk | 0 | 6 | string matching in an array | 1,408 | 0.639 | Easy | 21,104 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2796521/Simple-Python-Approach | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans =[]
for i in range(0,len(words)):
for j in range(0,len(words)):
if words[i] in words[j] and words[i] != words[j]:
ans.append(words[i])
return list(set(ans)) | string-matching-in-an-array | Simple Python Approach | Shagun_Mittal | 0 | 2 | string matching in an array | 1,408 | 0.639 | Easy | 21,105 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2777957/Python-Easy-Loop-Solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
lst = set()
for i in range(len(words)):
for j in range(len(words)):
if words[i]!=words[j]:
if words[i] in words[j]:
lst.add(words[i])
else:
pass
return lst | string-matching-in-an-array | Python Easy Loop Solution | Aayush3014 | 0 | 4 | string matching in an array | 1,408 | 0.639 | Easy | 21,106 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2775461/Python-Brute-force-Easy-to-Understand | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
s=words
s1=[]
for i in words:
for j in s:
if j !=i and i in j and i not in s1:
s1.append(i)
return s1 | string-matching-in-an-array | Python Brute force Easy to Understand | ganesh_5I4 | 0 | 2 | string matching in an array | 1,408 | 0.639 | Easy | 21,107 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2715362/Python-code-(97.52-faster) | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
x=dict()
for ele in words:
x[ele]=ele
ans=[]
for ele in words:
for w in x:
if ele in x[w] and ele!=w:
ans.append(ele)
ans=set(ans)
return list(ans) | string-matching-in-an-array | Python code (97.52% faster) | asiffarhan2701 | 0 | 26 | string matching in an array | 1,408 | 0.639 | Easy | 21,108 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2582687/Python-simple-solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
"""
- Sort by length of words and check if the previous is in the next word,
Why sort by length? because a smaller word can only be a substring of a longer word.
(time, space) complexity = 0(n^2), 0(n)
"""
words.sort(key=len)
res = []
for i in range(len(words)):
for j in range(i+1, len(words)):
if (words[i] in words[j]) and (words[i] not in res):
res.append(words[i])
return res | string-matching-in-an-array | Python simple solution | onosetaleoseghale | 0 | 25 | string matching in an array | 1,408 | 0.639 | Easy | 21,109 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2465859/Python-(Simple-Solution-and-Beginner-Friendly) | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
output = []
for i in range (0, len(words)):
for j in range(0, len(words)):
if words[j] != words[i]:
if words[j] in words[i]:
output.append(words[j])
return set(output) | string-matching-in-an-array | Python (Simple Solution and Beginner-Friendly) | vishvavariya | 0 | 28 | string matching in an array | 1,408 | 0.639 | Easy | 21,110 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2449284/Python-Solution-(-Fast-) | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
l = []
for i in words:
for j in words:
if i == j:
continue
if i in j:
l.append(i)
break
return l | string-matching-in-an-array | Python Solution ( Fast ) | SouravSingh49 | 0 | 37 | string matching in an array | 1,408 | 0.639 | Easy | 21,111 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2320978/Simple-python-O(n2) | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans=set()
l=len(words)
for i in range(l):
for j in range(l):
if (words[i] in words[j]) & (i!=j):
ans.add(words[i])
return ans | string-matching-in-an-array | Simple python O(n^2) | sunakshi132 | 0 | 61 | string matching in an array | 1,408 | 0.639 | Easy | 21,112 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2277754/Simple-solution-using-count-92-faster-97-less-Memory | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
out = []
total = '_'.join(words)
for i in words:
occurance = total.count(i)
if occurance >1:
out.append(i)
return out | string-matching-in-an-array | Simple solution using count 92 % faster 97 % less Memory | krishnamsgn | 0 | 31 | string matching in an array | 1,408 | 0.639 | Easy | 21,113 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2106777/Python-simple-solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans = []
for i in words:
for j in words:
if i == j:
continue
if j in i:
ans.append(j)
return set(ans) | string-matching-in-an-array | Python simple solution | StikS32 | 0 | 88 | string matching in an array | 1,408 | 0.639 | Easy | 21,114 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2037020/Python-Clean-and-Simple! | class Solution:
def stringMatching(self, words):
ans = set()
for i,subword in enumerate(words):
for j,word in enumerate(words):
if i == j: continue
if subword in word:
ans.add(subword)
break
return list(ans) | string-matching-in-an-array | Python - Clean and Simple! | domthedeveloper | 0 | 94 | string matching in an array | 1,408 | 0.639 | Easy | 21,115 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/2037020/Python-Clean-and-Simple! | class Solution:
def stringMatching(self, words):
ans = set()
words.sort(key=len)
for i,subword in enumerate(words):
for j,word in enumerate(words[i+1:]):
if subword in word:
ans.add(subword)
break
return list(ans) | string-matching-in-an-array | Python - Clean and Simple! | domthedeveloper | 0 | 94 | string matching in an array | 1,408 | 0.639 | Easy | 21,116 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1896202/Python-easy-brute-force-solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
res = []
for i in range(len(words)):
for j in range(len(words)):
if i != j:
if words[j] in words[i]:
if words[j] not in res:
res.append(words[j])
return res | string-matching-in-an-array | Python easy brute force solution | alishak1999 | 0 | 74 | string matching in an array | 1,408 | 0.639 | Easy | 21,117 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1826462/python-3-or-brute-force | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
n = len(words)
res = []
for i in range(n):
for j in range(n):
if i != j and words[i] in words[j]:
res.append(words[i])
break
return res | string-matching-in-an-array | python 3 | brute force | dereky4 | 0 | 64 | string matching in an array | 1,408 | 0.639 | Easy | 21,118 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1746582/Python-or-Simple-or-Beats-99.86-in-Memory | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
op = []
for i in range(len(words)):
temp = " ".join(words[:i] + words[i+1:])
if words[i] in temp:
op.append(words[i])
return op | string-matching-in-an-array | Python | Simple | Beats 99.86% in Memory | veerbhansari | 0 | 73 | string matching in an array | 1,408 | 0.639 | Easy | 21,119 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1745109/Python-dollarolution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
words.sort(key = len)
v = []
for i in range(len(words)):
for j in words[i+1:]:
if words[i] in j:
v.append(words[i])
break
return v | string-matching-in-an-array | Python $olution | AakRay | 0 | 80 | string matching in an array | 1,408 | 0.639 | Easy | 21,120 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1726963/Python3-or-1LS-or-Brute-force-solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
return list(set([w for word in words for w in words if w in word and w != word])) | string-matching-in-an-array | Python3 | 1LS | Brute force solution | khalidhassan3011 | 0 | 43 | string matching in an array | 1,408 | 0.639 | Easy | 21,121 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1726963/Python3-or-1LS-or-Brute-force-solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
return {w for word in words for w in words if w in word and w != word} | string-matching-in-an-array | Python3 | 1LS | Brute force solution | khalidhassan3011 | 0 | 43 | string matching in an array | 1,408 | 0.639 | Easy | 21,122 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1409708/Python3-Faster-Than-97.95 | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
words.sort(key = lambda x : len(x))
v = set()
for i in range(len(words) - 1):
for j in range(i + 1, len(words)):
if words[i] in words[j]:
v.add(words[i])
return v | string-matching-in-an-array | Python3 Faster Than 97.95% | Hejita | 0 | 130 | string matching in an array | 1,408 | 0.639 | Easy | 21,123 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1238632/python3-one-liner | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
return [words[i] for i in range(len(words)) if ' '.join(words[:i] + words[i+1:]).find(words[i]) != -1] | string-matching-in-an-array | python3 one-liner | S-c-r-a-t-c-h-y | 0 | 119 | string matching in an array | 1,408 | 0.639 | Easy | 21,124 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/1077497/Python3-simple-solution | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
pairs = ((x, y) for i, x in enumerate(words) for j, y in enumerate(words) if i != j)
return list({ a for a, b in pairs if a in b }) | string-matching-in-an-array | Python3 simple solution | EklavyaJoshi | 0 | 182 | string matching in an array | 1,408 | 0.639 | Easy | 21,125 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/815204/Python3-O(NlogN)-Time-Beats-99.9-Time-90-Space | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
words.sort(reverse=True, key=len)
ans = set()
for i in range(len(words)):
for j in range(1, len(words)):
if words[j] in words[0]:
ans.add(words[j])
words.pop(0)
return list(ans)
#O(NlogN) time - N is length of words list
#O(N) space - N is number of substrings | string-matching-in-an-array | Python3 O(NlogN) Time - Beats 99.9% Time, 90% Space | srf6413 | 0 | 179 | string matching in an array | 1,408 | 0.639 | Easy | 21,126 |
https://leetcode.com/problems/string-matching-in-an-array/discuss/655920/Python3-that-is-simple-and-faster-than-97.99-of-submissions-so-far | class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
words_set = words.sort(key=len)
ans = []
for i in range(len(words)):
for j in range(i+1, len(words)):
if words[i] in words[j]:
ans.append(words[i])
return set(ans) | string-matching-in-an-array | Python3 that is simple and faster than 97.99% of submissions so far | psparks | 0 | 183 | string matching in an array | 1,408 | 0.639 | Easy | 21,127 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/1702309/Understandable-code-for-beginners-in-python!!! | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
permuteArr=[i for i in range(1,m+1)]
query_len=len(queries)
answer=[]
left,right=[],[]
for query in range(query_len):
index=permuteArr.index(queries[query])
answer.append(index)
left=permuteArr[:index]
right=permuteArr[index+1:]
permuteArr=[permuteArr[index]]+left+right
return answer | queries-on-a-permutation-with-key | Understandable code for beginners in python!!! | kabiland | 1 | 46 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,128 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/972188/Python3-Solution-beats-90.80 | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
l = [i+1 for i in range(m)]
x = []
for i in queries:
n = l.index(i)
x.append(n)
l.insert(0,l.pop(n))
return x | queries-on-a-permutation-with-key | Python3 Solution beats 90.80 | EklavyaJoshi | 1 | 94 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,129 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/717534/Python-3.-Queries-on-Permutation-With-Key.-Beats-81 | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
res=[]
p=[]
for i in range(1,m+1):
p.append(i)
for i in range(len(queries)):
num = queries[i] # 3
idx = p.index(num) # get Index of 3 from P
res.append(idx)
temp=p[idx] #store 3 in temp
del p[idx] # delete 3 from P
p.insert(0,temp) # Insert 3 in P at position 0 i.e. -> Starting
return res | queries-on-a-permutation-with-key | [Python 3]. Queries on Permutation With Key. Beats 81% | tilak_ | 1 | 82 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,130 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/2779857/python-weird-and-ugly-but-somehow-beats-100-time-for-python3 | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
p=[i for i in range(m+1)]
scnt=1
rcnt=0
sh={}
ans=[]
def getPosBin(v, l, r):
mid=(r+l)//2
mval=p[mid]
while mval!=v:
if v>mval:
l=mid+1
else:
r=mid-1
mid=(l+r)//2
mval=p[mid]
return mid
for v in queries:
if v not in sh:
l=max(scnt, v)
r=min(l+v-p[l], m)
pos=getPosBin(v, l, r)
sh[v]=[scnt, rcnt]
scnt+=1
else:
l=scnt-sh[v][0]
r=l+rcnt-sh[v][1]
for i in range(l, r+1):
if p[i]==v:
pos=i
break
sh[v]=[scnt, rcnt]
rcnt+=1
ans.append(pos-1) #p is 1-indexed unlike the answer
p.insert(1, p.pop(pos))
return ans | queries-on-a-permutation-with-key | [python] weird and ugly, but somehow beats 100% time for python3 | kagotpush | 0 | 11 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,131 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/2759743/easy-python-solutionororO(n)-beats-90 | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
ans = []
p = []
maxq = max(queries)
for i in range(1,maxq+1):
p.append(i)
for ele in queries:
ans.append(p.index(ele))
p.remove(ele)
p.insert(0,ele)
return ans | queries-on-a-permutation-with-key | easy python solution||O(n) ,beats 90% | chessman_1 | 0 | 13 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,132 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/2705180/Python3-solution | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
P = list(range(1,m+1))
ans = []
for q in queries:
ind = P.index(q)
ans.append(ind)
P.insert(0,P.pop(ind))
return ans | queries-on-a-permutation-with-key | Python3 solution | sipi09 | 0 | 2 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,133 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/2698633/Python3-Simple-Solution | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
res = []
P = [i for i in range(1, m + 1)]
for query in queries:
pos = P.index(query)
P.remove(query)
P = [query] + P
res.append(pos)
return res | queries-on-a-permutation-with-key | Python3 Simple Solution | mediocre-coder | 0 | 5 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,134 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/2539083/Python-or-loop-or-O(n)-Time-or-O(m)-Space | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
ans = []
perm = [i for i in range(1,m+1)]
for query in queries:
index = perm.index(query)
ans.append(index)
perm = [query]+perm[0:index]+perm[index+1:]
return ans | queries-on-a-permutation-with-key | Python | loop | O(n) Time | O(m) Space | coolakash10 | 0 | 11 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,135 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/2137309/Queries-on-a-Permutation-With-Key | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
arr = [i for i in range(1,m+1)]
ans = []
for i in range(len(queries)):
ind = arr.index(queries[i])
ans.append(ind)
val = 0
while val<ind:
temp = arr[ind]
arr[ind] = arr[val]
arr[val] = temp
val+=1
return ans | queries-on-a-permutation-with-key | Queries on a Permutation With Key | somendrashekhar2199 | 0 | 28 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,136 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/1695294/Python-using-a-deque | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
l = []
q = deque(range(1, m+1))
for v in queries:
i = q.index(v)
l.append(i)
q.remove(v)
q.appendleft(v)
return l | queries-on-a-permutation-with-key | Python, using a deque | emwalker | 0 | 46 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,137 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/1385519/Explanation-for-what's-happening-to-permutations-Python3-4-lines | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
n = len(queries); result = []
q = collections.deque([i for i in range(1,m+1)]) # holds permutation - deque can append on left side
for query in queries:
result.append(q.index(query)) # save the place query is in
q.remove(query); q.appendleft(query) # move query to front
return result | queries-on-a-permutation-with-key | Explanation for what's happening to permutations [Python3 4 lines] | mikekaufman4 | 0 | 48 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,138 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/669292/Python3-Solution-79 | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
P = [*range(1,m+1)]
output= []
for i in range(len(queries)):
currentInt = queries[i]
getIndex = P.index(currentInt)
output.append(getIndex)
P.remove(currentInt)
P.insert(0,currentInt)
return output | queries-on-a-permutation-with-key | Python3 Solution 79% | xuexiaoyu | 0 | 94 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,139 |
https://leetcode.com/problems/queries-on-a-permutation-with-key/discuss/633129/Simple-and-easy-to-understand-Python-Solution | class Solution:
def processQueries(self, queries: List[int], m: int) -> List[int]:
p=[]
for i in range(1,m+1):
p.append(i)
result=[]
for i in range(len(queries)):
x=queries[i]
idx=p.index(x)
p.pop(idx)
p.insert(0,x)
result.append(idx)
return result
print(result) | queries-on-a-permutation-with-key | Simple and easy to understand Python Solution | Ayu-99 | 0 | 50 | queries on a permutation with key | 1,409 | 0.833 | Medium | 21,140 |
https://leetcode.com/problems/html-entity-parser/discuss/575248/Python-sol-by-replace-and-regex.-85%2B-w-Hint | class Solution:
def entityParser(self, text: str) -> str:
html_symbol = [ '&quot;', '&apos;', '&gt;', '&lt;', '&frasl;', '&amp;']
formal_symbol = [ '"', "'", '>', '<', '/', '&']
for html_sym, formal_sym in zip(html_symbol, formal_symbol):
text = text.replace( html_sym , formal_sym )
return text | html-entity-parser | Python sol by replace and regex. 85%+ [w/ Hint ] | brianchiang_tw | 11 | 740 | html entity parser | 1,410 | 0.52 | Medium | 21,141 |
https://leetcode.com/problems/html-entity-parser/discuss/579353/Clean-Python-3-generator-and-trie | class Solution:
def entityParser(self, text: str) -> str:
def add(entity: str, symbol: str):
node = trie
for c in entity:
node = node.setdefault(c, {})
node['#'] = symbol
def check(idx: int) -> tuple:
node = trie
while text[idx] in node:
node = node[text[idx]]
idx += 1
if '#' in node: return node['#'], idx
return False, idx
def parse():
i = 0
while i < len(text):
if text[i] in trie:
symbol, j = check(i)
yield symbol or text[i:j]
i = j
else:
yield text[i]
i += 1
trie = {}
entities = [('&quot;', '"'), ('&apos;', "'"), ('&amp;', '&'), ('&gt;', '>'), ('&lt;', '<'), ('&frasl;', '/')]
for entity, symbol in entities:
add(entity, symbol)
return ''.join(parse()) | html-entity-parser | Clean Python 3, generator and trie | lenchen1112 | 3 | 477 | html entity parser | 1,410 | 0.52 | Medium | 21,142 |
https://leetcode.com/problems/html-entity-parser/discuss/712784/simple-of-simple | class Solution:
def entityParser(self, text: str) -> str:
m = {
'&quot;': '\"',
'&apos;': "'",
'&amp;': '&',
'&gt;': '>',
'&lt;': '<',
'&frasl;': '/'
}
delimiter = '$!@#$%^&$'
for k, v in m.items():
text = text.replace(k, v+delimiter)
return text.replace(delimiter, '') | html-entity-parser | simple of simple | seunggabi | 2 | 154 | html entity parser | 1,410 | 0.52 | Medium | 21,143 |
https://leetcode.com/problems/html-entity-parser/discuss/577029/A-Simple-Python-3-Solution!(-Explained) | class Solution:
def entityParser(self, text: str) -> str:
dat={"&quot;":"\"",
"&apos;":"'",
"&amp;":"&",
"&gt;":">",
"&lt;":"<",
"&frasl;":"/",
}
txt=''
amp_idx,sem_idx=None,None
for i,e in enumerate(text):
# if & we update the amp_idx
if e=="&":
amp_idx=i
# if ; we update sem idx
if e==";":
sem_idx=i
# if we don't have any amp_idx yet to be tracked just add the curr char from text
if amp_idx==None:
txt+=e
# if we have amp_idx and sem_idx, means we have a contiguous block to compare in dat dictonary
if amp_idx!=None and sem_idx!=None:
key = text[amp_idx:sem_idx+1] # we get that block to compare from text
# if key in dat then we add the replacement in txt. e.g: &gt replace with >
if key in dat.keys():
txt+=dat[key]
# but what if we don't have that key in dat? e.g: &ambassador;. so we just add the full string aka key
else:
txt+=key
# assign the idx tracker to None to track next blocks
amp_idx,sem_idx=None,None
return txt | html-entity-parser | A Simple Python 3 Solution!( Explained) | xrssa | 1 | 190 | html entity parser | 1,410 | 0.52 | Medium | 21,144 |
https://leetcode.com/problems/html-entity-parser/discuss/576773/Python-Stack-solution | class Solution:
def entityParser(self, text):
END = '&'
START = ';'
d = {
'&quot;': '"',
'&apos;': "'",
'&amp;': '&',
'&gt;': '>',
'&lt;': '<',
'&frasl;': '/',
}
stack = []
seen_start = False
for ch in reversed(text):
if ch == START:
seen_start = True
stack.append(ch)
if ch == END and seen_start:
# check for a match
temp = []
while stack[-1] != START:
temp.append(stack.pop())
temp.append(stack.pop()) # the ;
val = ''.join(temp)
if val in d:
stack.append(d[val])
else:
stack.append(val)
seen_start = False
return ''.join(reversed(stack)) | html-entity-parser | [Python] Stack solution | hw11 | 1 | 193 | html entity parser | 1,410 | 0.52 | Medium | 21,145 |
https://leetcode.com/problems/html-entity-parser/discuss/2459830/Python-intuitive-solution-O(N)-by-using-finite-state-machine | class Solution:
def entityParser(self, text: str) -> str:
"""
and I quote: &quot;...&quot;
case ordinary -> append to result
case & -> update the index
case ; -> try to decode the pattern
"""
# try to decode the last pattern
def convert(res, lastIdx):
tmp = res[lastIdx:]
if tmp in mp:
res = res[:lastIdx] + mp[tmp]
return res
res = ''
mp = {
'&quot;':'"',
'&apos;':"'",
'&amp;':'&',
'&gt;':'>',
'&lt;':'<',
'&frasl;':'/'
}
lastIdx = -1
for c in text:
res += c
# & case, update the postion
if c == '&':
lastIdx = len(res)-1
continue
# ; enclose the html code, try to decode it
if c == ';':
if lastIdx != -1:
res = convert(res,lastIdx)
lastIdx = -1
continue
return res | html-entity-parser | Python intuitive solution O(N) by using finite state machine | user9178q | 0 | 29 | html entity parser | 1,410 | 0.52 | Medium | 21,146 |
https://leetcode.com/problems/html-entity-parser/discuss/1857545/Simple-for-beginners | class Solution:
def entityParser(self, text: str) -> str:
chars = {'&quot;':'"',
'&apos;':'\'',
'&amp;':'&',
'&gt;':'>',
'&lt;':'<',
'&frasl;':'/'}
output = ''
i = 0
while i < len(text):
if text[i] != '&':
output += text[i]
i += 1
else:
if i == len(text) - 1:
output += text[i]
break
check = True
next_idx = i + 1
while text[next_idx] != ';':
if text[next_idx] == '&':
check = False
break
else:
next_idx += 1
if check:
next_idx += 1
string = text[i:next_idx]
if string in chars:
output += chars[string]
else:
output += string
i = next_idx
return output | html-entity-parser | Simple for beginners | MeltMelt | 0 | 73 | html entity parser | 1,410 | 0.52 | Medium | 21,147 |
https://leetcode.com/problems/html-entity-parser/discuss/575132/Python3-replace-strings-repeated | class Solution:
def entityParser(self, text: str) -> str:
mapping = {"&quot;" : '"',
"&apos;" : "'",
"&gt;" : ">",
"&lt;" : "<",
"&frasl;": "/",
"&amp;" : "&"}
for key, val in mapping.items():
text = text.replace(key, val)
return text | html-entity-parser | [Python3] replace strings repeated | ye15 | 0 | 81 | html entity parser | 1,410 | 0.52 | Medium | 21,148 |
https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/1648004/dynamic-programming-32ms-beats-99.44-in-Python | class Solution:
def numOfWays(self, n: int) -> int:
mod = 10 ** 9 + 7
two_color, three_color = 6, 6
for _ in range(n - 1):
two_color, three_color = (two_color * 3 + three_color * 2) % mod, (two_color * 2 + three_color * 2) % mod
return (two_color + three_color) % mod | number-of-ways-to-paint-n-3-grid | dynamic programming, 32ms, beats 99.44% in Python | kryuki | 2 | 205 | number of ways to paint n × 3 grid | 1,411 | 0.623 | Hard | 21,149 |
https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/1605095/Python-or-Recursion-or-Easy-to-understand | class Solution:
def numOfWays(self, n: int) -> int:
mod = 10 ** 9 + 7
@lru_cache(None)
def func(idx, c1, c2, c3):
if idx == n:
return 1
nc1, nc2, nc3 = 0, 0, 0
this = 0
for i in range(1, 4):
if i != c1:
nc1 = i
for j in range(1, 4):
if j != c2 and j != nc1:
nc2 = j
for k in range(1, 4):
if k != c3 and k != nc2:
nc3 = k
this += func(idx + 1, nc1, nc2, nc3)
return this % mod
return func(0, 0, 0, 0) | number-of-ways-to-paint-n-3-grid | Python | Recursion | Easy to understand | detective_dp | 1 | 151 | number of ways to paint n × 3 grid | 1,411 | 0.623 | Hard | 21,150 |
https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/575770/Python-O(n)-sol-by-dp.-90%2B-w-Hint | class Solution:
def numOfWays(self, n: int) -> int:
# Base case:
# for n = 1
# painting( n = 1 )
# = head_tail_equal( n = 1 ) + head_tail_differnt( n = 1 )
# = 6 + 6
# = 12
head_tail_equal = 6
head_tail_differnt = 6
if n == 1:
# Quick response for base case
return head_tail_equal + head_tail_differnt
# Recurrence for general case:
# for n >= 2
# painting( n ) = head_tail_equal( n ) + head_tail_differnt( n )
# where
# head_tail_equal( n ) = 3 * head_tail_equal(n-1) + 2 * head_tail_differnt(n-1)
# head_tail_differnt( n ) = 2 * head_tail_equal(n-1) + 2 * head_tail_differnt(n-1)
modulo = 10**9 + 7
for i in range(2, n+1):
next_ht_equal = ( 3 * head_tail_equal + 2 * head_tail_differnt ) % modulo
next_ht_different = ( 2 * head_tail_equal + 2 * head_tail_differnt ) % modulo
head_tail_equal, head_tail_differnt = next_ht_equal, next_ht_different
ways_to_paint = head_tail_equal + head_tail_differnt
return (ways_to_paint % modulo) | number-of-ways-to-paint-n-3-grid | Python O(n) sol by dp. 90%+ [w/ Hint] | brianchiang_tw | 1 | 228 | number of ways to paint n × 3 grid | 1,411 | 0.623 | Hard | 21,151 |
https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/575304/python3-five-ish-lines-linear-time-constant-memory | class Solution:
def numOfWays(self, n: int) -> int:
# class1: form 010. 6 of these to begin with. can have 5 after it: 3 class1 and 2 class2
# class2: form 012. 6 of these to begin with. can have 4 after it: 2 class2 and 2 class2
class1 = 6
class2 = 6
for _ in range(n - 1):
newclass1 = 3 * class1 + 2 * class2
newclass2 = 2 * class1 + 2 * class2
class1, class2 = newclass1 % (10 ** 9 + 7), newclass2 % (10 ** 9 + 7)
return (class1 + class2) % (10 ** 9 + 7) | number-of-ways-to-paint-n-3-grid | python3 five-ish lines, linear time, constant memory | hashtag_epic | 1 | 103 | number of ways to paint n × 3 grid | 1,411 | 0.623 | Hard | 21,152 |
https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/discuss/1970964/Python-simpler-dp | class Solution:
def numOfWays(self, n: int) -> int:
if n == 0:
return 0
atlevel = 12
five = 6
four = 6
depth = 1
while depth < n:
depth+=1
a1four = five*2
a1five = five*3
b1five = four*2
b1four = four*2
atlevel = a1four+a1five+b1four+b1five
five = a1five+b1five
four = a1four+b1four
return atlevel%(10**9+7) | number-of-ways-to-paint-n-3-grid | Python simpler dp | seun998 | 0 | 87 | number of ways to paint n × 3 grid | 1,411 | 0.623 | Hard | 21,153 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1431774/2-Lines-Easy-Python-Solution | class Solution:
def minStartValue(self, nums: List[int]) -> int:
for i in range(1, len(nums)): nums[i] = nums[i] + nums[i - 1]
return 1 if min(nums) >= 1 else abs(min(nums)) + 1 | minimum-value-to-get-positive-step-by-step-sum | 2 Lines Easy Python Solution | caffreyu | 5 | 305 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,154 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1195114/Python-Step-by-Step-Explanation-or-Very-Easy-and-Fast-or | class Solution:
def minStartValue(self, nums: List[int]) -> int:
#Take starting value as first elemet of the array
startValue = 0
#This will store the minimum step sum for all iterations
minimum_num = 0
#Iterate
for i in nums:
#StepSum
startValue += i
#Storing minimum possible step sum
minimum_num = min(minimum_num, startValue)
#Now if we add a number abs(minimum_num)+1, at each iteration stepsum will increase by this number and hence every stepsum is greater than 1
return 1-minimum_num | minimum-value-to-get-positive-step-by-step-sum | Python Step by Step Explanation | Very Easy & Fast | | iamkshitij77 | 3 | 177 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,155 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2315920/Python-Simplest-Solution-With-Explanation-or-Beg-to-adv-or-Prefix-sum | class Solution:
def minStartValue(self, nums: List[int]) -> int:
prefix_sum = 0 # taking this variable to store prefix sum
min_start_value = 1 # as stated in the question min startValue shouldn`t be less then 1.
for num in nums: # traversing through the provided list
prefix_sum += num # calculating prefix sum
min_start_value = max(min_start_value, 1-prefix_sum) # Taking max as value shouldnt be less them 1. Subtracting 1 to convert the negetives to positives.
return min_start_value | minimum-value-to-get-positive-step-by-step-sum | Python Simplest Solution With Explanation | Beg to adv | Prefix sum | rlakshay14 | 2 | 114 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,156 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1745189/Python-dollarolution(mem-use%3A-99-less) | class Solution:
def minStartValue(self, nums: List[int]) -> int:
startValue = 1 + (-1*nums[0])
while True:
s = startValue + nums[0]
for i in nums[1:]:
s += i
if s < 1:
startValue += 1
break
if s > 0:
break
if startValue < 1:
startValue = 1
return startValue | minimum-value-to-get-positive-step-by-step-sum | Python $olution(mem use: 99% less) | AakRay | 1 | 85 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,157 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1570717/Python-O(n)-solution | class Solution:
def minStartValue(self, nums: List[int]) -> int:
startValue = x = 1
for num in nums:
x += num
if x < 1:
startValue += 1 - x
x = 1
return startValue | minimum-value-to-get-positive-step-by-step-sum | Python O(n) solution | dereky4 | 1 | 107 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,158 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1568577/Using-accumulate-99-speed | class Solution:
def minStartValue(self, nums: List[int]) -> int:
m = min(accumulate(nums))
return -m + 1 if m < 0 else 1 | minimum-value-to-get-positive-step-by-step-sum | Using accumulate, 99% speed | EvgenySH | 1 | 51 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,159 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1395393/Simple-Python-Solution-(O(n)-using-prefix-sum) | class Solution:
def minStartValue(self, nums: List[int]) -> int:
min_ = nums[0]
for i in range(1, len(nums)):
nums[i] += nums[i-1]
if nums[i] < min_:
min_ = nums[i]
if min_ >= 1:
return 1
else:
return 1-min_ | minimum-value-to-get-positive-step-by-step-sum | Simple Python Solution (O(n), using prefix sum) | the_sky_high | 1 | 136 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,160 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2838021/Prefix-Sum-with-intuitive-cases | class Solution:
def minStartValue(self, nums: List[int]) -> int:
minLeftSum,leftSum=nums[0],nums[0]
for i in range(1,len(nums)):
leftSum=leftSum+nums[i]
minLeftSum=min(leftSum,minLeftSum)
if minLeftSum>=0:
return 1
else:
return abs(minLeftSum)+1 | minimum-value-to-get-positive-step-by-step-sum | Prefix Sum with intuitive cases | romilvt | 0 | 4 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,161 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2805230/Beats-100 | class Solution:
def minStartValue(self, nums: List[int]) -> int:
low_neg = 0
tot = 0
for num in nums:
tot += num
if tot < 0 and tot < low_neg:
low_neg = tot
return -low_neg + 1 | minimum-value-to-get-positive-step-by-step-sum | Beats 100% | Mauli_06 | 0 | 2 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,162 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2801567/minimum-value-to-get-positive-step-by-step-sum-solution-in-Python | class Solution:
def minStartValue(self, nums):
min_n=0
sum_n=0
for i in nums:
sum_n+=i
min_n=min(sum_n,min_n)
return 1-min_n | minimum-value-to-get-positive-step-by-step-sum | minimum value to get positive step by step sum solution in Python | vazimax | 0 | 2 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,163 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2761161/Python-3-Solution | class Solution:
def minStartValue(self, nums: List[int]) -> int:
theSmallest = inf
suma = 0
for x in nums:
suma += x
if suma < theSmallest:
theSmallest = suma
if theSmallest <= 0:
return abs(theSmallest) + 1
return 1 | minimum-value-to-get-positive-step-by-step-sum | Python 3 Solution | mati44 | 0 | 4 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,164 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2692162/Faster-than-96.3 | class Solution:
def minStartValue(self, nums: List[int]) -> int:
current = []
sum = 0
for num in nums:
sum += num
current.append(sum)
return max(1, - min(current) + 1) | minimum-value-to-get-positive-step-by-step-sum | Faster than 96.3% | Jiaming-Hu | 0 | 6 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,165 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2691653/Python-or-Prefix-Sum | class Solution:
def minStartValue(self, nums: List[int]) -> int:
prefix=[nums[0]]
for i in nums[1:]:
prefix.append(i+prefix[-1])
# print(prefix)
return 1-min(prefix) if min(prefix)<1 else 1 | minimum-value-to-get-positive-step-by-step-sum | Python | Prefix Sum | Prithiviraj1927 | 0 | 7 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,166 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2634921/Python3-or-Solved-Using-Prefix-Sum-Array-O(N)-TIME-AND-SPACE! | class Solution:
#Let n = len(nums)!
#Time-Complexity: O(n + n ) -> O(n)
#Space-Complexity: O(n)
def minStartValue(self, nums: List[int]) -> int:
#Approach: At every step by step sum, you are basically asking the question if whether
#current value x + prefix sum up to current index is at least 1! Thus, it would be nice
#to initialize prefix sum array beforehand!
#Also, the smallest x could be is lower bounded by the smallest prefix sum value, since it
#has to take it to value 1, which is positive!
#Edge Case: if all prefix sum are nonnegative, we can simply default to smallest value of x
#being 1!
prefix_sum = []
prefix_sum.append(nums[0])
for i in range(1, len(nums)):
prefix_sum.append(prefix_sum[len(prefix_sum)-1] + nums[i])
#find the smallest prefix sum out of all!
smallest_sum = min(prefix_sum)
#check for edge case!
if(smallest_sum >= 0):
return 1
else:
return abs(1 - smallest_sum) | minimum-value-to-get-positive-step-by-step-sum | Python3 | Solved Using Prefix Sum Array O(N) TIME AND SPACE! | JOON1234 | 0 | 7 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,167 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2458045/Python3-Solution-with-using-prefix-sum | class Solution:
def minStartValue(self, nums: List[int]) -> int:
cur_sum = 0
res = 1
for num in nums:
cur_sum += num
if cur_sum > 0:
continue
res = max(res, 1 - cur_sum)
return res | minimum-value-to-get-positive-step-by-step-sum | [Python3] Solution with using prefix sum | maosipov11 | 0 | 36 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,168 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/2367620/Easy-understandable-python-solution | class Solution:
def minStartValue(self, nums: List[int]) -> int:
i=0
pos=0
l=len(nums)
ans=0
while i<l:
if pos+nums[i] < 1:
ans+=abs(pos+nums[i])+1
pos=1
else:
pos+=nums[i]
i+=1
return max(ans,1) | minimum-value-to-get-positive-step-by-step-sum | Easy understandable python solution | sunakshi132 | 0 | 59 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,169 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1968767/easy-python-O(n)-prefix-sum-code-faster-than-99.41 | class Solution:
def minStartValue(self, nums: List[int]) -> int:
for i in range(1,len(nums)):
nums[i] = nums[i]+nums[i-1]
x = min(nums)
if x <= 0:
return 1-x
else:
return 1 | minimum-value-to-get-positive-step-by-step-sum | easy python O(n) prefix sum code faster than 99.41% | dakash682 | 0 | 79 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,170 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1902993/Python-Clean-and-Concise!-One-Liners-(x3)! | class Solution:
def minStartValue(self, nums):
return 1-min(filter(lambda x:x<0,accumulate(nums)), default=0) | minimum-value-to-get-positive-step-by-step-sum | Python - Clean and Concise! One-Liners (x3)! | domthedeveloper | 0 | 56 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,171 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1902993/Python-Clean-and-Concise!-One-Liners-(x3)! | class Solution:
def minStartValue(self, nums):
return max(1, 1-min(accumulate(nums))) | minimum-value-to-get-positive-step-by-step-sum | Python - Clean and Concise! One-Liners (x3)! | domthedeveloper | 0 | 56 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,172 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1902993/Python-Clean-and-Concise!-One-Liners-(x3)! | class Solution:
def minStartValue(self, nums):
return 1-min(accumulate(nums, initial=0)) | minimum-value-to-get-positive-step-by-step-sum | Python - Clean and Concise! One-Liners (x3)! | domthedeveloper | 0 | 56 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,173 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1833577/2-Lines-Python-Solution-oror-97-Faster-oror-Memory-less-than-80 | class Solution:
def minStartValue(self, nums: List[int]) -> int:
i=0 ; mn=min([i:=i+n for n in nums])
return [1-mn if mn<1 else 1][0] | minimum-value-to-get-positive-step-by-step-sum | 2-Lines Python Solution || 97% Faster || Memory less than 80% | Taha-C | 0 | 132 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,174 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1833577/2-Lines-Python-Solution-oror-97-Faster-oror-Memory-less-than-80 | class Solution:
def minStartValue(self, nums: List[int]) -> int:
return [1-min(list(accumulate(nums))) if min(list(accumulate(nums)))<1 else 1][0] | minimum-value-to-get-positive-step-by-step-sum | 2-Lines Python Solution || 97% Faster || Memory less than 80% | Taha-C | 0 | 132 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,175 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1812382/Python-Simple-Solution-or-Almost-the-same-idea-with-174.-Dungeon-Game-(hard) | class Solution:
def minStartValue(self, nums: List[int]) -> int:
last = 1
for num in nums[::-1]:
last = max(last - num, 1)
return last | minimum-value-to-get-positive-step-by-step-sum | [Python] Simple Solution | Almost the same idea with 174. Dungeon Game (hard) | yasufumy | 0 | 72 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,176 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1698534/WEEB-DOES-PYTHON-PREFIX-SUM | class Solution:
def minStartValue(self, nums: List[int]) -> int:
minVal = float("inf")
for i in range(len(nums)): # we get sum every index and get the minimum
if sum(nums[:i+1]) < minVal:
minVal = sum(nums[:i+1])
# if a particular index sum is less than or equal to zero we need to add 1 to the absolute value of the minVal
# to make sure the prefix sum of that index can be 1 (as stated in the question, we need at least 1),
# otherwise we return 1 since all index sum is already >=1
return abs(minVal) + 1 if minVal <= 0 else 1 | minimum-value-to-get-positive-step-by-step-sum | WEEB DOES PYTHON PREFIX SUM | Skywalker5423 | 0 | 48 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,177 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1657279/easy-to-understand | class Solution:
def minStartValue(self, nums: List[int]) -> int:
def getTempsum(startValue):
temp, temp_arr=0, []
for i in nums:
temp = startValue+i
startValue= temp
temp_arr.append(temp)
return min(temp_arr)
startValue= 1
while True:
res= getTempsum(startValue)
if res>1: return 1
elif res==1: return startValue
startValue+=1 | minimum-value-to-get-positive-step-by-step-sum | easy to understand | sc618445 | 0 | 44 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,178 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1596561/Prefix-Sum-easy-to-understand | class Solution(object):
def minStartValue(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(1, len(nums)):
nums[i] = nums[i-1] + nums[i]
i = 0
while i < len(nums):
if nums[i] >= 1:
i += 1
else:
return abs(min(nums)) + 1
return 1 | minimum-value-to-get-positive-step-by-step-sum | Prefix Sum easy to understand | junaidoxford59 | 0 | 32 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,179 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1585170/Python-Easy-Solution-or-Simple-Approach | class Solution:
def minStartValue(self, nums: List[int]) -> int:
ans = 0
sum = 0
for i in range(len(nums)):
sum += nums[i]
ans = min(ans, sum)
return -ans+1 | minimum-value-to-get-positive-step-by-step-sum | Python Easy Solution | Simple Approach | leet_satyam | 0 | 73 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,180 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1572407/Python-or-Prefix-sum-or-Simple-Approach | class Solution:
def minStartValue(self, nums: List[int]) -> int:
l=[nums[0]]
for i in range(1,len(nums)):
l.append(l[i-1]+nums[i])
mini=min(l)
#print(l)
if mini>0:
return 1
else:
return abs(mini-1) | minimum-value-to-get-positive-step-by-step-sum | Python | Prefix-sum | Simple Approach | meghanakolluri | 0 | 23 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,181 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1571564/Python-O(n)-solution-with-comments | class Solution:
def minStartValue(self, nums: List[int]) -> int:
most_neg = 0
s = 0
n = len(nums)
for i in range(n):
s += nums[i]
most_neg = min(most_neg, s) #keep track of the most negative sum within the traversal
return -most_neg+1 # min positive is 1 if the sum never becomes negative else the start val will be one more than the most negative sum | minimum-value-to-get-positive-step-by-step-sum | Python O(n) solution with comments | abkc1221 | 0 | 33 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,182 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1571310/o(n)-Python-Solution-using-Prefix-sum | class Solution:
def minStartValue(self, nums: List[int]) -> int:
l = [nums[0]]
for i in range (1, len(nums)):
l.append(l[-1] + nums[i])
m = min(l)
if m < 0 :
m -= 1
m = -m
else:
m = 1
return m | minimum-value-to-get-positive-step-by-step-sum | o(n) Python Solution using Prefix sum | Utkarsh_Hans_Verma | 0 | 30 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,183 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1571047/python3-ez-solution | class Solution:
def minStartValue(self, nums: List[int]) -> int:
min_start = 1
curr_val = 0
for num in nums:
curr_val += num
# assuming curr_val is negative. if curr_val is positive it should stay with min_start
min_start = max(-curr_val+1,min_start)
return min_start | minimum-value-to-get-positive-step-by-step-sum | python3 ez solution | yingziqing123 | 0 | 19 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,184 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1404863/Python3-Faster-Than-96.45-Memory-Less-Than-93.06 | class Solution:
def minStartValue(self, nums: List[int]) -> int:
needed, cum = 1, 0
for i in nums:
cum1 = cum
cum += i
if cum1 > cum and cum < 0:
needed = max(abs(cum) + 1, needed)
return needed | minimum-value-to-get-positive-step-by-step-sum | Python3 Faster Than 96.45%, Memory Less Than 93.06% | Hejita | 0 | 68 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,185 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1371031/Python3-solution-explained | class Solution:
def minStartValue(self, nums: List[int]) -> int:
my_min = 0
firstnegative = 0
if nums[0] < 1:
firstnegative = abs(nums[0])+1
for i in range(1,len(nums)):
if nums[i-1] + nums[i] < 1:
if my_min > nums[i-1]+nums[i]:
my_min = nums[i-1]+nums[i]
nums[i] = nums[i] + nums[i-1]
if firstnegative > abs(my_min)+1:
return firstnegative
return abs(my_min)+1
# you have to iterate through the list of numbers and update the last element with the sum of the previous one
# you need to get the smallest consecutive negative sum, ex: nums[0] = -3 | nums[1] = 2 => sum = -1 nums[1] = -1 | nums[2] = -3 => sum = -4 nums[2] = -4 and so on
# there's a case when the first element is < 1 so you have to check if that element is smaller than the smallest sum
# if that's the case then you choose that element and return the absolute value of the element + 1 (to cover it)
# you need to add +1 in every case anyways.. you figure out that thing from description
# please, upvote if you like it | minimum-value-to-get-positive-step-by-step-sum | Python3 solution explained | FlorinnC1 | 0 | 76 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,186 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1133385/Python3-Simple-solution-O(n)-32-ms | class Solution:
def minStartValue(self, nums: List[int]) -> int:
out = last = 1
for i in nums:
last = last + i
if last < 1:
out = out + 1-last
last = 1
return out | minimum-value-to-get-positive-step-by-step-sum | Python3, Simple solution, O(n), 32 ms | naiem_ece | 0 | 93 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,187 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1121219/Simple-Python-Faster-99-Memory-and-Speed-No-Fancy-Stuff | class Solution:
def minStartValue(self, nums: List[int]) -> int:
start = min_num = 0
for num in nums:
start += num
min_num = min(start, min_num)
return 1 - min_num | minimum-value-to-get-positive-step-by-step-sum | Simple Python Faster 99% Memory & Speed - No Fancy Stuff | JuanRodriguez | 0 | 97 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,188 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/1064896/Python3-simple-solution | class Solution:
def minStartValue(self, nums: List[int]) -> int:
min = 0
for i in range(len(nums)+1):
if sum(nums[:i]) < min :
min = sum(nums[:i])
return abs(min)+1 | minimum-value-to-get-positive-step-by-step-sum | Python3 simple solution | EklavyaJoshi | 0 | 82 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,189 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/859686/Python3-prefix-sum | class Solution:
def minStartValue(self, nums: List[int]) -> int:
ans = prefix = 0
for x in nums:
prefix += x
ans = min(ans, prefix)
return 1 - ans | minimum-value-to-get-positive-step-by-step-sum | [Python3] prefix sum | ye15 | 0 | 114 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,190 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/825001/python-soution-faster-than-95 | class Solution(object):
def minStartValue(self, nums):
li=[]
s=0
for i in range(len(nums)):
s += nums[i]
li.append(s)
if min(li) <= 0:
return abs(min(li)) + 1
else:
return 1 | minimum-value-to-get-positive-step-by-step-sum | python soution, faster than 95% | Namangarg98 | 0 | 93 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,191 |
https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/discuss/587494/Python-O(n)-by-prefix-sum-w-Comment | class Solution:
def minStartValue(self, nums: List[int]) -> int:
prefix_sum = 0
negative_most_offset = float('inf')
# linear scan, and update nagative most offset by prefix sum
for i in range(0, len(nums)):
prefix_sum += nums[i]
negative_most_offset = min( prefix_sum, negative_most_offset)
# compute the minimum value to get positive
threshold = min( negative_most_offset, 0 )
# abs to flip the sign, +1 to make it larger than zero
return abs(threshold)+1 | minimum-value-to-get-positive-step-by-step-sum | Python O(n) by prefix sum [w/ Comment] | brianchiang_tw | 0 | 194 | minimum value to get positive step by step sum | 1,413 | 0.68 | Easy | 21,192 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/1341772/Python3-easy-solution-using-recursion | class Solution:
def findMinFibonacciNumbers(self, n: int) -> int:
def check(z):
key = [1,1]
while key[-1] + key[-2] <= z:
key.append(key[-1]+key[-2])
print(key,z)
if z in key:
return 1
return 1 + check(z-key[-1])
return check(n) | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | Python3 easy solution using recursion | EklavyaJoshi | 1 | 333 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,193 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/2778172/Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K-leetcode-python | class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
l=[1,1]
a=0
while l[len(l)-1] <= k:
a += l[len(l)-1]+l[len(l)-2]
if a > k:
break
l.append(a)
a=0
b = l[::-1]
ans = 0
for i in b:
if i<=k:
ans = ans + 1
k -= i
if k == 0:
break
return ans | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | Find the Minimum Number of Fibonacci Numbers Whose Sum Is K leetcode python | user4548N | 0 | 4 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,194 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/2761524/Python3-using-binary-search-in-finbonacci-array | class Solution:
def __init__(self):
self.fib = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025
, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578
, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155
, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903]
def findMinFibonacciNumbers(self, k: int) -> int:
# print(k)
if k == 0:
return 0
idx = bisect.bisect_right(self.fib, k)
if self.fib[idx] == k:
return 1
return self.findMinFibonacciNumbers(k - self.fib[idx - 1]) + 1 | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | [Python3] using binary search in finbonacci array | huangweijing | 0 | 2 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,195 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/2345061/Python-easy-to-read-and-understand-or-recursion | class Solution:
def solve(self, k):
if k < 2:
return k
n1, n2 = 1, 1
while n2 <= k:
n2, n1 = n1+n2, n2
return self.solve(k - n1) + 1
def findMinFibonacciNumbers(self, k: int) -> int:
return self.solve(k) | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | Python easy to read and understand | recursion | sanial2001 | 0 | 53 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,196 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/1677224/1-beer-and-2-teas-later-faster-than-70-but-vv-easy-to-understand | class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
coins=[]
coins.append(1)
coins.append(1)
while(coins[-1]<=k):
coins.append(coins[-1]+coins[-2])
coins=coins[::-1]
rem=k
total_coins=0
for i in range(0,len(coins)):
if(coins[i]>rem):
continue
elif(rem==0):
break
else:
total_coins+=int(rem/coins[i])
rem=rem%coins[i]
return(total_coins) | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | 1 beer and 2 teas later, faster than 70% but vv easy to understand😅 | naren_nadig | 0 | 62 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,197 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/1500797/python-O((logk)2)-time-O(logk)-space-solution-with-binary-search | class Solution(object):
def findMinFibonacciNumbers(self, k):
"""
:type k: int
:rtype: int
"""
fib = [0, 1]
a, b = 1, 1
while a + b <= k:
c = a + b
fib.append(c)
a = b
b = c
n = len(fib)
def index(x):
if x == 0:
return 0
if x >= fib[n-1]:
return n-1
# find maximum x such that fib[x] <= k
left, right = 0, n-1
while left < right:
mid = (left + right) // 2
if fib[mid] > x:
right = mid
else:
left = mid + 1
return left-1
res = 0
while k > 0:
idx = index(k)
res += 1
k -= fib[idx]
return res | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | python O((logk)^2) time, O(logk) space solution with binary search | byuns9334 | 0 | 101 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,198 |
https://leetcode.com/problems/find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k/discuss/1350543/Python-Iterative-Solution | class Solution:
def findMinFibonacciNumbers(self, k: int) -> int:
a,b = 0,1
while(b <= k):
a,b = b, a+b
cnt = 0
while(k > 0):
while(k >= a):
k -= a
cnt += 1
a,b = b-a,a
return cnt | find-the-minimum-number-of-fibonacci-numbers-whose-sum-is-k | Python Iterative Solution | liamc628 | 0 | 239 | find the minimum number of fibonacci numbers whose sum is k | 1,414 | 0.654 | Medium | 21,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.