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/fizz-buzz/discuss/1072221/Python3-simple-solution-using-%22dictionary%22-and-%22if-else%22 | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1,n+1):
if i % 3 == 0 and i % 5 == 0:
res.append("FizzBuzz")
elif i % 3 == 0:
res.append("Fizz")
elif i % 5 == 0:
res.append("Buzz")
else:
res.append(str(i))
return res | fizz-buzz | Python3 simple solution using "dictionary" and "if-else" | EklavyaJoshi | 1 | 97 | fizz buzz | 412 | 0.69 | Easy | 7,200 |
https://leetcode.com/problems/fizz-buzz/discuss/1068691/Python3-%3A-Runtime%3A-28-ms-faster-than-99.86 | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
arr = []
for i in range(1,n+1) :
x = ""
if i % 3 == 0 : x += "Fizz"
if i % 5 == 0 : x += "Buzz"
if x == "" : x = str(i)
arr += [x]
return arr | fizz-buzz | Python3 : Runtime: 28 ms, faster than 99.86% | sp27 | 1 | 193 | fizz buzz | 412 | 0.69 | Easy | 7,201 |
https://leetcode.com/problems/fizz-buzz/discuss/292526/Python3-faster-99.57-Mless-79.93 | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
'''
Runtime: 48 ms, faster than 99.57% of Python3 online submissions for Fizz Buzz.
Memory Usage: 14 MB, less than 79.93% of Python3 online submissions for Fizz Buzz.
'''
a=[]
for i in range(1,n+1):
if i%3 == 0:
if i % 5 ==0:
a.append("FizzBuzz")
else:
a.append("Fizz")
else:
if(i % 5==0):
a.append("Buzz")
else:
a.append(str(i))
return a | fizz-buzz | Python3 faster 99.57% M< 79.93% | Triple-L | 1 | 531 | fizz buzz | 412 | 0.69 | Easy | 7,202 |
https://leetcode.com/problems/fizz-buzz/discuss/2847981/Easy-Python-Solution-for-Fizz-Buzz | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer=[]
for i in range(1,n+1):
if i%3==0 and i%5 == 0:
answer.append("FizzBuzz")
elif i%3==0:
answer.append("Fizz")
elif i%5==0:
answer.append("Buzz")
else:
answer.append(str(i))
print(answer)
return answer
sol=Solution()
sol.fizzBuzz(3)
sol.fizzBuzz(5)
sol.fizzBuzz(15)
sol.fizzBuzz(2) | fizz-buzz | Easy Python Solution for Fizz Buzz | dassdipanwita | 0 | 1 | fizz buzz | 412 | 0.69 | Easy | 7,203 |
https://leetcode.com/problems/fizz-buzz/discuss/2827918/FizzBuzz-The-Best | class Solution:
def fizzBuzz(self, n: int) -> list[str]:
return [self.getFizzBuzz(i) for i in range(1, n+1)]
def getFizzBuzz(self, i: int) -> str:
return ''.join([self.getIndicator(i, p, s) for p, s in ((3, "Fizz"), (5, "Buzz"))]) or str(i)
def getIndicator(self, i: int, p: int, s: str) -> str:
return s if self.isDivisible(i, p) else ""
def isDivisible(self, i: int, p: int) -> bool:
return i % p == 0 | fizz-buzz | FizzBuzz, The Best | Triquetra | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,204 |
https://leetcode.com/problems/fizz-buzz/discuss/2827918/FizzBuzz-The-Best | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
fizz_buzz = [None] * n
for divisor, generator in (15, lambda _: "FizzBuzz"), (5, lambda _: "Buzz"), (3, lambda _: "Fizz"), (1, lambda index: str(index+1)):
for index in range(divisor-1, n, divisor):
if not fizz_buzz[index]:
fizz_buzz[index] = generator(index)
return fizz_buzz | fizz-buzz | FizzBuzz, The Best | Triquetra | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,205 |
https://leetcode.com/problems/fizz-buzz/discuss/2827208/Simple-obvious-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1, n+1):
if i % (3 * 5 ) == 0:
res.append("FizzBuzz")
elif i % 3 == 0:
res.append("Fizz")
elif i % 5 == 0:
res.append("Buzz")
else:
res.append(str(i))
return res | fizz-buzz | Simple obvious solution | rustynail | 0 | 1 | fizz buzz | 412 | 0.69 | Easy | 7,206 |
https://leetcode.com/problems/fizz-buzz/discuss/2825395/Easy-Python-Solution-for-beginners | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ans = []
for i in range(1, n+1):
if i % 3==0 and i%5==0:
ans.append("FizzBuzz")
elif i % 3==0:
ans.append("Fizz")
elif i % 5==0:
ans.append("Buzz")
else:
ans.append(str(i))
return ans | fizz-buzz | Easy Python Solution for beginners | aayushhh_13 | 0 | 1 | fizz buzz | 412 | 0.69 | Easy | 7,207 |
https://leetcode.com/problems/fizz-buzz/discuss/2818347/simple-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
output=[]
i=0
while(i<n):
i=i+1
if(i%3==0 and i%5==0):
#print("can be divisible by both")
output.append("FizzBuzz")
elif(i%3==0):
#print("divisble by 3")
output.append("Fizz")
elif(i%5==0):
#print("divisible by5")
output.append("Buzz")
else:
#print('none')
output.append(str(i))
return output | fizz-buzz | simple solution | saisupriyavaru | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,208 |
https://leetcode.com/problems/fizz-buzz/discuss/2818300/The-common | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = []
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
answer.append("FizzBuzz")
elif i % 3 == 0:
answer.append("Fizz")
elif i % 5 == 0:
answer.append("Buzz")
else:
answer.append(f"{i}")
return answer | fizz-buzz | The common | pkozhem | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,209 |
https://leetcode.com/problems/fizz-buzz/discuss/2807670/Python-Solution-for-Fizz-Buzz | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
return ["Fizz"*(i % 3 == 0) + "Buzz"*(i % 5 == 0) or f"{i}" for i in range(1,n+1)] | fizz-buzz | Python Solution for Fizz Buzz | Bassel_Alf | 0 | 5 | fizz buzz | 412 | 0.69 | Easy | 7,210 |
https://leetcode.com/problems/fizz-buzz/discuss/2807670/Python-Solution-for-Fizz-Buzz | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = []
for i in range (1,n+1):
divisibleBy3 = bool(i % 3 == 0)
divisibleBy5 = bool(i % 5 == 0)
current_str = ""
if divisibleBy3:
current_str += "Fizz"
if divisibleBy5:
current_str += "Buzz"
if not current_str:
current_str += str(i)
answer.append(current_str)
return answer | fizz-buzz | Python Solution for Fizz Buzz | Bassel_Alf | 0 | 5 | fizz buzz | 412 | 0.69 | Easy | 7,211 |
https://leetcode.com/problems/fizz-buzz/discuss/2801089/Simple-Python3-Solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
result = []
for i in range(1, n+1):
if i %3 == 0 and i % 5 == 0:
result.append("FizzBuzz")
elif i%3 == 0:
result.append("Fizz")
elif i%5 == 0:
result.append("Buzz")
else:
result.append(str(i))
return result | fizz-buzz | Simple Python3 Solution | vivekrajyaguru | 0 | 2 | fizz buzz | 412 | 0.69 | Easy | 7,212 |
https://leetcode.com/problems/fizz-buzz/discuss/2797720/A-really-complicated-solution-for-a-simple-problem-(LOL) | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = list(range(1,n+1))
for idx, i in enumerate(map(self.remainder, list(zip(answer, [(3,5)]*n)))):
if all(i): answer[idx] = str(idx+1)
else:
if any(i)==True: answer[idx] = "Fizz" if not i[0] else "Buzz"; continue
if all(i)==False: answer[idx] = "FizzBuzz"
return answer
def remainder(self, nums):
n, [t, f] = nums
return n%t, n%f | fizz-buzz | A really complicated solution for a simple problem (LOL) | dummynode404 | 0 | 4 | fizz buzz | 412 | 0.69 | Easy | 7,213 |
https://leetcode.com/problems/fizz-buzz/discuss/2779422/Slower-approach-maybe-because-initializing-list-of-particular-size-with-None | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = [None]*n
for i in range(1,n+1):
res_str = ""
if i%3 == 0:
res_str += "Fizz"
if i%5 == 0:
res_str += "Buzz"
if res_str == "":
res_str = str(i)
res[i-1] = res_str
return res | fizz-buzz | Slower approach maybe because initializing list of particular size with None? | sdsahil12 | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,214 |
https://leetcode.com/problems/fizz-buzz/discuss/2767661/Without-If-Else-91-faster-than-others | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
li = [str(i) for i in range(n + 1)]
for i in range(0, n + 1, 3 ):
li[i] = "Fizz"
for i in range(0, n + 1, 5):
li[i] = "Buzz"
for i in range(0, n + 1, 15):
li[i] = "FizzBuzz"
return li[1:] | fizz-buzz | Without If Else, 91% faster than others | prashantiwari | 0 | 12 | fizz buzz | 412 | 0.69 | Easy | 7,215 |
https://leetcode.com/problems/fizz-buzz/discuss/2760711/Python-oror-Simple | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
answer = []
for i in range(1,n+1):
if i%3 == 0 and i%5 == 0:
answer.append('FizzBuzz')
continue
if i%3 == 0:
answer.append('Fizz')
continue
if i%5 == 0:
answer.append('Buzz')
continue
answer.append(str(i))
return answer | fizz-buzz | Python || Simple | morpheusdurden | 0 | 7 | fizz buzz | 412 | 0.69 | Easy | 7,216 |
https://leetcode.com/problems/fizz-buzz/discuss/2760667/Python-Three-ways-to-solve-the-problem-along-with-their-runtime-and-Memory-usage | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
lis=[]
#First way to solve this question
for val in range(1,n+1):
#print(val%3, val%5)
if (val%3==0 and val%5==0):
lis.append("FizzBuzz")
elif val%3==0:
lis.append("Fizz")
elif val%5==0:
lis.append("Buzz")
else:
lis.append(str(val))
return lis
#Runtime: 88 ms, faster than 42.22% of Python3 online submissions for Fizz Buzz.
#Memory Usage: 14.9 MB, less than 85.86% of Python3 online submissions for Fizz Buzz
#Second way to solve this question
for val in range(1,n+1):
lis.append("FizzBuzz") if (val%3==0 and val%5==0) else (lis.append("Fizz") if val%3==0 else (lis.append("Buzz") if val%5==0 else lis.append(str(val)) ) )
return lis;
#Runtime: 87 ms, faster than 44.84% of Python3 online submissions for Fizz Buzz.
#Memory Usage: 15.1 MB, less than 43.11% of Python3 online submissions for Fizz Buzz.
#Third way to solve this solution
# Learned this way to solve the question from https://leetcode.com/problems/fizz-buzz/discuss/2704645/Python-One-liner
#Runtime: 96 ms, faster than 22.65% of Python3 online submissions for Fizz Buzz.
#Memory Usage: 15.2 MB, less than 17.01% of Python3 online submissions for Fizz Buzz.
return [ "Fizz"*(val%3==0)+"Buzz"*(val%5==0) or f"{val}" for val in range(1,n+1)] | fizz-buzz | Python Three ways to solve the problem along with their runtime and Memory usage | khubaib | 0 | 2 | fizz buzz | 412 | 0.69 | Easy | 7,217 |
https://leetcode.com/problems/fizz-buzz/discuss/2749553/easiest-solution-in-python | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
l=[]
for i in range(1,n+1):
if i%3 ==0 and i%5==0:
l.append("FizzBuzz")
elif i%3==0:
l.append("Fizz")
elif i%5==0:
l.append("Buzz")
else:
l.append(str(i))
return l | fizz-buzz | easiest solution in python | sindhu_300 | 0 | 6 | fizz buzz | 412 | 0.69 | Easy | 7,218 |
https://leetcode.com/problems/fizz-buzz/discuss/2739031/python-fast-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
counter = 1
while counter <= n:
if counter%3==0 and counter%5==0:
res.append("FizzBuzz")
elif counter%3 == 0:
res.append("Fizz")
elif counter%5==0:
res.append("Buzz")
else:
res.append(str(counter))
counter += 1
return res | fizz-buzz | python fast solution | muge_zhang | 0 | 6 | fizz buzz | 412 | 0.69 | Easy | 7,219 |
https://leetcode.com/problems/fizz-buzz/discuss/2736494/Python-Easy-code | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
list_=[str(i) for i in range(1, n+1)]
for i in list_:
i=int(i)
if i%3==0 and i%5==0:
i=str(i)
index=list_.index(i)
list_[index]="FizzBuzz"
elif i%3==0:
i=str(i)
index=list_.index(i)
list_[index]="Fizz"
elif i%5==0:
i=str(i)
index=list_.index(i)
list_[index]="Buzz"
return list_ | fizz-buzz | Python Easy code | indurisaieshwar225 | 0 | 2 | fizz buzz | 412 | 0.69 | Easy | 7,220 |
https://leetcode.com/problems/fizz-buzz/discuss/2720355/Python3-List-Comprehension | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
return [
"FizzBuzz" if not i % 15 else
"Fizz" if not i % 3 else
"Buzz" if not i % 5 else
str(i) for i in range(1, n + 1)
] | fizz-buzz | Python3 List Comprehension | jbeBan | 0 | 5 | fizz buzz | 412 | 0.69 | Easy | 7,221 |
https://leetcode.com/problems/fizz-buzz/discuss/2716785/Beginner-Friendly-and-Easy-O(N)-Solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
res.append("FizzBuzz")
elif i % 3 == 0:
res.append("Fizz")
elif i % 5 == 0:
res.append("Buzz")
else:
res.append(str(i))
return res | fizz-buzz | Beginner Friendly and Easy O(N) Solution | user6770yv | 0 | 2 | fizz buzz | 412 | 0.69 | Easy | 7,222 |
https://leetcode.com/problems/fizz-buzz/discuss/2716056/String-concatentaion-with-generic-and-extensible-solution-in-Python3.-TC%3A-O(n)-SC%3A-O(1). | class Solution:
modulo_replacements = {
3: 'Fizz',
5: 'Buzz',
}
def elementString(self, i: int) -> str:
result = ''
for k in self.modulo_replacements.keys():
if i % k == 0:
result += self.modulo_replacements[k]
if result == '':
result += str(i)
return result
def fizzBuzz(self, n: int) -> List[str]:
return [self.elementString(i) for i in range(1,n+1)] | fizz-buzz | String concatentaion with generic and extensible solution in Python3. TC: O(n), SC: O(1). | mwalle | 0 | 2 | fizz buzz | 412 | 0.69 | Easy | 7,223 |
https://leetcode.com/problems/fizz-buzz/discuss/2716025/FizzBuzz-with-a-twist | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
newArray = []
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
newArray.append("FizzBuzz")
elif i % 3 == 0:
newArray.append("Fizz")
elif i % 5 == 0:
newArray.append("Buzz")
else:
newArray.append(str(i))
return newArray | fizz-buzz | FizzBuzz with a twist | EverydayScriptkiddie | 0 | 4 | fizz buzz | 412 | 0.69 | Easy | 7,224 |
https://leetcode.com/problems/fizz-buzz/discuss/2715246/Simple-and-Easy-to-Understand-Python3-Code | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
lst=["Fizz","Buzz","FizzBuzz"]
output=[]
if n==0:
return output
else:
for i in range(n):
if (i+1)%3==0 and (i+1)%5==0:
output.append(lst[2])
elif (i+1)%3==0 and (i+1)%5!=0:
output.append(lst[0])
elif (i+1)%3!=0 and (i+1)%5==0:
output.append(lst[1])
else:
output.append(str(i+1))
return output | fizz-buzz | Simple and Easy to Understand Python3 Code | sowmika_chaluvadi | 0 | 4 | fizz buzz | 412 | 0.69 | Easy | 7,225 |
https://leetcode.com/problems/fizz-buzz/discuss/2710473/Python%3A-Simple-code-from-beginner-(maybe-not-productive)-but-easy-to-understand-used-enumerate-%2B-map | class Solution:
# fizz for 3 and buzz for 5
# we need to create a list of int items
# try to do just a list without fizz and bazz
# complited
# need change 3 to Fizz and 5 to Bazz etc
# then we need to switch list to new type string
# complited
def fizzBuzz(self, m: int) -> List[str]:
a = [i + 1 for i in range(m)]
for i, n in enumerate(a):
if n % 3 == 0:
a[i] = "Fizz"
if n % 5 == 0:
a[i] = "Buzz"
if (n % 3 == 0 and n % 5 == 0):
a[i] = "FizzBuzz"
answer = list(map(str, a))
return answer | fizz-buzz | Python: Simple code from beginner (maybe not productive) but easy to understand used enumerate + map | moodkeeper | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,226 |
https://leetcode.com/problems/fizz-buzz/discuss/2703787/python-dictionary-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
result={}
for num in range(1,n+1):
result[num]=num
for i in result:
if result[i]%3==0 and result[i]%5==0:
result[i]='FizzBuzz'
elif result[i]%3==0:
result[i]='Fizz'
elif result[i]%5==0:
result[i]='Buzz'
else:
result[i]=str(i)
return result.values() | fizz-buzz | python dictionary solution | sahityasetu1996 | 0 | 1 | fizz buzz | 412 | 0.69 | Easy | 7,227 |
https://leetcode.com/problems/fizz-buzz/discuss/2697884/Simple-Python-Solution. | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ans = []
i = 1
while(i<=n):
if i%3 == 0:
if i%5 == 0:
ans.append("FizzBuzz")
else:
ans.append("Fizz")
elif i%5 == 0:
ans.append("Buzz")
else:
ans.append(str(i))
i += 1
return ans | fizz-buzz | Simple Python Solution. | imkprakash | 0 | 4 | fizz buzz | 412 | 0.69 | Easy | 7,228 |
https://leetcode.com/problems/fizz-buzz/discuss/2666602/Solving-FizzBuzz-using-while-loop. | class Solution(object):
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
i = 1
newlist = []
while i <= n:
if i%3 == 0 and i%5 == 0:
newlist.append("FizzBuzz")
elif i%3 == 0:
newlist.append("Fizz")
elif i%5 == 0:
newlist.append("Buzz")
else:
newlist.append(str(i))
i+=1
return newlist | fizz-buzz | Solving FizzBuzz using while loop. | wjdghks | 0 | 6 | fizz buzz | 412 | 0.69 | Easy | 7,229 |
https://leetcode.com/problems/fizz-buzz/discuss/2660452/Python-Easy-FizzBuzz-Solution-or-93.03-Time-or-85.41-Space | class Solution:
def fizzBuzzElement(self, i):
if i % 3 == 0:
if i % 5 == 0:
return 'FizzBuzz'
else:
return 'Fizz'
if i % 5 == 0:
return 'Buzz'
return str(i)
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1, n+1):
i_res = self.fizzBuzzElement(i)
res.append(i_res)
return res | fizz-buzz | Python Easy FizzBuzz Solution | 93.03% Time | 85.41% Space | ivan_shelonik | 0 | 36 | fizz buzz | 412 | 0.69 | Easy | 7,230 |
https://leetcode.com/problems/fizz-buzz/discuss/2657640/Fizz-Buzz-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
result = []
for i in range(1,n+1):
if i%3 ==0 and i % 5 == 0:
result.append("FizzBuzz")
elif(i%3 == 0):
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(str(i))
return result | fizz-buzz | Fizz Buzz solution | mepujan10 | 0 | 1 | fizz buzz | 412 | 0.69 | Easy | 7,231 |
https://leetcode.com/problems/fizz-buzz/discuss/2654132/Easy-Python-Solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
c=[]
for i in range(1,n+1):
if(i%3 ==0 and i%5==0):
c.append("FizzBuzz")
elif(i%3==0):
c.append("Fizz")
elif(i%5==0):
c.append("Buzz")
else:
c.append(str(i))
return c | fizz-buzz | Easy Python Solution | Abhisheksoni5975 | 0 | 3 | fizz buzz | 412 | 0.69 | Easy | 7,232 |
https://leetcode.com/problems/fizz-buzz/discuss/2652998/Python3-one-line-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
return ["FizzBuzz" if i % 3 == 0 and i % 5 == 0 else "Fizz" if i % 3 == 0 else "Buzz" if i % 5 == 0 else str(i) for i in range(1, n + 1)] | fizz-buzz | Python3 one line solution | hideuk | 0 | 4 | fizz buzz | 412 | 0.69 | Easy | 7,233 |
https://leetcode.com/problems/fizz-buzz/discuss/2605123/Python3 | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
l = []
for i in range(1,n+1):
if i%3 == 0 and i%5 == 0:
l.append("FizzBuzz")
elif i%3 == 0:
l.append("Fizz")
elif i%5 == 0:
l.append("Buzz")
else:
l.append(str(i))
return l | fizz-buzz | Python3 | amansaini1030 | 0 | 61 | fizz buzz | 412 | 0.69 | Easy | 7,234 |
https://leetcode.com/problems/fizz-buzz/discuss/2575421/Python3-Solution-with-using-if | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ans = []
for i in range(1, n + 1):
tmp = [""]
if i % 3 == 0:
tmp.append("Fizz")
if i % 5 == 0:
tmp.append("Buzz")
if len(tmp) == 1:
tmp[0] = str(i)
ans.append(''.join(tmp))
return ans | fizz-buzz | [Python3] Solution with using if | maosipov11 | 0 | 57 | fizz buzz | 412 | 0.69 | Easy | 7,235 |
https://leetcode.com/problems/fizz-buzz/discuss/2328304/Python-simple-solution | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
arr = [None] * n
for i in range(1, n+1):
if (i%3==0 and i%5==0):
arr[i-1] = "FizzBuzz"
elif (i%3==0):
arr[i-1] = "Fizz"
elif (i%5 == 0):
arr[i-1] = "Buzz"
else:
arr[i-1] = str(i)
return arr | fizz-buzz | Python simple solution | nanicp2202 | 0 | 181 | fizz buzz | 412 | 0.69 | Easy | 7,236 |
https://leetcode.com/problems/fizz-buzz/discuss/2323509/Python-Simple-faster-solution-oror-Else-If-Ladder | class Solution:
# Time Complexity O(n)
# Space Complexity O(1)
def fizzBuzz(self, n: int) -> List[str]:
result = []
for i in range(1,n+1):
isDivisibleBy3, isDivisibleBy5 = i % 3 == 0, i % 5 == 0
if isDivisibleBy3 and isDivisibleBy5: result.append("FizzBuzz")
elif isDivisibleBy3: result.append("Fizz")
elif isDivisibleBy5: result.append("Buzz")
else: result.append(str(i))
return result | fizz-buzz | [Python] Simple faster solution || Else If Ladder | Buntynara | 0 | 65 | fizz buzz | 412 | 0.69 | Easy | 7,237 |
https://leetcode.com/problems/fizz-buzz/discuss/2282281/Python3-or-Without-else | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
total = []
for i in range(1,n+1):
if i % 3 == 0 and i % 5 == 0:
total.append("FizzBuzz")
continue
if i % 3 == 0:
total.append("Fizz")
continue
if i % 5 == 0:
total.append("Buzz")
continue
total.append(str(i))
return total | fizz-buzz | Python3 | Without else | muradbay | 0 | 101 | fizz buzz | 412 | 0.69 | Easy | 7,238 |
https://leetcode.com/problems/arithmetic-slices/discuss/1816132/beginners-solution-Easy-to-understand | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
count = 0
for i in range(len(nums)-2):
j = i+1
while(j<len(nums)-1):
if nums[j]-nums[j-1] == nums[j+1]-nums[j]:
count += 1
j += 1
else:
break
return count | arithmetic-slices | beginners solution, Easy to understand | harshsatpute16201 | 3 | 201 | arithmetic slices | 413 | 0.651 | Medium | 7,239 |
https://leetcode.com/problems/arithmetic-slices/discuss/1071181/Python.-faster-than-97.99.-O(n)-Super-simple-and-easy-understanding-solution | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
if len(A) < 3: return 0
res, counter = 0, 2
last_dif = A[1] - A[0]
for index, num in enumerate(A[2:], 1):
if last_dif == num - A[index]:
counter += 1
else:
if counter >= 3:
res += (counter - 1) * (counter - 2) // 2
counter = 2
last_dif = num - A[index]
if counter >= 3:
res += (counter - 1) * (counter - 2) // 2
return res | arithmetic-slices | Python. faster than 97.99%. O(n), Super simple & easy-understanding solution | m-d-f | 3 | 390 | arithmetic slices | 413 | 0.651 | Medium | 7,240 |
https://leetcode.com/problems/arithmetic-slices/discuss/1025130/Sliding-Window | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
l = 0
res = 0
for r, num in enumerate(A):
if r - l < 2:
continue
if num - A[r-1] == A[l+1] - A[l]:
res += r - l - 1
else:
l = r - 1
return res | arithmetic-slices | Sliding Window | aquafie | 2 | 135 | arithmetic slices | 413 | 0.651 | Medium | 7,241 |
https://leetcode.com/problems/arithmetic-slices/discuss/2450433/Clean-Fast-Python3-or-O(n)-Time-O(1)-Space | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n, subs = len(nums), 0
last_diff, count = None, 0
for i in range(1, n):
this_diff = nums[i] - nums[i - 1]
if this_diff == last_diff:
subs += count
count += 1
else:
last_diff = this_diff
count = 1
return subs | arithmetic-slices | Clean, Fast Python3 | O(n) Time, O(1) Space | ryangrayson | 1 | 20 | arithmetic slices | 413 | 0.651 | Medium | 7,242 |
https://leetcode.com/problems/arithmetic-slices/discuss/2086681/Easy-to-understand-iteration-python-solution | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
length=len(nums)
res=0
if length>=3:
count=0
for i in range(length-2):
if nums[i]-nums[i+1]==nums[i+1]-nums[i+2]:
count+=1
res+=count
else:
count=0
return res | arithmetic-slices | Easy to understand iteration python solution | xsank | 1 | 21 | arithmetic slices | 413 | 0.651 | Medium | 7,243 |
https://leetcode.com/problems/arithmetic-slices/discuss/1817142/Python-Noob-Solution-O(N)-or-O(1) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3: return 0
nums.append(2001) # so thatit will surely break at last
left = count = 0
for i in range(2,len(nums)):
if nums[i] - nums[i-1] != nums[i-1] - nums[i-2]: # streak breaks
n = i-left # streak length
count += (n * (n+1) // 2 - (2 * n - 1)) # add the number of subarray formed using the length of the strek
left = i - 1 # store the streak breakpoint
return count | arithmetic-slices | Python Noob Solution O(N) | O(1) | dhananjay79 | 1 | 31 | arithmetic slices | 413 | 0.651 | Medium | 7,244 |
https://leetcode.com/problems/arithmetic-slices/discuss/1816130/Simple-Python-code-pretty-fast | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
diff = [nums[i]-nums[i-1] for i in range(1, len(nums))]
ans = [0] * len(diff)
for i in range(1, len(diff)):
if diff[i]==diff[i-1]:
ans[i] = ans[i-1]+1
return sum(ans) | arithmetic-slices | Simple Python code, pretty fast | Jeff871025 | 1 | 20 | arithmetic slices | 413 | 0.651 | Medium | 7,245 |
https://leetcode.com/problems/arithmetic-slices/discuss/1815105/Easy-two-pointers-solution-O(n)-time-or-O(1)-space-(Python-C%2B%2B-Javascript) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums)<=2:
return 0
res=0
left=0
right=2
i=0 #increment
diff=nums[1]-nums[0]
while right<len(nums):
if nums[right]-nums[right-1]==diff:
right+=1
i+=1 #will increase by 1 for each iteration if nums from left to right in AP
res+=i
else:
i=0
left=right-1
diff=nums[right]-nums[right-1]
right+=1
return res | arithmetic-slices | Easy two pointers solution O(n) time | O(1) space (Python, C++, Javascript) | amlanbtp | 1 | 20 | arithmetic slices | 413 | 0.651 | Medium | 7,246 |
https://leetcode.com/problems/arithmetic-slices/discuss/1603047/Python3-Solution-with-using-dp | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
dp = [0] * len(nums)
res = 0
for idx in range(2, len(nums)):
if nums[idx - 1] - nums[idx - 2] == nums[idx] - nums[idx - 1]:
dp[idx] = dp[idx - 1] + 1
res += dp[idx]
return res | arithmetic-slices | [Python3] Solution with using dp | maosipov11 | 1 | 79 | arithmetic slices | 413 | 0.651 | Medium | 7,247 |
https://leetcode.com/problems/arithmetic-slices/discuss/1498271/Python-O(n)-time-O(1)-space-solution | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
res, count, d = 0, 0, float('inf')
n = len(nums)
for i in range(1, n):
if nums[i] - nums[i-1] == d:
count += 1
else:
res += count*(count+1)//2
count = 0
d = nums[i]-nums[i-1]
return res + count*(count+1)//2 | arithmetic-slices | Python O(n) time, O(1) space solution | byuns9334 | 1 | 89 | arithmetic slices | 413 | 0.651 | Medium | 7,248 |
https://leetcode.com/problems/arithmetic-slices/discuss/2737145/77ms-or-Python-solution | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if not len(nums):
return 0
diff, ans = 0, 0
for i in range(0, len(nums) - 2):
diff = nums[i+1] - nums[i]
for j in range(i + 2, len(nums)):
if nums[j] - nums[j-1] == diff:
ans += 1
else:
break
return ans | arithmetic-slices | 77ms | Python solution | kitanoyoru_ | 0 | 5 | arithmetic slices | 413 | 0.651 | Medium | 7,249 |
https://leetcode.com/problems/arithmetic-slices/discuss/2721035/Python-Solution-oror-92.30-Faster-oror-Space-Complexity%3A-O(1) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
if n < 3:
return 0
prev = 0
curr = 0
for i in range(2, n):
curr = 0
if nums[i]-nums[i-1] == nums[i-1]-nums[i-2]:
curr = prev + 1
ans += curr
prev = curr
return ans | arithmetic-slices | Python Solution || 92.30% Faster || Space Complexity: O(1) | shreya_pattewar | 0 | 2 | arithmetic slices | 413 | 0.651 | Medium | 7,250 |
https://leetcode.com/problems/arithmetic-slices/discuss/2602988/two-pointer | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n =len(nums)
if n<=2:
return 0
ans =0
c=1
i=0
j=0
while(i<n-1 and j <n ):
dif =nums[i+1]-nums[i]
j =i+1
while(j<n-1 and nums[j+1]-nums[j]==dif):
ans+=c
c+=1
j+=1
i =j
c=1
return ans | arithmetic-slices | two pointer | abhayCodes | 0 | 15 | arithmetic slices | 413 | 0.651 | Medium | 7,251 |
https://leetcode.com/problems/arithmetic-slices/discuss/2560785/Python-Solution-oror-Easy-oror-faster-than-94.06-of-Python3 | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
# no need to continue if the length is not enough
if len(nums) < 3:
return 0
ans = 0
for i in range(len(nums)-1):
# diff should be the same as that between first two elements in the list
diff = nums[i+1] - nums[i]
cur = [nums[i]]
for j in range(i+1, len(nums)):
if nums[j] - nums[j-1] != diff:
break
else:
cur.append(nums[j])
if len(cur) >= 3:
ans+=1
return ans | arithmetic-slices | Python Solution || Easy || faster than 94.06% of Python3 | ckayfok | 0 | 49 | arithmetic slices | 413 | 0.651 | Medium | 7,252 |
https://leetcode.com/problems/arithmetic-slices/discuss/2383288/Python3-Two-Pointers-and-One-Pass | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
res = 0
i, j, diff = 0, 1, nums[1]-nums[0]
while j < len(nums):
newDiff = nums[j]-nums[j-1]
if newDiff != diff:
res += (j-i-1)*(j-i-2)//2
i, diff = j-1, newDiff
j += 1
res += (j-i-1)*(j-i-2)//2
return res | arithmetic-slices | [Python3] Two Pointers and One-Pass | ruosengao | 0 | 22 | arithmetic slices | 413 | 0.651 | Medium | 7,253 |
https://leetcode.com/problems/arithmetic-slices/discuss/2325482/dynamic-programming-is-simple-when-you-do-like-this-with-comments | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums)<=2:return 0
dp=[0 for i in range(len(nums))]
'''[1,2,3,4]
dp=[0,0,0,0]
1st:dp=[0,0,1,0]
2nd:dp=[0,0,1,2]({2,3,4} {1,2,3,4})
hence dp[i]=1+dp[i-1] works here (same as like kadanes algorithms)
overall subarrays possible is sum(dp)
you can also do with out dp by storing dp[i-1] as in variable
'''
dp[0]=0
dp[1]=0
num1,num2=nums[0],nums[1]
res=0
for i in range(2,len(nums)):
num3=nums[i]
if 2*num2==num1+num3:
dp[i]=1+dp[i-1]
res+=dp[i]
num1=num2
num2=num3
return res | arithmetic-slices | dynamic programming is simple when you do like this with comments | yaswanthkosuru | 0 | 12 | arithmetic slices | 413 | 0.651 | Medium | 7,254 |
https://leetcode.com/problems/arithmetic-slices/discuss/2153356/Python-one-pass-O(N)O(1) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
result = 0
lengths = 0
for i in range(2, len(nums)):
if nums[i] - nums[i-1] == nums[i-1] - nums[i-2]:
lengths += 1
result += lengths
else:
lengths = 0
return result | arithmetic-slices | Python, one-pass O(N)/O(1) | blue_sky5 | 0 | 41 | arithmetic slices | 413 | 0.651 | Medium | 7,255 |
https://leetcode.com/problems/arithmetic-slices/discuss/2063873/Python-or-DP-or-Easy-Solution-or-Time-%3A-O(n)-or-Space-%3A-O(1) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n = len(nums)
if n==1 or n==2:
return 0
s = 0
t = 0
for i in range(n-3,-1,-1):
l = 0
if nums[i]-nums[i+1] == nums[i+1]-nums[i+2]:
l = 1
if l != 0:
l += t
t = l
else:
t = 0
s += l
return s | arithmetic-slices | Python | DP | Easy Solution | Time : O(n) | Space : O(1) | Shivamk09 | 0 | 78 | arithmetic slices | 413 | 0.651 | Medium | 7,256 |
https://leetcode.com/problems/arithmetic-slices/discuss/2042121/Easy-Python-Greedy-O(n)-time-and-O(1)-space-soluton | class Solution:
# Sum of n natural numbers
def sumN(self, n):
return int(n*(n+1)/2)
def numberOfArithmeticSlices(self, arr) -> int:
n = len(arr)
if 3 > n:
return 0
ans = 0
i = 0
j = 1
diff = arr[j] - arr[i]
while j < n and i < n:
if (arr[j] - arr[j-1]) == diff:
j += 1
else:
if (j - i) >= 3:
ans += self.sumN(j - i - 2)
i = j-1
diff = arr[j] - arr[i]
if (j - i + 1) > 3:
ans += self.sumN(j - i - 2)
return ans | arithmetic-slices | Easy Python Greedy O(n) time and O(1) space soluton | dbansal18 | 0 | 25 | arithmetic slices | 413 | 0.651 | Medium | 7,257 |
https://leetcode.com/problems/arithmetic-slices/discuss/2001777/Python3-oror-Simple-Solution-oror-O(n)-time-oror-Less-Calculation-oror-Math | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
total, count, num = 0, 1, nums[0] - nums[1]
for i in range(2, len(nums)):
diff = nums[i-1] - nums[i]
if num == diff:
count += 1
else:
num = diff
if count > 1:
# this is just a math formula. When you reach the end of subarray, use it
total += (count + 1)*(count - 2)//2 + 1
count = 1
if count > 1:
total += (count + 1)*(count - 2)//2 + 1
return total | arithmetic-slices | Python3 || Simple Solution || O(n) time || Less Calculation || Math | otabek8866 | 0 | 61 | arithmetic slices | 413 | 0.651 | Medium | 7,258 |
https://leetcode.com/problems/arithmetic-slices/discuss/1979471/python-3-oror-O(n)-time-oror-O(1)-space | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) <= 2:
return 0
res = 0
diff = nums[1] - nums[0]
curLen = 2
for i in range(2, len(nums)):
curDiff = nums[i] - nums[i-1]
if curDiff == diff:
curLen += 1
else:
res += (curLen - 1) * (curLen - 2) // 2
curLen = 2
diff = curDiff
return res + (curLen - 1) * (curLen - 2) // 2 | arithmetic-slices | python 3 || O(n) time || O(1) space | dereky4 | 0 | 49 | arithmetic slices | 413 | 0.651 | Medium | 7,259 |
https://leetcode.com/problems/arithmetic-slices/discuss/1817581/Python-DP | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
def isArithmetic(i):
return
p, total, pWasArithmetic = 0, 0, False
for i in range(2, len(nums)):
if not isArithmetic(i):
pWasArithmetic = False
continue
p = p + 1
total = total + p if pWasArithmetic else total + 1
pWasArithmetic = True
return total | arithmetic-slices | Python DP | Rush_P | 0 | 26 | arithmetic slices | 413 | 0.651 | Medium | 7,260 |
https://leetcode.com/problems/arithmetic-slices/discuss/1815908/Python-or-DP | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n=len(nums)
dp=[0]*n
for i in range(1,n-1):
if(nums[i]-nums[i-1]==nums[i+1]-nums[i]):
dp[i+1]=1+dp[i]
return sum(dp)
``` | arithmetic-slices | Python | DP | InvincibleTaki | 0 | 22 | arithmetic slices | 413 | 0.651 | Medium | 7,261 |
https://leetcode.com/problems/arithmetic-slices/discuss/1815758/Dynamic-Programming-Solution-Python | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums)<3:
return 0
cnt = 0
dp = [0 for _ in range(len(nums))]
for i in range(2, len(nums)):
if nums[i-2]-nums[i-1] == nums[i-1]-nums[i]:
dp[i] = 1 + dp[i-1]
cnt += dp[i]
return cnt | arithmetic-slices | Dynamic Programming Solution Python | takahiro2 | 0 | 6 | arithmetic slices | 413 | 0.651 | Medium | 7,262 |
https://leetcode.com/problems/arithmetic-slices/discuss/1815268/Python-Solutions-with-Detail-Equation-explanation | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
res, i = 0, 0
N = len(nums)
while i < N - 1:
j = i + 2
while j < N and nums[j-1] - nums[j] == nums[i] - nums[i+1]:
j += 1
n = j - i
res += n * (n - 3) // 2 + 1
i = j
return res | arithmetic-slices | Python Solutions with Detail Equation explanation | atiq1589 | 0 | 21 | arithmetic slices | 413 | 0.651 | Medium | 7,263 |
https://leetcode.com/problems/arithmetic-slices/discuss/1815002/Python-or-Easy-with-Expaination-or-O(N)-time-or-O(1)-SPACE-OPTIMAL-SOLUTION | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums) < 3:
return 0
n = len(nums)
getdiff = lambda i,j: nums[i]-nums[j]
getPossibilities = lambda l : (l*(l+1))//2
checkMinSubArrayLength = lambda i,j : abs(i-j) >= 2
i,j=0,1
diff = getdiff(j,i)
cnt = 0
while i<n and j<n:
if checkMinSubArrayLength(i,j):
if diff != getdiff(j,j-1):
#scenario
#[1,2,4] i=0,j=2 (Not a valid Arithmetic Subarray)
diff = getdiff(j,j-1)
i = j-1
continue
length = 3
j+=1
while j<n:
if diff == getdiff(j,j-1):
length+=1
j+=1
else:
#scenario [1,2,3,6,9,12] now say i = idx 0 and j = idx 4 this block adjusts i to idx 3 (Will understand once we trace this, i mean why i = j-1)
diff = getdiff(j, j-1)
i = j-1
break
cnt += getPossibilities(length-2)
#What i did here?
#say array is [1,2,4,6,8,10,12] When i entered this if condition i = 1, j = 4
#now i figured out idx 1 -> idx 6 are in Ap (length 6)
# in a 6 length array we can have 3 -> 4 length Subarrays and 3 -> 4 length subarrays and 2 -> 5 length Subarrays and 1 -> 6 length Subarray
# 4+3+2+1 => sum of length-2 natural numbers
else:
j+=1
return cnt | arithmetic-slices | Python | Easy with Expaination | O(N) time | O(1) SPACE OPTIMAL SOLUTION | sathwickreddy | 0 | 30 | arithmetic slices | 413 | 0.651 | Medium | 7,264 |
https://leetcode.com/problems/arithmetic-slices/discuss/1814953/Python3-or-Single-Iteration-or-Simple-Solution | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
# if length of nums is less then 3 return 0
if len(nums) < 3:
return 0
# cnt : count the subarray, ind: count the single subarray, diff : monitoring the diff of current pointer
cnt, ind, diff = 0, 0, 0
# first diff
prevDif = nums[1] - nums[0]
# loop start from 1 to len(nums)-1
for i in range(1,len(nums) - 1):
#diff of the current pointer
diff = nums[i+1] - nums[i]
# if it is equal to prev one then update the ind else make prevDif to current diff and ind = 0
if diff == prevDif:
ind += 1
else:
prevDif = diff
ind = 0
# update the count
cnt += ind
return cnt | arithmetic-slices | Python3 | Single Iteration | Simple Solution | mady09 | 0 | 19 | arithmetic slices | 413 | 0.651 | Medium | 7,265 |
https://leetcode.com/problems/arithmetic-slices/discuss/1814679/Python3-O(N)-One-pass-solution | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
diff=[]
for i in range(1,len(nums)):
diff.append(nums[i]-nums[i-1])
ans=0
N=1
for i in range(1,len(diff)):
if diff[i]==diff[i-1]:
N+=1
else:
if N>1:
ans+=N*(N-1)//2
N=1
ans+=N*(N-1)//2
return ans | arithmetic-slices | Python3 O(N) One pass solution | KiranRaghavendra | 0 | 26 | arithmetic slices | 413 | 0.651 | Medium | 7,266 |
https://leetcode.com/problems/arithmetic-slices/discuss/1814606/Python-Simple-Python-Solution-Using-Iterative-Approach | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
result = 0
check = 0
for i in range(len(A)-2):
subarray = A[i:i+3]
if subarray[2] - subarray[1] == subarray[1] - subarray[0]:
difference=subarray[2] - subarray[1]
result = result + 1
for j in range(i+3,len(A)):
if A[j] - subarray[-1] == difference:
subarray.append(A[j])
result = result + 1
else:
break
return result | arithmetic-slices | [ Python ] ✔✔ Simple Python Solution Using Iterative Approach 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 0 | 26 | arithmetic slices | 413 | 0.651 | Medium | 7,267 |
https://leetcode.com/problems/arithmetic-slices/discuss/1796438/Python-or-Different-Dynamic-Programming-Solution-with-comments-or-O(N) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
if len(nums)<3:
return 0
stack=[]
dp=[0]*len(nums)
start=1
fin=0
diff=dp[1]=nums[1]-nums[0] #find the first difference which will help in comparing
for i in range(2,len(nums)):
dp[i]=nums[i]-nums[i-1] #Subtract and keep that number in dp
if dp[i]!=diff: #If not equal, that means we have come to end of subarray and can add it to stack
if (i-start+1)>2: #make sure of the length
stack.append(nums[start-1:i]) #start-1 because we are subtracting from the second element
diff=dp[i] #set the new difference
start=i
stack.append(nums[start-1:i+1])
for i in stack: #this for loop will identify and add the number of sets that can be generated
x=len(i)
fin+=int((x-1)*(x-2)/2)
return fin | arithmetic-slices | Python | Different Dynamic Programming Solution with comments | O(N) | RickSanchez101 | 0 | 32 | arithmetic slices | 413 | 0.651 | Medium | 7,268 |
https://leetcode.com/problems/arithmetic-slices/discuss/1771411/Python-easy-to-read-and-understand-or-DP | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
n = len(nums)
if n < 3:
return 0
t = [0 for _ in range(n)]
for i in range(2, n):
if nums[i] - nums[i-1] == nums[i-1] - nums[i-2]:
t[i] = t[i-1] + 1
return sum(t) | arithmetic-slices | Python easy to read and understand | DP | sanial2001 | 0 | 51 | arithmetic slices | 413 | 0.651 | Medium | 7,269 |
https://leetcode.com/problems/arithmetic-slices/discuss/1687283/Python-Simple-Solution-for-someone-who-likes-mathematics.-Complexity%3A-O(N) | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
count = 0
n = len(nums)
if n < 3:
return count
else:
difference_array = list()
for i in range(n-1):
difference_array.append(nums[i+1]-nums[i])
counter = 1
for j in range(1, len(difference_array)):
if difference_array[j] - difference_array[j-1] == 0:
counter = counter + 1
else:
if counter > 1:
consecutive_num_count = counter +1 -2
sum_of_consecutive_nums = (consecutive_num_count*(consecutive_num_count+1))//2
count = count + sum_of_consecutive_nums
counter = 1
if counter > 1:
consecutive_num_count = counter +1 -2
sum_of_consecutive_nums = (consecutive_num_count*(consecutive_num_count+1))//2
count = count + sum_of_consecutive_nums
return count | arithmetic-slices | Python Simple Solution for someone who likes mathematics. Complexity: O(N) | anushkabajpai | 0 | 42 | arithmetic slices | 413 | 0.651 | Medium | 7,270 |
https://leetcode.com/problems/arithmetic-slices/discuss/1684139/Python-simple-solution-by-observation-Time-O(N)-Space-O(1) | class Solution(object):
def numberOfArithmeticSlices(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
if n < 3:
return 0
# 1, 3, 6, 10
res, cnt, temp = 0, 2, nums[1] - nums[0]
for i in range(2, n):
if nums[i] - nums[i-1] == temp:
cnt += 1
res += (cnt-2)
else:
temp = nums[i] - nums[i-1]
cnt = 2
return res | arithmetic-slices | [Python] simple solution by observation — Time O(N) Space O(1) | lucile5657 | 0 | 37 | arithmetic slices | 413 | 0.651 | Medium | 7,271 |
https://leetcode.com/problems/arithmetic-slices/discuss/1654923/python-oror-easy-oror-beginner-oror-dp | class Solution:
def numberOfArithmeticSlices(self, arr: List[int]) -> int:
dp=[0]*len(arr)
ans=0
for i in range(2,len(arr)):
if arr[i]-arr[i-1] == arr[i-1]-arr[i-2]:
dp[i]=dp[i-1]+1
ans+=dp[i]
return ans | arithmetic-slices | python || easy || beginner || dp | minato_namikaze | 0 | 50 | arithmetic slices | 413 | 0.651 | Medium | 7,272 |
https://leetcode.com/problems/arithmetic-slices/discuss/1650430/Easy-to-understand-python3-solution | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
diffarr=[]
res=0
for i in range(len(nums)-1):
diffarr.append(nums[i+1]-nums[i])
count=1
for i in range(len(diffarr)-1):
if diffarr[i]==diffarr[i+1]:
count+=1
else:
res+=(count)*(count-1)//2
count=1
res+=(count)*(count-1)//2
return res | arithmetic-slices | Easy to understand python3 solution | Karna61814 | 0 | 32 | arithmetic slices | 413 | 0.651 | Medium | 7,273 |
https://leetcode.com/problems/arithmetic-slices/discuss/1614826/intuitive-python-solution-w-explanation-53-in-time-47-in-space | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
def findArithmetic(start):
end = len(nums) - 1
if start >= end - 1:return -1
ini, count = start + 1, 2
ini_diff = nums[ini] - nums[start]
while ini < end:
if nums[ini + 1] - nums[ini] == ini_diff:
count += 1
ini += 1
else:break
if count <= 2:return -1
return count
ini = 0
ans = 0
while ini < len(nums) - 1:
length = findArithmetic(ini)
if length == -1:
ini += 1
continue
curr_total = ((length - 2) * (length - 1 ))//2
ans += curr_total
ini += length - 1
return ans | arithmetic-slices | intuitive python solution w explanation, 53% in time, 47% in space | 752937603 | 0 | 38 | arithmetic slices | 413 | 0.651 | Medium | 7,274 |
https://leetcode.com/problems/arithmetic-slices/discuss/1072627/PYTHON-97.99ms-faster-using-Common-Difference | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
# Calculate common difference
A = [A[i+1] - A[i] for i in range(len(A)-1)]
n = 2
result = 0
# Find the length of Arthimetic slices
# If the length = n, total combinations are
# (n**2 - 3*n + 2)//2
for i in range(len(A)-1):
if A[i] == A[i+1]:
n +=1
elif n > 2:
result += (n**2 - 3*n +2)//2
n = 2
else:
n = 2
# For the edge case where slice is at the end of A
if n > 2:
result += (n ** 2 - 3 * n + 2) // 2
return result | arithmetic-slices | [PYTHON] 97.99ms faster using Common Difference | amanpathak2909 | 0 | 19 | arithmetic slices | 413 | 0.651 | Medium | 7,275 |
https://leetcode.com/problems/arithmetic-slices/discuss/1072475/Python-Solution | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
n = len(A)
if n < 3:
return 0
slices = cnt = 0
r = A[1]-A[0]
for i in range(2, n):
if A[i]-A[i-1] == r:
cnt += 1
slices += cnt
else:
r = A[i]-A[i-1]
cnt = 0
return slices | arithmetic-slices | Python Solution | mariandanaila01 | 0 | 54 | arithmetic slices | 413 | 0.651 | Medium | 7,276 |
https://leetcode.com/problems/arithmetic-slices/discuss/824441/Python-3-or-DP-%2B-Math-or-Explanation | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
n = len(A)
diff = [0] * (n-1)
for i, val in enumerate(zip(A, A[1:])): diff[i] = val[1]-val[0] # calculate difference
diff.append(sys.maxsize)
as_count, cur = [], 2
for i in range(1, n): # count arithematic sequence and save to as_count
if diff[i] == diff[i-1]: cur += 1
else:
if cur >= 3: as_count.append(cur)
cur = 2
return sum((1+cnt-2) * (cnt-2) // 2 for cnt in as_count) # Use math to add up all arithmatic sequence | arithmetic-slices | Python 3 | DP + Math | Explanation | idontknoooo | 0 | 86 | arithmetic slices | 413 | 0.651 | Medium | 7,277 |
https://leetcode.com/problems/arithmetic-slices/discuss/644541/Python3-sliding-window-Arithmetic-Slices | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
ans = 0
l = 0
for r in range(2,len(A)):
if A[r] - A[r-1] == A[r-1] - A[r-2]:
ans += r - l - 1
else:
l = r - 1
return ans | arithmetic-slices | Python3 sliding window - Arithmetic Slices | r0bertz | 0 | 138 | arithmetic slices | 413 | 0.651 | Medium | 7,278 |
https://leetcode.com/problems/arithmetic-slices/discuss/534748/Python3-6-line | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
ans = cnt = 0
for i in range(2, len(nums)):
if nums[i-1] - nums[i-2] == nums[i] - nums[i-1]: cnt += 1
else: cnt = 0
ans += cnt
return ans | arithmetic-slices | [Python3] 6-line | ye15 | 0 | 129 | arithmetic slices | 413 | 0.651 | Medium | 7,279 |
https://leetcode.com/problems/arithmetic-slices/discuss/515121/Python3-both-O(n**2)-and-O(n)-solutions | class Solution:
def numberOfArithmeticSlices(self, A: List[int]) -> int:
"""
O(n) time complexity
O(1) space complexity
"""
dp,sums=[0]*len(A),0
for i in range(2,len(A)):
if (A[i]-A[i-1])==(A[i-1]-A[i-2]):
dp[i]=dp[i-1]+1
sums+=dp[i]
return sums
def numberOfArithmeticSlices1(self, A: List[int]) -> int:
"""
O(n^2) time complexity
O(1) space complexity
"""
count=0
for i in range(len(A)-2):
d=A[i+1]-A[i]
for j in range(i+2,len(A)):
if (A[j]-A[j-1])==d: count+=1
else: break
return count | arithmetic-slices | Python3 both O(n**2) and O(n) solutions | jb07 | 0 | 84 | arithmetic slices | 413 | 0.651 | Medium | 7,280 |
https://leetcode.com/problems/arithmetic-slices/discuss/1712707/Python-DP | class Solution:
def numberOfArithmeticSlices(self, nums: List[int]) -> int:
def totSlices(nums,slices): # For total number of same difference consecutive integers
count = 2
co = nums[1]-nums[0]
for i in range(2,n):
pre = nums[i]-nums[i-1]
if pre == co:
count += 1
else:
if count>2:
slices.append(count)
co = pre
count = 2
if count>2:
slices.append(count)
return slices
def totCombination(maxSlice,combination): #For combination of maximum slice integer
combination = [0]*(maxSlice+1)
st = 1
for i in range(3,maxSlice+1):
combination[i] = combination[i-1]+st
st+=1
return combination
n = len(nums)
if n < 3:
return 0
total = 0
slices = totSlices(nums,[])
if slices == []:
return 0
combination = totCombination(max(slices),[])
for i in slices:
total += combination[i]
return total | arithmetic-slices | Python DP | hrithikhh86 | -1 | 32 | arithmetic slices | 413 | 0.651 | Medium | 7,281 |
https://leetcode.com/problems/third-maximum-number/discuss/352011/Solution-in-Python-3-(beats-~99)-(-O(n)-) | class Solution:
def thirdMax(self, nums: List[int]) -> int:
n, T = list(set(nums)), [float('-inf')]*3
for i in n:
if i > T[0]:
T = [i,T[0],T[1]]
continue
if i > T[1]:
T = [T[0],i,T[1]]
continue
if i > T[2]:
T = [T[0],T[1],i]
return T[2] if T[2] != float('-inf') else T[0]
- Junaid Mansuri | third-maximum-number | Solution in Python 3 (beats ~99%) ( O(n) ) | junaidmansuri | 18 | 3,500 | third maximum number | 414 | 0.326 | Easy | 7,282 |
https://leetcode.com/problems/third-maximum-number/discuss/1309868/Python-O(n)-time-O(1)-Space-Easy-to-understand-Solution! | class Solution:
def thirdMax(self, nums: List[int]) -> int:
max1 = nums[0] #Initialised the max with first index
secmax = float('-inf')
thirmax = float('-inf')
#assuming second and third to be -infinity
if len(nums)<3:
return max(nums)
#this won't run more than 2 times and hence we can consider this in our O(n) solution!
# It isn't worth writing the Whole Loop logic here
for i in range(len(nums)):
num = nums[i]
#Read the below if conditions to get the approach of updating First, second and third max respectively
if (num>max1):
thirmax = secmax
secmax = max1
max1 = nums[i]
elif(num>secmax and num<max1):
thirmax = secmax
secmax = num
elif(num>thirmax and num<secmax):
thirmax = num
return thirmax if thirmax != float('-inf') else max1
#if condition when the elements get repeated such that thirdmax remains -infinity | third-maximum-number | Python O(n) time O(1) Space - Easy to understand Solution! ✔🙌 | dxmpu | 7 | 833 | third maximum number | 414 | 0.326 | Easy | 7,283 |
https://leetcode.com/problems/third-maximum-number/discuss/1461970/Simple-Python-O(n)-three-pointer-solution | class Solution:
def thirdMax(self, nums: List[int]) -> int:
max1 = max2 = max3 = -float("inf")
# max1 < max2 < max3
for n in nums:
if n in [max1, max2, max3]:
continue
if n > max3:
max1 = max2
max2 = max3
max3 = n
elif n > max2:
max1 = max2
max2 = n
elif n > max1:
max1 = n
return max1 if max1 != -float("inf") else max3 | third-maximum-number | Simple Python O(n) three pointer solution | Charlesl0129 | 4 | 416 | third maximum number | 414 | 0.326 | Easy | 7,284 |
https://leetcode.com/problems/third-maximum-number/discuss/2013751/Python-oneliner | class Solution:
def thirdMax(self, nums: List[int]) -> int:
return max(nums) if len(set(nums)) < 3 else sorted(list(set(nums)))[-3] | third-maximum-number | Python oneliner | StikS32 | 3 | 194 | third maximum number | 414 | 0.326 | Easy | 7,285 |
https://leetcode.com/problems/third-maximum-number/discuss/403979/python-sets | class Solution:
def thirdMax(self, nums: List[int]) -> int:
numset = set(nums)
if len(numset) <= 2:
return max(nums)
else:
for i in range(2):
numset = numset - {max(numset)}
return max(numset) | third-maximum-number | python sets | mars9000 | 3 | 202 | third maximum number | 414 | 0.326 | Easy | 7,286 |
https://leetcode.com/problems/third-maximum-number/discuss/1641503/Python-Easy-4-lines-Solution | class Solution:
def thirdMax(self, nums: List[int]) -> int:
nums = sorted(set(nums))
n = len(nums)
if (n>=3):
return(nums[n-3])
else:
return(nums[n-1]) | third-maximum-number | Python Easy 4 lines Solution | CoderIsCodin | 2 | 345 | third maximum number | 414 | 0.326 | Easy | 7,287 |
https://leetcode.com/problems/third-maximum-number/discuss/2258774/Python3-O(n)-oror-O(1)-Runtime%3A-71ms-68.83-oror-Memory%3A-14.9mb-79.38 | class Solution:
# O(n) || O(1)
# Runtime: 71ms 68.83% || Memory: 14.9mb 79.38%
def thirdMax(self, nums: List[int]) -> int:
if not nums:
return nums
threeLargest = [float('-inf')] * 3
for num in nums:
if not num in threeLargest:
self.updateThreeMax(threeLargest, num)
return threeLargest[0] if not float('-inf') in threeLargest else max(threeLargest)
def updateThreeMax(self, threeLargest, num):
if threeLargest[2] is None or threeLargest[2] < num:
self.shiftUp(threeLargest, num, 2)
elif threeLargest[1] is None or threeLargest[1] < num:
self.shiftUp(threeLargest, num, 1)
elif threeLargest[0] is None or threeLargest[0] < num:
self.shiftUp(threeLargest, num, 0)
def shiftUp(self, threeLargest, num, idx):
for i in range(idx + 1):
if i == idx:
threeLargest[i] = num
else:
threeLargest[i] = threeLargest[i + 1] | third-maximum-number | Python3 O(n) || O(1) # Runtime: 71ms 68.83% || Memory: 14.9mb 79.38% | arshergon | 1 | 67 | third maximum number | 414 | 0.326 | Easy | 7,288 |
https://leetcode.com/problems/third-maximum-number/discuss/2176289/Python-solution-beat-93-in-memory-NO-SORT | class Solution:
def thirdMax(self, nums: List[int]) -> int:
maxes = [-float('inf'), -float('inf'), -float('inf')]
for i in range(0,len(nums)):
curr = nums[i]
if curr in maxes:
continue
else:
if curr>maxes[-1]:
maxes.insert(len(maxes), curr)
elif curr>maxes[-2]:
maxes.insert(len(maxes)-1, curr)
elif curr>maxes[-3]:
maxes.insert(len(maxes)-2, curr)
if len(maxes)>3:
maxes.pop(0)
# maxes.append(curr)
if maxes[0]==-float('inf'):
return maxes[2]
else: return maxes[0] | third-maximum-number | Python solution - beat 93% in memory NO SORT | pratijayguha1 | 1 | 86 | third maximum number | 414 | 0.326 | Easy | 7,289 |
https://leetcode.com/problems/third-maximum-number/discuss/1820819/Ez-sols-oror-O(n)-TC | class Solution:
def thirdMax(self, nums: List[int]) -> int:
nums=list(set(nums))
n=len(nums)
if n<=2:
return max(nums)
nums.remove(max(nums))
nums.remove(max(nums))
return max(nums) | third-maximum-number | Ez sols || O(n) TC | ashu_py22 | 1 | 32 | third maximum number | 414 | 0.326 | Easy | 7,290 |
https://leetcode.com/problems/third-maximum-number/discuss/1820819/Ez-sols-oror-O(n)-TC | class Solution:
def thirdMax(self, nums: List[int]) -> int:
nums=list(set(nums))
n=len(nums)
if n<=2:
return max(nums)
nums.sort()
return nums[-3] | third-maximum-number | Ez sols || O(n) TC | ashu_py22 | 1 | 32 | third maximum number | 414 | 0.326 | Easy | 7,291 |
https://leetcode.com/problems/third-maximum-number/discuss/1820819/Ez-sols-oror-O(n)-TC | class Solution:
def thirdMax(self, nums: List[int]) -> int:
nums=list(set(nums))
if len(nums)<=2:
return max(nums)
maxx=-2**31-1
for i in range(len(nums)):
if nums[i]>maxx:
maxx=nums[i]
i=0
while(i<len(nums)):
if nums[i]==maxx:
nums.pop(i)
break
else:
i+=1
maxx=-2**31-1
for i in range(len(nums)):
if nums[i]>maxx:
maxx=nums[i]
i=0
while(i<len(nums)):
if nums[i]==maxx:
nums.pop(i)
break
else:
i+=1
maxx=-2**31-1
for i in range(len(nums)):
if nums[i]>maxx:
maxx=nums[i]
return maxx | third-maximum-number | Ez sols || O(n) TC | ashu_py22 | 1 | 32 | third maximum number | 414 | 0.326 | Easy | 7,292 |
https://leetcode.com/problems/third-maximum-number/discuss/1820819/Ez-sols-oror-O(n)-TC | class Solution:
def thirdMax(self, nums: List[int]) -> int:
nums=list(set(nums))
n=len(nums)
if n<=2:
return max(nums)
def quickselect(lo, hi):
i=lo
pivot=nums[hi]
for j in range(lo, hi):
if nums[j]<=pivot:
nums[i], nums[j] = nums[j], nums[i]
i+=1
nums[i], nums[hi] = nums[hi], nums[i]
if k==i:
return nums[k]
elif k>i:
return quickselect(i+1, hi)
else:
return quickselect(lo, i-1)
k=n-3
lo=0
hi=n-1
return quickselect(lo, hi) | third-maximum-number | Ez sols || O(n) TC | ashu_py22 | 1 | 32 | third maximum number | 414 | 0.326 | Easy | 7,293 |
https://leetcode.com/problems/third-maximum-number/discuss/1820819/Ez-sols-oror-O(n)-TC | class Solution:
def thirdMax(self, nums: List[int]) -> int:
final_values=[-2**32-1, -2**32-1, -2**32-1]
for i in nums:
if i not in final_values:
if i > final_values[0]:
final_values=[i, final_values[0], final_values[1]]
elif i > final_values[1]:
final_values=[final_values[0], i, final_values[1]]
elif i > final_values[2]:
final_values=[final_values[0], final_values[1], i]
if -2**32-1 in final_values:
return max(nums)
else:
return final_values[2] | third-maximum-number | Ez sols || O(n) TC | ashu_py22 | 1 | 32 | third maximum number | 414 | 0.326 | Easy | 7,294 |
https://leetcode.com/problems/third-maximum-number/discuss/1217179/Optimal-solution-beats-99-Speed-and-Memory | class Solution:
def thirdMax(self, nums: List[int]) -> int:
if len(nums) < 3: return max(nums)
first=second=third=float('-inf')
for num in nums:
if num > first:
first,second,third=num,first,second
elif num > second and num<first:
second,third=num,second
elif num > third and num < second:
third=num
return third if third != float('-inf') else max(nums) | third-maximum-number | Optimal solution beats 99% Speed and Memory | hasham | 1 | 155 | third maximum number | 414 | 0.326 | Easy | 7,295 |
https://leetcode.com/problems/third-maximum-number/discuss/1068866/Python3-beats-99.91-of-submissions | class Solution:
def thirdMax(self, nums: List[int]) -> int:
ordered = sorted(list(set(nums)))
if len(ordered) >= 3:
return ordered[-3]
else:
return max(ordered) | third-maximum-number | Python3 beats 99.91% of submissions | veevyo | 1 | 98 | third maximum number | 414 | 0.326 | Easy | 7,296 |
https://leetcode.com/problems/third-maximum-number/discuss/941918/Python-Solution-and-Explanation | class Solution:
def thirdMax(self, nums: List[int]) -> int:
# pass nums list through a set to remove duplicates
# revert our set back into a list
unique_nums = list(set(nums))
# sort in descending order
unique_nums = sorted(unique_nums, reverse=True)
# check if length of unique nums list is less than 3
if len(unique_nums) < 3:
# return max num
return unique_nums[0]
else:
# return value at 3rd index of unique nums list
return unique_nums[2] | third-maximum-number | Python Solution and Explanation | ErikRodriguez-webdev | 1 | 413 | third maximum number | 414 | 0.326 | Easy | 7,297 |
https://leetcode.com/problems/third-maximum-number/discuss/2845422/Python-simple-for-loop-O(n)-dynamic-programming-beats-94.17-runtime-and-94.32-mem-usage | class Solution:
def thirdMax(self, nums: List[int]) -> int:
m1 = m2 = m3 = float('-inf')
for x in nums:
if x in (m1, m2, m3):
continue
if x > m1:
m1, m2, m3 = x, m1, m2
elif x > m2:
m2, m3 = x, m2
elif x > m3:
m3 = x
return m3 if m3 != float('-inf') else m1 | third-maximum-number | Python simple for loop O(n) dynamic programming beats 94.17% runtime & 94.32% mem usage | Molot84 | 0 | 1 | third maximum number | 414 | 0.326 | Easy | 7,298 |
https://leetcode.com/problems/third-maximum-number/discuss/2826260/Python-solution | class Solution:
def thirdMax(self, nums: List[int]) -> int:
nums=list(set(nums))
nums.sort()
if len(nums)<=2:
return max(nums)
else:
return nums[-3] | third-maximum-number | Python solution | SheetalMehta | 0 | 5 | third maximum number | 414 | 0.326 | Easy | 7,299 |
Subsets and Splits