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/relative-ranks/discuss/2464096/Python-Solution-oror-easy-to-understand-oror-beginner-friendly | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
s=[]
l=score.copy()
l.sort(reverse=True)
for i in range(len(l)):
if score[i]==l[0]:
s.append("Gold Medal")
elif score[i]==l[1]:
s.append("Silver Medal"... | relative-ranks | Python Solution || easy to understand || beginner friendly | T1n1_B0x1 | 1 | 79 | relative ranks | 506 | 0.592 | Easy | 8,900 |
https://leetcode.com/problems/relative-ranks/discuss/2223290/Python-beats-99.43-(%2B-easy-to-understand) | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
srt: list = [(score[i], i) for i in range(len(score))] # Save info about index
srt.sort(reverse=True, key=lambda t: t[0]) # Sort based on value
ret: list = [""] * len(score)
for i, v in enumerate(srt):
... | relative-ranks | Python beats 99.43 % (+ easy to understand) | amaargiru | 1 | 108 | relative ranks | 506 | 0.592 | Easy | 8,901 |
https://leetcode.com/problems/relative-ranks/discuss/1915471/Python-easy-solution-with-memory-less-than-91 | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
res = []
score_sort = sorted(score, reverse=True)
for i in score:
if score_sort.index(i) == 0:
res.append("Gold Medal")
elif score_sort.index(i) == 1:
res.appen... | relative-ranks | Python easy solution with memory less than 91% | alishak1999 | 1 | 107 | relative ranks | 506 | 0.592 | Easy | 8,902 |
https://leetcode.com/problems/relative-ranks/discuss/1342662/Python-Solution-or-Time-O(nlogn)-or-Space-O(n) | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
sorted_score = sorted(score, reverse=True)
hashmap = {}
if len(score) > 2:
hashmap[sorted_score[0]] = "Gold Medal"
hashmap[sorted_score[1]] = "Silver Medal"
... | relative-ranks | Python Solution | Time - O(nlogn) | Space - O(n) | peatear-anthony | 1 | 143 | relative ranks | 506 | 0.592 | Easy | 8,903 |
https://leetcode.com/problems/relative-ranks/discuss/1293533/Python3-dollarolution-(93-Faster) | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
d, m = {}, []
s = sorted(score, reverse = True)
for i in range(len(s)):
if i == 0:
d[s[i]] = 'Gold Medal'
elif i == 1:
d[s[i]] = 'Silver Medal'
elif... | relative-ranks | Python3 $olution (93% Faster) | AakRay | 1 | 158 | relative ranks | 506 | 0.592 | Easy | 8,904 |
https://leetcode.com/problems/relative-ranks/discuss/856950/Python-simple-and-easy-solution | class Solution(object):
def findRelativeRanks(self, nums):
sorteddata = sorted(nums,reverse=True)
result = []
for n in nums:
if n == sorteddata[0]:
result.append('Gold Medal')
elif n == sorteddata[1]:
result.append('Silver Medal')
... | relative-ranks | Python simple and easy solution | RandomPP | 1 | 141 | relative ranks | 506 | 0.592 | Easy | 8,905 |
https://leetcode.com/problems/relative-ranks/discuss/352311/Solution-in-Python-3-(beats-~100)-(three-lines) | class Solution:
def findRelativeRanks(self, nums: List[int]) -> List[str]:
for i, (j,k) in enumerate(sorted(zip(nums,range(len(nums))), reverse = True)):
nums[k] = str(i+1) if i > 2 else ["Gold","Silver","Bronze"][i] + " Medal"
return nums
- Junaid Mansuri
(LeetCode ID)@hotmail.com | relative-ranks | Solution in Python 3 (beats ~100%) (three lines) | junaidmansuri | 1 | 511 | relative ranks | 506 | 0.592 | Easy | 8,906 |
https://leetcode.com/problems/relative-ranks/discuss/2845509/Python3-or-Hashmap-or-Sorting | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
res = []
s = sorted(score,reverse = True)
r = ["Gold Medal","Silver Medal","Bronze Medal"]
for i in range(4,len(score)+1):
r.extend([str(i)])
d = {}
for i in range(len(s)):
... | relative-ranks | Python3 | Hashmap | Sorting | chakalivinith | 0 | 1 | relative ranks | 506 | 0.592 | Easy | 8,907 |
https://leetcode.com/problems/relative-ranks/discuss/2733423/Python-Simple-and-Fast-Solution-oror-O(N)-Time | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
arr = sorted(score, reverse = True)
if len(score) == 1:
return ['Gold Medal']
elif len(score) == 2:
if score[0] == max(score):
return ['Gold Medal', 'Silver Medal']
... | relative-ranks | Python - Simple and Fast Solution || O(N) Time | dayaniravi123 | 0 | 13 | relative ranks | 506 | 0.592 | Easy | 8,908 |
https://leetcode.com/problems/relative-ranks/discuss/2695607/python-solution | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
s = score.copy()
s.sort(reverse=True)
out = []
for i in score:
if s.index(i) == 0:
out.append("Gold Medal")
elif s.index(i) == 1:
out.append("Silver Med... | relative-ranks | python solution | anshsharma17 | 0 | 8 | relative ranks | 506 | 0.592 | Easy | 8,909 |
https://leetcode.com/problems/relative-ranks/discuss/2645485/Python-Simple-clean-2-solutions-3-line | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
sorted_score = sorted(score, reverse=True)
len_s = len(sorted_score)
d = {i:j for i,j in zip(sorted_score, ["Gold Medal",'Silver Medal', 'Bronze Medal', *(str(i) for i in range(4,len_s+1)) ])}
return [d[val] ... | relative-ranks | [Python] Simple, clean, 2 solutions 3-line | girraj_14581 | 0 | 30 | relative ranks | 506 | 0.592 | Easy | 8,910 |
https://leetcode.com/problems/relative-ranks/discuss/2645485/Python-Simple-clean-2-solutions-3-line | class Solution:
def findRelativeRanks(self, nums):
sort = sorted(nums, reverse=True)
rank = ["Gold Medal", "Silver Medal", "Bronze Medal"] + map(str, range(4, len(nums) + 1))
return map(dict(zip(sort, rank)).get, nums) | relative-ranks | [Python] Simple, clean, 2 solutions 3-line | girraj_14581 | 0 | 30 | relative ranks | 506 | 0.592 | Easy | 8,911 |
https://leetcode.com/problems/relative-ranks/discuss/2607339/Python-simple-solution-using-hashmap | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
r, tmp = range(len(score)), sorted(score, reverse=True)
d, rank = {}, {0: "Gold Medal", 1: "Silver Medal", 2: "Bronze Medal"}
for i in r:
d[tmp[i]] = rank.get(i, str(i + 1))
for j in r:
... | relative-ranks | Python simple solution using hashmap | Mark_computer | 0 | 29 | relative ranks | 506 | 0.592 | Easy | 8,912 |
https://leetcode.com/problems/relative-ranks/discuss/2445852/Easy-Python-Solution | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
li = []
s = sorted(score)
ss = sorted(score, reverse = True)
for i in range(len(score)):
if score[i]==s[-1]:
li.append("Gold Medal")
elif score[i]==s[-2]:
... | relative-ranks | Easy Python Solution | NishantKr1 | 0 | 59 | relative ranks | 506 | 0.592 | Easy | 8,913 |
https://leetcode.com/problems/relative-ranks/discuss/2357164/Easy-priority-queue-%2B-hash-table | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
dict_={}
for i in score:
dict_[i]=0
c=len(score)
heapify(score)
while score:
s = heappop(score)
if c>3:
dict_[s]=str(c)
elif c==3:
... | relative-ranks | Easy priority queue + hash table | sunakshi132 | 0 | 43 | relative ranks | 506 | 0.592 | Easy | 8,914 |
https://leetcode.com/problems/relative-ranks/discuss/2285754/Easy-to-Understand-Python-solution... | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
if len(score)==1:
return ["Gold Medal"]
elif len(score)==2:
if score[1]>score[0]:
return ["Silver Medal","Gold Medal"]
else:
return ["Gold Medal","Silver Me... | relative-ranks | Easy to Understand Python solution... | guneet100 | 0 | 57 | relative ranks | 506 | 0.592 | Easy | 8,915 |
https://leetcode.com/problems/relative-ranks/discuss/2186506/Python-Solution-O(n)-Time-Complexity | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
temp=sorted(score)[::-1]
for i in range(len(score)):
if temp[0]==score[i]:
score[i]="Gold Medal"
elif len(score)>1 and temp[1]==score[i] :
score[i]="Silver Medal"
... | relative-ranks | Python Solution O(n) Time Complexity | Kunalbmd | 0 | 78 | relative ranks | 506 | 0.592 | Easy | 8,916 |
https://leetcode.com/problems/relative-ranks/discuss/1934765/Python-Solution | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
i = 0
ans = [score[j] for j in range(len(score))]
score.sort(reverse = True)
while i < len(score):
if i == 0:
ans[ans.index(score[0])] = 'Gold Medal'
i += 1
... | relative-ranks | Python Solution | MS1301 | 0 | 112 | relative ranks | 506 | 0.592 | Easy | 8,917 |
https://leetcode.com/problems/relative-ranks/discuss/1884612/Python-easy-to-read-and-understand-or-sorting | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
d = collections.defaultdict(int)
sorted_score = sorted(score, reverse=True)
for i, val in enumerate(sorted_score):
d[val] = i+1
for i, val in enumerate(score):
if d[val] == 1:... | relative-ranks | Python easy to read and understand | sorting | sanial2001 | 0 | 70 | relative ranks | 506 | 0.592 | Easy | 8,918 |
https://leetcode.com/problems/relative-ranks/discuss/1697861/Python-or-Readable-Beginner-Friendly | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
n = len(score)
res = [""] * n
d = defaultdict(int)
for idx, val in enumerate(score):
d[val] = idx
count = 1
for val, idx in sorted(d.items(), reverse=Tru... | relative-ranks | Python | Readable Beginner Friendly | jgroszew | 0 | 62 | relative ranks | 506 | 0.592 | Easy | 8,919 |
https://leetcode.com/problems/relative-ranks/discuss/1643510/Pythonic-solution(beats-83) | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
top = {1: "Gold Medal", 2: "Silver Medal", 3: "Bronze Medal"} # storage of top-3 places
indexes = {val: index for index, val in enumerate(score)} # storage for initial indexes of elements
result = [0 for _ in range... | relative-ranks | Pythonic solution(beats 83%) | Dany_Sulimov | 0 | 86 | relative ranks | 506 | 0.592 | Easy | 8,920 |
https://leetcode.com/problems/relative-ranks/discuss/1424135/Python3-Faster-Than-92.03-Memory-Less-Than-88.68 | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
if len(score) == 1:
return ["Gold Medal"]
elif len(score) == 2:
if score[0] > score[1]:
return ["Gold Medal", "Silver Medal"]
else:
return... | relative-ranks | Python3 Faster Than 92.03%, Memory Less Than 88.68% | Hejita | 0 | 150 | relative ranks | 506 | 0.592 | Easy | 8,921 |
https://leetcode.com/problems/relative-ranks/discuss/1368556/Python3-Solution.-Runtime%3A-56-ms-faster-than-99.18 | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
hashmap = {}
s = sorted(score)[::-1]
for i in range(len(score)):
if i == 0:
hashmap[s[i]] = "Gold Medal"
elif i == 1:
hashmap[s[i]] = "Silver Medal"
... | relative-ranks | Python3 Solution. Runtime: 56 ms, faster than 99.18% | estetika | 0 | 123 | relative ranks | 506 | 0.592 | Easy | 8,922 |
https://leetcode.com/problems/relative-ranks/discuss/1356800/Easy-Python-Solution(99.33) | class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
g = sorted(score, reverse=True)
d={}
m=[]
print(g)
for i in range(len(g)):
if i == 0:
d[g[i]] = "Gold Medal"
elif i == 1:
d[g[i]] = "Silver Meda... | relative-ranks | Easy Python Solution(99.33%) | Sneh17029 | 0 | 336 | relative ranks | 506 | 0.592 | Easy | 8,923 |
https://leetcode.com/problems/relative-ranks/discuss/1153252/Python-easy-to-understand | class Solution:
def getRank(self, rank: int) -> str:
return {
1: 'Gold Medal',
2: 'Silver Medal',
3: 'Bronze Medal'
}.get(rank, str(rank))
def findRelativeRanks(self, score: List[int]) -> List[str]:
rank = {}
for a in sorted(score, reverse=Tru... | relative-ranks | Python easy to understand | 111110100 | 0 | 106 | relative ranks | 506 | 0.592 | Easy | 8,924 |
https://leetcode.com/problems/relative-ranks/discuss/977516/Easy-Understanding | class Solution:
def findRelativeRanks(self, nums: List[int]) -> List[str]:
index = {}
nums1 = [x for x in nums]
i = 1
nums1.sort(reverse = True)
for j in range(len(nums1)):
if nums1[j] in index:
continue
else:
if i == 1:... | relative-ranks | Easy Understanding | Aditya380 | 0 | 68 | relative ranks | 506 | 0.592 | Easy | 8,925 |
https://leetcode.com/problems/relative-ranks/discuss/760168/simple-of-simple | class Solution:
def findRelativeRanks(self, nums: List[int]) -> List[str]:
s = sorted(nums, reverse=True)
m = {}
medals = {
"1": "Gold Medal",
"2": "Silver Medal",
"3": "Bronze Medal"
}
for i in range(len(s)):
rnk = str... | relative-ranks | simple of simple | seunggabi | 0 | 48 | relative ranks | 506 | 0.592 | Easy | 8,926 |
https://leetcode.com/problems/relative-ranks/discuss/629311/Intuitive-approach-by-sorting-and-fill-rank-back-to-nums | class Solution:
def findRelativeRanks(self, nums: List[int]) -> List[str]:
awarded_medals = ["Gold Medal", "Silver Medal", "Bronze Medal"]
nums_with_indx = sorted([(i, s) for i, s in enumerate(nums)], key=lambda t: t[1], reverse=True)
for rank, t in enumerate(nums_with_indx):
pos... | relative-ranks | Intuitive approach by sorting and fill rank back to nums | puremonkey2001 | 0 | 50 | relative ranks | 506 | 0.592 | Easy | 8,927 |
https://leetcode.com/problems/perfect-number/discuss/1268856/Python3-simple-solution | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num == 1:
return False
res = 1
for i in range(2,int(num**0.5)+1):
if num%i == 0:
res += i + num//i
return res == num | perfect-number | Python3 simple solution | EklavyaJoshi | 10 | 439 | perfect number | 507 | 0.378 | Easy | 8,928 |
https://leetcode.com/problems/perfect-number/discuss/1014230/Python-one-liner-and-full-detail-solution-faster-than-99.58 | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
l=[]
for i in range(1,int(num**0.5)+1):
if num%i==0:
l.extend([i,num//i])
return sum(set(l))-num==num | perfect-number | Python one liner and full detail solution-faster than 99.58% | thisisakshat | 5 | 522 | perfect number | 507 | 0.378 | Easy | 8,929 |
https://leetcode.com/problems/perfect-number/discuss/1014230/Python-one-liner-and-full-detail-solution-faster-than-99.58 | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return sum(set(reduce(lambda i,j:i+j ,([i,num//i] for i in range(1,int(num**0.5)+1) if num%i==0))))-num==num | perfect-number | Python one liner and full detail solution-faster than 99.58% | thisisakshat | 5 | 522 | perfect number | 507 | 0.378 | Easy | 8,930 |
https://leetcode.com/problems/perfect-number/discuss/1573963/Euclid-Euler-Theorem-Fast-Solution!!!!-(Math-Solution) | class Solution:
def checkPerfectNumber(self, num):
primes = {2, 3, 5, 7, 13, 17, 19, 31}
for item in primes:
if (2**(item-1))*((2**item)-1) == num:
return True
return False | perfect-number | Euclid-Euler Theorem Fast Solution!!!! (Math Solution) | al102030 | 3 | 258 | perfect number | 507 | 0.378 | Easy | 8,931 |
https://leetcode.com/problems/perfect-number/discuss/1839323/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-75 | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return False if num==1 else num-1==sum([i+num//i for i in range(2,int(sqrt(num))+1) if num%i==0]) | perfect-number | 1-Line Python Solution || 60% Faster || Memory less than 75% | Taha-C | 1 | 182 | perfect number | 507 | 0.378 | Easy | 8,932 |
https://leetcode.com/problems/perfect-number/discuss/1839323/1-Line-Python-Solution-oror-60-Faster-oror-Memory-less-than-75 | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return num in (6, 28, 496, 8128, 33550336) | perfect-number | 1-Line Python Solution || 60% Faster || Memory less than 75% | Taha-C | 1 | 182 | perfect number | 507 | 0.378 | Easy | 8,933 |
https://leetcode.com/problems/perfect-number/discuss/1274473/O(1)-one-liner-solution | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return num in [6, 28, 496, 8128, 33_550_336, 8_589_869_056,
137_438_691_328, 2_305_843_008_139_952_128] | perfect-number | O(1) one-liner solution | denizen-ru | 1 | 110 | perfect number | 507 | 0.378 | Easy | 8,934 |
https://leetcode.com/problems/perfect-number/discuss/461489/Python3-simple-solution-faster-than-95.50 | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num <= 1: return False
res,sq=0,int(num**0.5)
for i in range(2,sq+1):
if num % i == 0:
res += i + num//i
res += 1
return res == num | perfect-number | Python3 simple solution, faster than 95.50% | jb07 | 1 | 200 | perfect number | 507 | 0.378 | Easy | 8,935 |
https://leetcode.com/problems/perfect-number/discuss/2722050/1-liner-kick-ass-(-PYTHON-) | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return sum(i + num//i for i in range(2, floor(num**0.5)+1) if num%i == 0) + 1 == num if num > 1 else False | perfect-number | 1-liner kick ass ( PYTHON ) | RandomNPC | 0 | 14 | perfect number | 507 | 0.378 | Easy | 8,936 |
https://leetcode.com/problems/perfect-number/discuss/2701275/Python3-oror-Simple-Solution-oror-O(sqrt(N))-oror-Easy-Understanding | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if(num == 1):
return False
sum = 1
root = int(sqrt(num)) + 1
for i in range(2,root):
if(num%i == 0):
sum += i + (num/i)
if(sum == num):
return True
retu... | perfect-number | Python3 || Simple Solution || O(sqrt(N)) || Easy Understanding | ahamedmusadiq_-12 | 0 | 10 | perfect number | 507 | 0.378 | Easy | 8,937 |
https://leetcode.com/problems/perfect-number/discuss/2676018/python-solution | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num == 1:
return False
s=int(math.sqrt(num))
ss = 1
for i in range(2,s+1):
if num % i == 0:
t = num // i
ss += t+i
if (ss) == num:
return (T... | perfect-number | python solution | vivekraj185 | 0 | 22 | perfect number | 507 | 0.378 | Easy | 8,938 |
https://leetcode.com/problems/perfect-number/discuss/1657193/Python3-math | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return num in (6, 28, 496, 8128, 33550336) | perfect-number | [Python3] math | ye15 | 0 | 153 | perfect number | 507 | 0.378 | Easy | 8,939 |
https://leetcode.com/problems/perfect-number/discuss/1657193/Python3-math | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
"""Euclid-Euler theorem"""
for p in 2,3,5,7,13:
if 2**(p-1) * (2**p - 1) == num: return True
return False | perfect-number | [Python3] math | ye15 | 0 | 153 | perfect number | 507 | 0.378 | Easy | 8,940 |
https://leetcode.com/problems/perfect-number/discuss/1657193/Python3-math | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
sm = 0
for x in range(1, int(sqrt(num))+1):
if num % x == 0:
sm += x
if num//x != x: sm += num//x
return sm == 2*num | perfect-number | [Python3] math | ye15 | 0 | 153 | perfect number | 507 | 0.378 | Easy | 8,941 |
https://leetcode.com/problems/perfect-number/discuss/1555758/97-without-%22cheating%22-but-still-o(sqrt(n))-for-primes | class Solution2:
def _get_all_divisors(self, num: int):
for divisor in range(1, int(num ** 0.5) + 1):
if num % divisor == 0:
yield divisor
other_divisor = num // divisor
if divisor != other_divisor:
yield other_divisor
def ... | perfect-number | 97% without "cheating", but still o(sqrt(n)) for primes | tamat | 0 | 264 | perfect number | 507 | 0.378 | Easy | 8,942 |
https://leetcode.com/problems/perfect-number/discuss/1597455/Python-Easy-Solution-or-Simple-Math | class Solution:
def checkPerfectNumber(self, n: int) -> bool:
res = []
ans = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
if n // i == i:
res.append(i)
else:
res.append(i)
ans.insert(0, n//i)
res.extend(ans)
res.pop()
return sum(res) == n | perfect-number | Python Easy Solution | Simple Math | leet_satyam | -1 | 184 | perfect number | 507 | 0.378 | Easy | 8,943 |
https://leetcode.com/problems/perfect-number/discuss/337820/Simple-Solution-in-Python-3 | class Solution:
def checkPerfectNumber(self, num: int) -> bool:
if num <= 1: return False
s, q = 0, num**.5
for i in range(2,1+int(q)):
if num%i == 0: s += i + num//i
if int(q) == q: s -= int(q)
return s == num - 1
- Python 3
- Junaid Mansuri | perfect-number | Simple Solution in Python 3 | junaidmansuri | -1 | 736 | perfect number | 507 | 0.378 | Easy | 8,944 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/1101640/A-Basic-Python | class Solution:
def findFrequentTreeSum(self, root: TreeNode) -> List[int]:
counts = collections.Counter()
def dfs(node):
if not node: return 0
result = node.val + dfs(node.left) + dfs(node.right)
counts[result] += 1
... | most-frequent-subtree-sum | A Basic Python | dev-josh | 1 | 117 | most frequent subtree sum | 508 | 0.644 | Medium | 8,945 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/863015/Python3 | class Solution:
def findFrequentTreeSum(self, root: TreeNode) -> List[int]:
def fn(node):
"""Populate frequency table."""
if not node: return 0
ans = node.val + fn(node.left) + fn(node.right)
freq[ans] = 1 + freq.get(ans, 0)
return ans
... | most-frequent-subtree-sum | [Python3] | ye15 | 1 | 55 | most frequent subtree sum | 508 | 0.644 | Medium | 8,946 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/2755784/python | class Solution:
def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
cnt = Counter()
def dfs(root):
if not root:
return 0
sums = root.val + dfs(root.left) + dfs(root.right)
cnt[sums] += 1
return sums
dfs(root)
... | most-frequent-subtree-sum | python | xy01 | 0 | 2 | most frequent subtree sum | 508 | 0.644 | Medium | 8,947 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/2597339/Python-95-Faster-Solution-easy-dfs-postorder-Traversal | class Solution:
def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
hashmap = defaultdict(int)
def dfs(root):
nonlocal hashmap
if not root:
return 0
left = ... | most-frequent-subtree-sum | Python 95% Faster Solution easy dfs postorder Traversal | abhisheksanwal745 | 0 | 21 | most frequent subtree sum | 508 | 0.644 | Medium | 8,948 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/2289758/Python3-Simple-solution-with-hash-table | class Solution:
def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
# Stores count of subtree sum
# hash_map[2] = 4 ==> substree sum 2 have frequency of 4
hash_map = {}
def subtree(root):
if not root:
return 0
... | most-frequent-subtree-sum | [Python3] Simple solution with hash table | Gp05 | 0 | 10 | most frequent subtree sum | 508 | 0.644 | Medium | 8,949 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/1566293/Easy-Approach-oror-Well-Explained-oror-Well-coded | class Solution:
def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
if root is None:
return []
dic = defaultdict(int)
def helper(root):
nonlocal dic
if root is None:
return 0
l = helper(root.left)
r = helper(root.right)
s = root.val+l+r
dic[s]+=1
return s
helper(... | most-frequent-subtree-sum | 📌📌 Easy-Approach || Well-Explained || Well-coded 🐍 | abhi9Rai | 0 | 63 | most frequent subtree sum | 508 | 0.644 | Medium | 8,950 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/1479911/Python-Clean-Postorder-DFS | class Solution:
def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
def postorder(node = root):
if not node:
return 0
L, R = postorder(node.left), postorder(node.right)
M = L+node.val+R
if M not in hashmap:
... | most-frequent-subtree-sum | [Python] Clean Postorder DFS | soma28 | 0 | 73 | most frequent subtree sum | 508 | 0.644 | Medium | 8,951 |
https://leetcode.com/problems/most-frequent-subtree-sum/discuss/1426123/Python3-Recursive-DFS-with-hashmap | class Solution:
def traversal(self, node, d, max_freq):
if node == None:
return 0
_sum = self.traversal(node.left, d, max_freq) + self.traversal(node.right, d, max_freq) + node.val
d[_sum] = d.get(_sum, 0) + 1
max_freq[0] = max(max_freq[0], d[_sum])
... | most-frequent-subtree-sum | [Python3] Recursive DFS with hashmap | maosipov11 | 0 | 66 | most frequent subtree sum | 508 | 0.644 | Medium | 8,952 |
https://leetcode.com/problems/fibonacci-number/discuss/336501/Solution-in-Python-3-(beats-~100)-(three-lines) | class Solution:
def fib(self, N: int) -> int:
a, b = 0, 1
for i in range(N): a, b = b, a + b
return a
- Python 3
- Junaid Mansuri | fibonacci-number | Solution in Python 3 (beats ~100%) (three lines) | junaidmansuri | 41 | 6,700 | fibonacci number | 509 | 0.692 | Easy | 8,953 |
https://leetcode.com/problems/fibonacci-number/discuss/2244130/Python-Simple-Python-Solution-Using-Dynamic-Programming | class Solution:
def fib(self, n: int) -> int:
dp = [0,1]
for i in range(2,n+1):
dp.append(dp[i-1] + dp[i-2])
return dp[n] | fibonacci-number | [Python] ✅✅ Simple Python Solution Using Dynamic Programming 🙏🥳 | ASHOK_KUMAR_MEGHVANSHI | 16 | 1,900 | fibonacci number | 509 | 0.692 | Easy | 8,954 |
https://leetcode.com/problems/fibonacci-number/discuss/2712402/Python-Classic-Fibonacci-Number-Problem-or-99-Faster-or-Fastest-Solution | class Solution:
def fib(self, n: int) -> int:
f0 = 0
f1 = 1
if n == 0 or n == 1:
return n
res = 0
for i in range(n-1):
res = f0+f1
f0 = f1
f1 = res
return res | fibonacci-number | ✔️ Python Classic Fibonacci Number Problem | 99% Faster | Fastest Solution | pniraj657 | 11 | 1,700 | fibonacci number | 509 | 0.692 | Easy | 8,955 |
https://leetcode.com/problems/fibonacci-number/discuss/2243600/Python-Simple-Recursive-and-Iterative-Solutions-with-Explanation | class Solution:
@cache
def fib(self, n: int) -> int:
return n if n <= 1 else self.fib(n-1)+self.fib(n-2) | fibonacci-number | [Python] Simple Recursive and Iterative Solutions with Explanation | zayne-siew | 4 | 711 | fibonacci number | 509 | 0.692 | Easy | 8,956 |
https://leetcode.com/problems/fibonacci-number/discuss/2243600/Python-Simple-Recursive-and-Iterative-Solutions-with-Explanation | class Solution:
def fib(self, n: int) -> int:
res, nxt = 0, 1
for _ in range(n):
res, nxt = nxt, res+nxt
return res | fibonacci-number | [Python] Simple Recursive and Iterative Solutions with Explanation | zayne-siew | 4 | 711 | fibonacci number | 509 | 0.692 | Easy | 8,957 |
https://leetcode.com/problems/fibonacci-number/discuss/253291/clean-python-solution-in-both-recursive-and-dp-bottom-up-approach | class Solution:
def fib(self, N: int) -> int:
if N == 0:
return 0
if N == 1 or N == 2:
return 1
else:
return self.fib(N - 1) + self.fib(N - 2) | fibonacci-number | clean python solution in both recursive and dp-bottom-up approach | mazheng | 4 | 525 | fibonacci number | 509 | 0.692 | Easy | 8,958 |
https://leetcode.com/problems/fibonacci-number/discuss/253291/clean-python-solution-in-both-recursive-and-dp-bottom-up-approach | class Solution:
def fib(self, N: int) -> int:
if N == 0:
return 0
memo = [0] * (N + 1)
return self.helper(N, memo)
def helper(self, N, memo):
if memo[N]:
return memo[N]
if N == 1 or N == 2:
result = 1
else:
result =... | fibonacci-number | clean python solution in both recursive and dp-bottom-up approach | mazheng | 4 | 525 | fibonacci number | 509 | 0.692 | Easy | 8,959 |
https://leetcode.com/problems/fibonacci-number/discuss/253291/clean-python-solution-in-both-recursive-and-dp-bottom-up-approach | class Solution:
def fib(self, N: int) -> int:
if N == 0:
return 0
if N == 1 or N == 2:
return 1
memo = [0] * (N + 1)
memo[1] = 1
memo[2] = 1
for i in range(3, N + 1):
memo[i] = memo[i - 1] + memo[i - 2]
return memo[N] | fibonacci-number | clean python solution in both recursive and dp-bottom-up approach | mazheng | 4 | 525 | fibonacci number | 509 | 0.692 | Easy | 8,960 |
https://leetcode.com/problems/fibonacci-number/discuss/253291/clean-python-solution-in-both-recursive-and-dp-bottom-up-approach | class Solution:
def fib(self, N: int) -> int:
if N == 0:
return 0
a = 1
b = 1
for i in range(3, N + 1):
c = a + b
a = b
b = c
return b | fibonacci-number | clean python solution in both recursive and dp-bottom-up approach | mazheng | 4 | 525 | fibonacci number | 509 | 0.692 | Easy | 8,961 |
https://leetcode.com/problems/fibonacci-number/discuss/2608774/Four-Different-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def fib(self, n):
if n == 0 or n == 1:
return n
return self.fib(n - 1) + self.fib(n - 2) | fibonacci-number | ✔Four Different Solution | Easy to Understand | Python | its_krish_here | 3 | 187 | fibonacci number | 509 | 0.692 | Easy | 8,962 |
https://leetcode.com/problems/fibonacci-number/discuss/2608774/Four-Different-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def fib(self, n):
if n == 0 or n == 1:
return n
dp = [0] * (n + 1)
dp[1] = 1
for i in range(2, n+1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n] | fibonacci-number | ✔Four Different Solution | Easy to Understand | Python | its_krish_here | 3 | 187 | fibonacci number | 509 | 0.692 | Easy | 8,963 |
https://leetcode.com/problems/fibonacci-number/discuss/2608774/Four-Different-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def solution(self, n, dp):
if n == 0 or n == 1:
return n
if dp[n] != 0:
return dp[n]
dp[n] = self.solution(n - 1, dp) + self.solution(n - 2, dp)
return dp[n]
def fib(self, n):
if n == 0 or n == 1:
return ... | fibonacci-number | ✔Four Different Solution | Easy to Understand | Python | its_krish_here | 3 | 187 | fibonacci number | 509 | 0.692 | Easy | 8,964 |
https://leetcode.com/problems/fibonacci-number/discuss/2608774/Four-Different-Solution-or-Easy-to-Understand-or-Python | class Solution(object):
def fib(self, n):
if n == 1 or n == 0:
return n
first = 0
second = 1
for i in range(2, n + 1):
max_ = first + second
first = second
second = max_
return second | fibonacci-number | ✔Four Different Solution | Easy to Understand | Python | its_krish_here | 3 | 187 | fibonacci number | 509 | 0.692 | Easy | 8,965 |
https://leetcode.com/problems/fibonacci-number/discuss/1645885/Python-1-liner-beats-100-using-general-term | class Solution:
def fib(self, n: int) -> int:
return int((((1 + math.sqrt(5)) / 2) ** n - ((1 - math.sqrt(5)) / 2) ** n) / math.sqrt(5)) | fibonacci-number | Python, 1-liner, beats 100%, using general term | kryuki | 3 | 239 | fibonacci number | 509 | 0.692 | Easy | 8,966 |
https://leetcode.com/problems/fibonacci-number/discuss/2513811/Easy-oror-0-ms-oror-100-oror-DP-oror-Recursive-oror-Java-C%2B%2B-Python-JS-C-Python3-oror-No-Need-To-Explain | class Solution(object):
def fib(self, n):
x, y = 0, 1
for _ in range(n):
x, y = y, x+y
return x | fibonacci-number | Easy || 0 ms || 100% || DP || Recursive || Java, C++, Python, JS, C, Python3 || No Need To Explain | PratikSen07 | 2 | 289 | fibonacci number | 509 | 0.692 | Easy | 8,967 |
https://leetcode.com/problems/fibonacci-number/discuss/2513811/Easy-oror-0-ms-oror-100-oror-DP-oror-Recursive-oror-Java-C%2B%2B-Python-JS-C-Python3-oror-No-Need-To-Explain | class Solution:
def fib(self, n: int) -> int:
if n < 2: return n
return self.fib(n-1) + self.fib(n-2); | fibonacci-number | Easy || 0 ms || 100% || DP || Recursive || Java, C++, Python, JS, C, Python3 || No Need To Explain | PratikSen07 | 2 | 289 | fibonacci number | 509 | 0.692 | Easy | 8,968 |
https://leetcode.com/problems/fibonacci-number/discuss/2375300/Easy-And-Fast-Fibonacci-Solutions-With-and-Without-Dynamic-Programming | class Solution:
def fib(self, n: int) -> int:
if n == 0 or n == 1: return n
a = [0, 1]
for i in range(1, n):
temp = a[0]
a[0] = a[1]
a[1] = a[0] + temp
return a[1] | fibonacci-number | Easy And Fast Fibonacci Solutions With and Without Dynamic Programming | zip_demons | 2 | 145 | fibonacci number | 509 | 0.692 | Easy | 8,969 |
https://leetcode.com/problems/fibonacci-number/discuss/2375300/Easy-And-Fast-Fibonacci-Solutions-With-and-Without-Dynamic-Programming | class Solution:
def fib(self, n: int) -> int:
if n == 0 or n == 1: return n
return self.fib(n-1) + self.fib(n-2) | fibonacci-number | Easy And Fast Fibonacci Solutions With and Without Dynamic Programming | zip_demons | 2 | 145 | fibonacci number | 509 | 0.692 | Easy | 8,970 |
https://leetcode.com/problems/fibonacci-number/discuss/2798132/Binet's-Formula-for-larger-values-faster-than-all-others-and-least-space-possible | class Solution:
def fib(self, n: int) -> int:
# base cases to consider
if n == 0 :
return 0
elif 0 < n <= 2 :
return int(1)
# binet's formula for higher values
else :
# binet's formula is
# ((1+sqrt(5))/2)^n - ((1-sqrt5)/... | fibonacci-number | Binet's Formula for larger values, faster than all others and least space possible | laichbr | 1 | 5 | fibonacci number | 509 | 0.692 | Easy | 8,971 |
https://leetcode.com/problems/fibonacci-number/discuss/2435942/RUNTIME%3A%3A-50-MS-faster-than-57-MEMORY-USAGE%3A%3A-13.8-MB-less-than-95.72 | class Solution:
def fib(self, n: int) -> int:
if n==0: return 0
elif n==1: return 1
else:
l=[1,1]
for i in range(2,n+1):
l.append(l[i-1]+l[i-2])
return l[n-1] | fibonacci-number | RUNTIME:: 50 MS faster than 57% MEMORY USAGE:: 13.8 MB less than 95.72% | keertika27 | 1 | 47 | fibonacci number | 509 | 0.692 | Easy | 8,972 |
https://leetcode.com/problems/fibonacci-number/discuss/1967828/Python-recursive-and-iterative-solutions | class Solution:
def fib(self, n: int, fibs: dict[int, int] = {}) -> int:
if n <= 1:
return n
if n in fibs:
return fibs[n]
result = self.fib(n - 1, fibs) + self.fib(n - 2, fibs)
fibs[n] = result
return result | fibonacci-number | Python recursive and iterative solutions | user3694B | 1 | 100 | fibonacci number | 509 | 0.692 | Easy | 8,973 |
https://leetcode.com/problems/fibonacci-number/discuss/1967828/Python-recursive-and-iterative-solutions | class Solution:
def fib(self, n: int) -> int:
fib_first = 0
fib_second = 1
while n > 0:
fib_first, fib_second = fib_second, fib_first + fib_second
n -= 1
return fib_first | fibonacci-number | Python recursive and iterative solutions | user3694B | 1 | 100 | fibonacci number | 509 | 0.692 | Easy | 8,974 |
https://leetcode.com/problems/fibonacci-number/discuss/1913356/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def fib(self, n):
return n if n < 2 else self.fib(n-1) + self.fib(n-2) | fibonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 1 | 98 | fibonacci number | 509 | 0.692 | Easy | 8,975 |
https://leetcode.com/problems/fibonacci-number/discuss/1913356/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
@cache
def fib(self, n):
return n if n < 2 else self.fib(n-1) + self.fib(n-2) | fibonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 1 | 98 | fibonacci number | 509 | 0.692 | Easy | 8,976 |
https://leetcode.com/problems/fibonacci-number/discuss/1913356/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def fib(self, n):
q = deque([0,1], maxlen=2)
for i in range(n): q.append(q.popleft()+q[-1])
return q.popleft() | fibonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 1 | 98 | fibonacci number | 509 | 0.692 | Easy | 8,977 |
https://leetcode.com/problems/fibonacci-number/discuss/1913356/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def fib(self, n):
a, b = 0, 1
for i in range(n):
a, b = b, a + b
return a | fibonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 1 | 98 | fibonacci number | 509 | 0.692 | Easy | 8,978 |
https://leetcode.com/problems/fibonacci-number/discuss/1913356/Python-Multiple-Solutions-Clean-and-Simple! | class Solution:
def fib(self, n):
return reduce(lambda x,_:(x[1],x[0]+x[1]), range(n), (0, 1))[0] | fibonacci-number | Python - Multiple Solutions - Clean and Simple! | domthedeveloper | 1 | 98 | fibonacci number | 509 | 0.692 | Easy | 8,979 |
https://leetcode.com/problems/fibonacci-number/discuss/1846016/Easy-Python-O(1)-Space-Complexity-Better-than-99.87 | class Solution:
def fib(self, n: int) -> int:
if n <= 1:
return n
fibo_array = [0, 1]
for i in range(2, n+1):
num = sum(fibo_array)
fibo_array[0] = fibo_array[1]
fibo_array[1] = num
return fibo_array[-1] | fibonacci-number | Easy Python O(1) Space Complexity Better than 99.87% | Math_Prandini | 1 | 117 | fibonacci number | 509 | 0.692 | Easy | 8,980 |
https://leetcode.com/problems/fibonacci-number/discuss/1815493/Python3-or-Bottom-up-Approach | class Solution:
def fib(self, n: int) -> int:
if n<1: return 0
c=[0,1]
i=1
while i<n:
c.append(c[-1]+c[-2])
i+=1
return c[-1] | fibonacci-number | Python3 | Bottom-up Approach | Anilchouhan181 | 1 | 131 | fibonacci number | 509 | 0.692 | Easy | 8,981 |
https://leetcode.com/problems/fibonacci-number/discuss/1441001/Python-solution | class Solution:
def fib(self, n: int) -> int:
if n==0:
return 0;
if n==1:
return 1;
return self.fib(n-1)+self.fib(n-2); | fibonacci-number | Python solution | Pranav447 | 1 | 249 | fibonacci number | 509 | 0.692 | Easy | 8,982 |
https://leetcode.com/problems/fibonacci-number/discuss/1342349/Easy-Memoization-in-Python.-Faster-than-96 | class Solution:
def fib(self, n: int, memo={}) -> int:
if n in memo:
return memo[n]
if (1<= n <=2):
return 1
if n== 0:
return 0
memo[n] = self.fib(n-1, memo) + self.fib(n-2, memo)
return memo[n] | fibonacci-number | Easy Memoization in Python. Faster than 96% | pranshusharma712 | 1 | 363 | fibonacci number | 509 | 0.692 | Easy | 8,983 |
https://leetcode.com/problems/fibonacci-number/discuss/682541/Python3-top-down-dp | class Solution:
def fib(self, N: int) -> int:
@lru_cache(None)
def fn(i):
"""Return ith Fibonacci number"""
if i < 2: return i #boundary condition
return fn(i-1) + fn(i-2)
return fn(N) | fibonacci-number | [Python3] top-down dp | ye15 | 1 | 83 | fibonacci number | 509 | 0.692 | Easy | 8,984 |
https://leetcode.com/problems/fibonacci-number/discuss/682541/Python3-top-down-dp | class Solution:
def fib(self, n: int) -> int:
f0, f1 = 0, 1
for _ in range(n): f0, f1 = f1, f0+f1
return f0 | fibonacci-number | [Python3] top-down dp | ye15 | 1 | 83 | fibonacci number | 509 | 0.692 | Easy | 8,985 |
https://leetcode.com/problems/fibonacci-number/discuss/682541/Python3-top-down-dp | class Solution:
def fib(self, n: int) -> int:
phi = (1 + sqrt(5))/2 # golden ratio
return round((phi**n - (1-phi)**n)/sqrt(5)) | fibonacci-number | [Python3] top-down dp | ye15 | 1 | 83 | fibonacci number | 509 | 0.692 | Easy | 8,986 |
https://leetcode.com/problems/fibonacci-number/discuss/682541/Python3-top-down-dp | class Solution:
def fib(self, n: int) -> int:
phi = (1 + sqrt(5))/2 # golden ratio
return round(phi**n/sqrt(5)) | fibonacci-number | [Python3] top-down dp | ye15 | 1 | 83 | fibonacci number | 509 | 0.692 | Easy | 8,987 |
https://leetcode.com/problems/fibonacci-number/discuss/682541/Python3-top-down-dp | class Solution:
def fib(self, n: int) -> int:
def fn(n):
"""Return nth and (n+1)st Fibonacci numbers via fast doubling method."""
if n == 0: return [0, 1]
x, y = fn(n >> 1)
xx = x * (2* y - x)
yy = x*x + y*y
return [yy, xx+yy... | fibonacci-number | [Python3] top-down dp | ye15 | 1 | 83 | fibonacci number | 509 | 0.692 | Easy | 8,988 |
https://leetcode.com/problems/fibonacci-number/discuss/346689/32ms-Python3-Tabulation-DP | class Solution:
def fib(self, n):
if n == 0:
return 0
f = [0]*(n+1)
f[1] = 1
for i in range(2, n+1):
f[i] = f[i-1] + f[i-2]
return f[n] | fibonacci-number | 32ms Python3 Tabulation DP | TCarmic | 1 | 116 | fibonacci number | 509 | 0.692 | Easy | 8,989 |
https://leetcode.com/problems/fibonacci-number/discuss/2847693/Python-Dynamic-programming-beats-97.92-runtime-and-96.23-mem-usage | class Solution:
def fib(self, n: int) -> int:
i, j = 0, 1
while n:
i, j = j, i + j
n -= 1
return i | fibonacci-number | Python Dynamic programming beats 97.92% runtime & 96.23% mem usage | Molot84 | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 8,990 |
https://leetcode.com/problems/fibonacci-number/discuss/2846389/Python-oror-34ms | class Solution:
def fib(self, n: int) -> int:
if n == 0:
return 0
fibo = [0, 1]
for i in range(n - 1):
fibo.append(fibo[i] + fibo[i+1])
return fibo[-1] | fibonacci-number | Python || 34ms | amarildoaliaj | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 8,991 |
https://leetcode.com/problems/fibonacci-number/discuss/2844562/Python3-One-line-using-the-golden-ratio-(Faster-than-100) | class Solution:
def fib(self, n: int) -> int:
return round((1.618034**n - (1-1.618034)**n) / math.sqrt(5)) | fibonacci-number | [Python3] One line using the golden ratio (Faster than 100%) | nguyentangnhathuy | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 8,992 |
https://leetcode.com/problems/fibonacci-number/discuss/2842037/Python-Solution | class Solution:
def fib(self, n: int) -> int:
f1, f2 = 0, 1
for _ in range(n):
f1, f2 = f2, f1 + f2
return f1 | fibonacci-number | Python Solution | LaggerKrd | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 8,993 |
https://leetcode.com/problems/fibonacci-number/discuss/2841326/Most-Optimised-solution-using-DP-technique | class Solution:
def fib(self, n: int) -> int:
if n <= 1:
return n
prev1, prev2 = 1, 0 # Time : O(n) ,Space : O(1)
for _ in range(2, n+1):
curr = prev1 + prev2
prev2 = prev1
prev1 = curr
return curr | fibonacci-number | Most Optimised solution using DP technique | godabauday | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 8,994 |
https://leetcode.com/problems/fibonacci-number/discuss/2836086/Python-DP-4-lines | class Solution:
def fib(self, n: int) -> int:
table = [0,1]
for i in range(2,n+1):
table.append(table[i-1] + table[i-2])
return table[n] | fibonacci-number | Python DP 4 lines | kevinpan21 | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 8,995 |
https://leetcode.com/problems/fibonacci-number/discuss/2821964/Python-Neet-and-natural-solution-with-2-variables | class Solution:
def fib(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
c = a + b
a, b = b, c
return a | fibonacci-number | [Python] Neet and natural solution with 2 variables | Nezuko-NoBamboo | 0 | 1 | fibonacci number | 509 | 0.692 | Easy | 8,996 |
https://leetcode.com/problems/fibonacci-number/discuss/2818995/Linear-(O)n-Simple-solution | class Solution:
def fib(self, n: int) -> int:
a, b = 0, 1
while n:
a, b = b, a + b
n -= 1
return a | fibonacci-number | Linear (O)n Simple solution | halcyon_abraham | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 8,997 |
https://leetcode.com/problems/fibonacci-number/discuss/2815262/Fibonacci-Number-(Top-Down-Dynamic-Programming) | class Solution:
def fib(self, N: int) -> int:
arr = [-1 for i in range(N+1)]
def solve(N, arr):
if N == 0 or N == 1:
return N
if arr[N] != -1:
return arr[N]
arr[N] = solve(N-1,arr) + solve(N-2,arr)
... | fibonacci-number | Fibonacci Number (Top Down Dynamic Programming) | sarthakchawande14 | 0 | 3 | fibonacci number | 509 | 0.692 | Easy | 8,998 |
https://leetcode.com/problems/fibonacci-number/discuss/2814575/Simple-Python-Solution%3A-TC-greater-O(N)-and-SC-greater-O(1) | class Solution:
#tc = O(N)
#sc = O(1)
def fib(self, n: int) -> int:
if n == 0 or n == 1:
return n
prev1 = 1
prev2 = 0
for i in range(2,n+1):
cur = prev1 + prev2
prev2 = prev1
prev1 = cur
return prev1 | fibonacci-number | Simple Python Solution: TC -> O(N) and SC -> O(1) | MaviOp | 0 | 2 | fibonacci number | 509 | 0.692 | Easy | 8,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.