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")
... | 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):
... | 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")
... | 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... | 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... | 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")
... | 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")
... | 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... | 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.a... | 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 divisibl... | 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("B... | 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 "B... | 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_st... | 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"
... | 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 answ... | 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.a... | 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:
... | 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:
... | 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:
... | 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")... | 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 == '':
... | 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:
n... | 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)... | 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[s... | 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'
... | 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.ap... | 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("F... | 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]:... | 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("... | 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:
... | 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:
... | 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:
... | 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"
... | 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("FizzBu... | 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
... | 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
... | 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
... | 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
... | 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
... | 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
... | 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] ... | 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:
... | 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]:
... | 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
... | 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] ... | 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]:
... | 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[... | 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
... | 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 += (... | 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... | 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:
lengt... | 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
... | 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]
... | 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
... | 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:
... | 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(... | 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]
... | 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) /... | 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
... | 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
... | 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:
... | 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)):
i... | 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(num... | 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])
... | 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... | 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+... | 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:
i... | 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
... | 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:
... | 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): ... | 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
su... | 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]
... | 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] != ... | 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)
... | 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
... | 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(... | 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]:
m... | 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 num... | 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:
... | 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_v... | 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 nu... | 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)
... | 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 ... | 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
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.