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/queue-reconstruction-by-height/discuss/2665310/Python-O(N2)-O(1) | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: x[1])
people.sort(key=lambda x: x[0], reverse=True)
res = []
for h1, k in people:
res.insert(k, (h1, k))
return res | queue-reconstruction-by-height | Python - O(N^2), O(1) | Teecha13 | 0 | 6 | queue reconstruction by height | 406 | 0.728 | Medium | 7,100 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2509953/Best-Python3-implementation-oror-Very-simple | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
n = len(people)
people.sort()
ans = [[]]*n
i = 0
while people:
h,p = people.pop(0)
count= p
for i in range(n):
if count== 0 and ans[i] == []:
ans[i] = [h,p]
break
elif not ans[i] or (ans[i] and... | queue-reconstruction-by-height | ✔️ Best Python3 implementation || Very simple | UpperNoot | 0 | 62 | queue reconstruction by height | 406 | 0.728 | Medium | 7,101 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2215448/Python-ez-solution | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key = lambda x: (-x[0], x[1]))
output = []
for p in people:
output.insert(p[1], p)
return output | queue-reconstruction-by-height | Python ez solution | KOJI_LIU | 0 | 7 | queue reconstruction by height | 406 | 0.728 | Medium | 7,102 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2214973/Python-solution-or-Queue-Reconstruction-by-Height | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key = lambda x : (-x[0], x[1]))
res = []
for x in people:
res.insert(x[1],x)
return res | queue-reconstruction-by-height | Python solution | Queue Reconstruction by Height | nishanrahman1994 | 0 | 22 | queue reconstruction by height | 406 | 0.728 | Medium | 7,103 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2214860/Python-O(N2) | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
result = []
for p in sorted(people, key=lambda p: (-p[0], p[1])) :
result.insert(p[1], p)
return result | queue-reconstruction-by-height | Python O(N^2) | blue_sky5 | 0 | 3 | queue reconstruction by height | 406 | 0.728 | Medium | 7,104 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2213177/Custom-Sorting-oror-Simplest-Approach | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
peoples = sorted(people, key=lambda x : [-x[0], x[1]])
n = len(peoples)
reconstructedQueue = []
for people in peoples:
reconstructedQueue.insert(people[1], people)
... | queue-reconstruction-by-height | Custom Sorting || Simplest Approach | Vaibhav7860 | 0 | 27 | queue reconstruction by height | 406 | 0.728 | Medium | 7,105 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2212204/Not-Optimized-oror-Brute-Force | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort()
N=len(people)
ans=[None]*N
for height,q in people:
i,j=0,-1
while i<N:
if not ans[i] or ans[i][0]==height:
j+=1
... | queue-reconstruction-by-height | Not Optimized || Brute Force | chaurasiya_g | 0 | 5 | queue reconstruction by height | 406 | 0.728 | Medium | 7,106 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2211904/Python-easy-within-5-lines | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
output=[]
people.sort(key=lambda x: (-x[0], x[1]))
for a in people:
output.insert(a[1], a)
return output | queue-reconstruction-by-height | Python easy within 5 lines | shivam-rathod | 0 | 38 | queue reconstruction by height | 406 | 0.728 | Medium | 7,107 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2211816/O(n2)-solution-that-is-different-than-everyone-else's-insertsort-solution | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort()
res = [None for _ in range(len(people))]
for height, count in people:
tmp = 0
for i in range(len(res)):
if count == tmp and res[i] is None:
... | queue-reconstruction-by-height | O(n^2) solution that is different than everyone else's insert/sort solution | sicp_rush | 0 | 5 | queue reconstruction by height | 406 | 0.728 | Medium | 7,108 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/2079884/Python-or-Sorting | class Solution:
def reconstructQueue(self, pe: List[List[int]]) -> List[List[int]]:
l = [[-1,-1] for i in range(len(pe))]
pe.sort()
for i in range(len(pe)):
c = pe[i][1]
for j in range(len(pe)):
if c>0:
if l[j][0] != -1:
... | queue-reconstruction-by-height | Python | Sorting | Shivamk09 | 0 | 56 | queue reconstruction by height | 406 | 0.728 | Medium | 7,109 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/1613647/Pythonic-solution-with-comments | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: (-x[0], x[1])) # here we sort given sequence: firstly, we're looking at the person's height, then we compare by people in front of
final_queue = [] # variable where we're goi... | queue-reconstruction-by-height | Pythonic solution with comments | Dany_Sulimov | 0 | 71 | queue reconstruction by height | 406 | 0.728 | Medium | 7,110 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/1613162/Python3-Solution-with-using-sorting | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: (-x[0], x[1]))
res = []
for p in people:
res.insert(p[1], p)
return res | queue-reconstruction-by-height | [Python3] Solution with using sorting | maosipov11 | 0 | 42 | queue reconstruction by height | 406 | 0.728 | Medium | 7,111 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/1331472/Python-3-segment-tree-or-O(n*log2(n))-T-or-O(n)-S | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
import math
result = [None]*len(people)
# initialize segment tree
# leafs is len((0, 1, 1, ...)) = len(people)
# sum(0, i) is how many empties positions in [0, i-1]
... | queue-reconstruction-by-height | Python 3 segment tree | O(n*log^2(n)) T | O(n) S | CiFFiRO | 0 | 105 | queue reconstruction by height | 406 | 0.728 | Medium | 7,112 |
https://leetcode.com/problems/queue-reconstruction-by-height/discuss/673145/Small-Python3-Solution%3A-O(n2)-Time-and-O(n)-Space | class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
res = []
# Sort the array, with descending order for height and ascending order for the number of taller persons ahead
people_sorted = sorted(people, key=lambda p: (p[0], -p[1]), reverse=True)
# Add persons t... | queue-reconstruction-by-height | Small Python3 Solution: O(n^2) Time and O(n) Space | schedutron | 0 | 81 | queue reconstruction by height | 406 | 0.728 | Medium | 7,113 |
https://leetcode.com/problems/trapping-rain-water-ii/discuss/1138028/Python3Visualization-BFS-Solution-With-Explanation | class Solution:
def trapRainWater(self, heightMap: List[List[int]]) -> int:
if not heightMap or not heightMap[0]:
return 0
# Initial
# Board cells cannot trap the water
m, n = len(heightMap), len(heightMap[0])
if m < 3 or n < 3:
return 0
# Add Bo... | trapping-rain-water-ii | [Python3][Visualization] BFS Solution With Explanation | Picassos_Shoes | 209 | 4,700 | trapping rain water ii | 407 | 0.475 | Hard | 7,114 |
https://leetcode.com/problems/trapping-rain-water-ii/discuss/2594894/Share-my-novel-solution-with-horizontal-scanning-(No-heap-used) | class Solution:
def trapRainWater(self, heightMap: List[List[int]]) -> int:
m, n = len(heightMap), len(heightMap[0])
if m < 3 or n < 3: return 0
# to simplify the code
def adjacent(i,j):
return [(i-1,j), (i+1,j), (i,j-1), (i,j+1)]
# first we sort all heig... | trapping-rain-water-ii | Share my novel solution with horizontal scanning (No heap used) | SquirrelRay | 0 | 33 | trapping rain water ii | 407 | 0.475 | Hard | 7,115 |
https://leetcode.com/problems/trapping-rain-water-ii/discuss/1947461/Python3-or-Priority-Queue-or-Binary-Search-Tree | class Solution:
def __init__(self):
self.visited=dict()
self.length=0
self.width=0
def minPriorityQueueBSTAdd(self, minPriorityQueue, value):
length=len(minPriorityQueue)
start=0
end=length-1
while start<=end:
mid=(start+end)//2
if minPriorityQueue[mid][0]>value[0]:
end=mid-1
else:
sta... | trapping-rain-water-ii | Python3 | Priority Queue | Binary Search Tree | rajoriyas | 0 | 139 | trapping rain water ii | 407 | 0.475 | Hard | 7,116 |
https://leetcode.com/problems/trapping-rain-water-ii/discuss/1922157/Python3-easy-to-read-and-understand-or-heapq-or-2-solutions | class Solution:
def trapRainWater(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
visited = [[False for _ in range(n)] for _ in range(m)]
pq = []
for i in range(m):
visited[i][0] = True
heapq.heappush(pq, (grid[i][0], i, 0))
... | trapping-rain-water-ii | Python3 easy to read and understand | heapq | 2 solutions | sanial2001 | 0 | 243 | trapping rain water ii | 407 | 0.475 | Hard | 7,117 |
https://leetcode.com/problems/trapping-rain-water-ii/discuss/1922157/Python3-easy-to-read-and-understand-or-heapq-or-2-solutions | class Solution:
def trapRainWater(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
visited = set()
pq = []
for i in range(m):
visited.add((i, 0))
heapq.heappush(pq, (grid[i][0], i, 0))
visited.add((i, n-1))
h... | trapping-rain-water-ii | Python3 easy to read and understand | heapq | 2 solutions | sanial2001 | 0 | 243 | trapping rain water ii | 407 | 0.475 | Hard | 7,118 |
https://leetcode.com/problems/trapping-rain-water-ii/discuss/1491886/Python3-priority-queue | class Solution:
def trapRainWater(self, heightMap: List[List[int]]) -> int:
m, n = len(heightMap), len(heightMap[0])
pq = []
for i in range(m):
heappush(pq, (heightMap[i][0], i, 0))
heappush(pq, (heightMap[i][n-1], i, n-1))
for j in range(1, n-1):
... | trapping-rain-water-ii | [Python3] priority queue | ye15 | 0 | 119 | trapping rain water ii | 407 | 0.475 | Hard | 7,119 |
https://leetcode.com/problems/longest-palindrome/discuss/2221045/Python-oror-counter-oror-explanation | class Solution:
def longestPalindrome(self, s: str) -> int:
oddFlag=0
count=collections.Counter(s)
ans=0
for k,v in count.items():
if v%2==1:
ans+=v-1
oddFlag= 1
else:
ans+=v
if... | longest-palindrome | Python || counter || explanation | palashbajpai214 | 9 | 465 | longest palindrome | 409 | 0.547 | Easy | 7,120 |
https://leetcode.com/problems/longest-palindrome/discuss/2832908/Python-Hash-Table-faster-than-99.88 | class Solution:
def longestPalindrome(self, s: str) -> int:
count = {} # Hash Table
ans = [] # every word's frequency
odd= 0 # store an odd number's frequency
for word in s:
if word not in count:
count[word] = 1
else:
... | longest-palindrome | [Python] Hash Table faster than 99.88% | isu10903027A | 3 | 242 | longest palindrome | 409 | 0.547 | Easy | 7,121 |
https://leetcode.com/problems/longest-palindrome/discuss/1847975/Python-easy-to-read-and-understand-or-set | class Solution:
def longestPalindrome(self, s: str) -> int:
t = set()
for i in s:
if i in t:
t.remove(i)
else:
t.add(i)
if len(t) == 0:
return len(s)
else:
return len(s)-len(t) + 1 | longest-palindrome | Python easy to read and understand | set | sanial2001 | 3 | 242 | longest palindrome | 409 | 0.547 | Easy | 7,122 |
https://leetcode.com/problems/longest-palindrome/discuss/1798443/Python-Simple-Solution | class Solution:
def longestPalindrome(self, s: str) -> int:
s = Counter(s)
e = 0
ss = 0
for i in s.values():
if i%2==0:
ss+=i
else:
ss += i-1
if e==0:
e=1
return ss + e | longest-palindrome | [Python] Simple Solution | zouhair11elhadi | 3 | 280 | longest palindrome | 409 | 0.547 | Easy | 7,123 |
https://leetcode.com/problems/longest-palindrome/discuss/2630057/Python-solution-using-dictionary | class Solution:
def longestPalindrome(self, s: str) -> int:
dic ={}
res = 0
odd = False
for i in s:
dic.update({i:s.count(i)})
for key,value in dic.items():
if value%2==0:
res += value
else:
... | longest-palindrome | Python solution using dictionary | Ashwinpillai9 | 1 | 330 | longest palindrome | 409 | 0.547 | Easy | 7,124 |
https://leetcode.com/problems/longest-palindrome/discuss/2413593/JAVA-PYTHON3-C%2B%2B-Easy-Solution | class Solution:
def longestPalindrome(self, s: str) -> int:
counts = {}
for c in s: counts[c] = counts.get(c, 0) + 1
result, odd_found = 0, False
for _, c in counts.items():
if c % 2 == 0: result += c
else:
odd_found = True
... | longest-palindrome | [JAVA, PYTHON3, C++] Easy Solution | WhiteBeardPirate | 1 | 76 | longest palindrome | 409 | 0.547 | Easy | 7,125 |
https://leetcode.com/problems/longest-palindrome/discuss/2208899/Python3-Runtime%3A-40ms-76.32-memory%3A-14mb-21.11 | class Solution:
# Runtime: 40ms 76.32% memory: 14mb 21.11%
# O(n) || O(1); we are dealing with only 26 letters of english lowercase and 26 letters of english uppercase, or say it O(k) where k is the number of english alphabets that would be store.
def longestPalindrome(self, s: str) -> int:
hashMap = d... | longest-palindrome | Python3 Runtime: 40ms 76.32% memory: 14mb 21.11% | arshergon | 1 | 92 | longest palindrome | 409 | 0.547 | Easy | 7,126 |
https://leetcode.com/problems/longest-palindrome/discuss/1643107/Easy-Python-or-90-Speed-or-Two-Liner | class Solution:
def longestPalindrome(self, s):
C = Counter(s)
return (sum( (v>>1) for v in C.values() )<<1) + any( v%2 for v in C.values() ) | longest-palindrome | Easy Python | 90% Speed | Two-Liner | Aragorn_ | 1 | 245 | longest palindrome | 409 | 0.547 | Easy | 7,127 |
https://leetcode.com/problems/longest-palindrome/discuss/1188518/Super-Simple-Python-Solution-with-Comments | class Solution:
def longestPalindrome(self, s: str) -> int:
res = 0
# Initialize Frequency counter
freqDict = defaultdict(int)
for char in s:
freqDict[char] += 1
# Add the largest even number in each frequency to the result to e... | longest-palindrome | Super Simple Python Solution with Comments | kevinvle1997 | 1 | 223 | longest palindrome | 409 | 0.547 | Easy | 7,128 |
https://leetcode.com/problems/longest-palindrome/discuss/792730/Python-Simple-to-understand-and-implement | class Solution:
def longestPalindrome(self, s: str) -> int:
counts = collections.Counter(s)
tot = 0
odd = False
for freq in counts.values():
if freq % 2 != 0: # if odd number, set flag
odd = True
tot += freq... | longest-palindrome | Python - Simple to understand and implement | vdhyani96 | 1 | 60 | longest palindrome | 409 | 0.547 | Easy | 7,129 |
https://leetcode.com/problems/longest-palindrome/discuss/342995/Solution-in-Python-3 | class Solution:
def longestPalindrome(self, s: str) -> int:
D = {}
for c in s:
if c in D:
D[c] += 1
else:
D[c] = 1
L = D.values()
E = len([i for i in L if i % 2 == 1])
return sum(L) - E + (E > 0)
- Python 3
- Junaid Mansuri | longest-palindrome | Solution in Python 3 | junaidmansuri | 1 | 771 | longest palindrome | 409 | 0.547 | Easy | 7,130 |
https://leetcode.com/problems/longest-palindrome/discuss/2843409/4-lines-of-code-Python-faster-than-99.12.-Hash-map-and-bitwise | class Solution:
def longestPalindrome(self, s: str) -> int:
sum, map = 0, {}
for ch in s: map[ch] = map.get(ch, 0) + 1 # frequency hash map
for f in map.values(): sum += f - (f & 1) # sum up frequencies, subtract one for each odd
return min(sum + 1, len(s)) # off by one error cor... | longest-palindrome | 4 lines of code Python, faster than 99.12%. Hash map and bitwise | JacobSulewski | 0 | 1 | longest palindrome | 409 | 0.547 | Easy | 7,131 |
https://leetcode.com/problems/longest-palindrome/discuss/2839570/Simple-Python-Solution | class Solution:
def longestPalindrome(self, s: str) -> int:
dic = Counter(s)
even = 0
odd = 0
for k in dic.keys():
if dic[k]%2 == 1:
odd += 1
even += dic[k] - 1
else:
even += dic[k]
if odd:
re... | longest-palindrome | Simple Python Solution | ajayedupuganti18 | 0 | 1 | longest palindrome | 409 | 0.547 | Easy | 7,132 |
https://leetcode.com/problems/longest-palindrome/discuss/2829464/Longest-Palindrome-or-Optimum-Solution-in-Python | class Solution:
def longestPalindrome(self, s: str) -> int:
res = 0
for i in collections.Counter(s).values():
res += i // 2 * 2
return min(res+1, len(s)) | longest-palindrome | Longest Palindrome | Optimum Solution in Python | jashii96 | 0 | 5 | longest palindrome | 409 | 0.547 | Easy | 7,133 |
https://leetcode.com/problems/longest-palindrome/discuss/2829404/Easy-python-solution | class Solution:
def longestPalindrome(self, s: str) -> int:
pair = set()
ans = 0
for i in s:
if i in pair:
ans += 1
pair.remove(i)
else:
pair.add(i)
return ans*2 + 1 if len(pair)>0 else ans*2 | longest-palindrome | Easy python solution | _debanjan_10 | 0 | 3 | longest palindrome | 409 | 0.547 | Easy | 7,134 |
https://leetcode.com/problems/longest-palindrome/discuss/2827108/Python-solution-or-easy-to-understand | class Solution:
def longestPalindrome(self, s: str) -> int:
counter = {}
res = 0
remains = 0
for chr in s:
counter[chr] = counter.get(chr, 0) + 1
for val in counter.values():
remains += val % 2
res += val - val % 2
... | longest-palindrome | Python solution | easy to understand | LaggerKrd | 0 | 4 | longest palindrome | 409 | 0.547 | Easy | 7,135 |
https://leetcode.com/problems/longest-palindrome/discuss/2826245/finding-pairs-of-alphabet | class Solution:
def longestPalindrome(self, s: str) -> int:
sorted_s = sorted(s)
sorted_s.append(0)
ans , i = 0 , 0
flag = True
while i < len(sorted_s)-1:
if sorted_s[i] == sorted_s[i+1]:
ans += 2
i +=2
else:
... | longest-palindrome | finding pairs of alphabet | roger880327 | 0 | 1 | longest palindrome | 409 | 0.547 | Easy | 7,136 |
https://leetcode.com/problems/longest-palindrome/discuss/2824010/How-can-this-beat-over-90 | class Solution:
def longestPalindrome(self, s: str) -> int:
nums = dict()
for l in s:
if not l in nums:
nums[l] = 1
else:
nums[l] += 1
res = 0
hasOne = False
for i in nums:
if nums[i] > 1:
if... | longest-palindrome | How can this beat over 90%? | BinFan | 0 | 3 | longest palindrome | 409 | 0.547 | Easy | 7,137 |
https://leetcode.com/problems/longest-palindrome/discuss/2782571/Simple-and-Straightforward-Python-Solution. | class Solution:
def longestPalindrome(self, s: str) -> int:
if not s: return 0
s_list: list[str] = sorted(list(s))
repeated_letters: list[str] = []
other_letters: list[str] = []
while s_list:
total_appearances = s_list.count(s_list[0])
# ex... | longest-palindrome | Simple and Straightforward Python Solution. | kahuni | 0 | 11 | longest palindrome | 409 | 0.547 | Easy | 7,138 |
https://leetcode.com/problems/longest-palindrome/discuss/2778764/Python-Solution | class Solution:
def longestPalindrome(self, s: str) -> int:
count = 0
seen = set()
for c in s:
if c in seen:
seen.remove(c)
count += 2
else:
seen.add(c)
return count if len(seen) == 0 el... | longest-palindrome | Python Solution | mansoorafzal | 0 | 9 | longest palindrome | 409 | 0.547 | Easy | 7,139 |
https://leetcode.com/problems/longest-palindrome/discuss/2753877/Easy-Python-Solution-using-dictionary | class Solution:
def longestPalindrome(self, s: str) -> int:
# Frequency Hash Map for keeping the frequency of each unique character
freq = {}
for i in s:
freq[i] = freq.get(i,0) + 1
maxOdd = 0
evenSum = 0
for i in freq.values():
if i % 2 =... | longest-palindrome | Easy Python Solution using dictionary | Suryansh_Codes | 0 | 14 | longest palindrome | 409 | 0.547 | Easy | 7,140 |
https://leetcode.com/problems/longest-palindrome/discuss/2737325/Python-Easy | class Solution:
def longestPalindrome(self, s: str) -> int:
from collections import Counter
if (s == s[::-1]):
return len(s)
ans = 0
count = Counter(s)
check = True
for key in count:
if (check):
if (count[key] % 2 == 1):
ans += 1
check = False
ans += ((count[key] // 2) * 2)... | longest-palindrome | Python Easy | lucasschnee | 0 | 6 | longest palindrome | 409 | 0.547 | Easy | 7,141 |
https://leetcode.com/problems/longest-palindrome/discuss/2695271/Python-using-Dictionary | class Solution:
def longestPalindrome(self, s: str) -> int:
d=dict()
for i in s:
if i in d:
d[i]+=1
else:
d[i]=1
c=0
k=[0]
for i,j in d.items():
if j%2==0:
c=c+j
else:
... | longest-palindrome | Python using Dictionary | Mani_23_ | 0 | 8 | longest palindrome | 409 | 0.547 | Easy | 7,142 |
https://leetcode.com/problems/longest-palindrome/discuss/2692609/Simple-Python-soln-O(n) | class Solution:
def longestPalindrome(self, s: str) -> int:
oddFlag = False
s = collections.Counter(s)
res = 0
for key, value in s.items():
if value % 2 == 0:
res += value
if value % 2 == 1:
oddFlag = True
res +... | longest-palindrome | Simple Python soln O(n) | 113377code | 0 | 17 | longest palindrome | 409 | 0.547 | Easy | 7,143 |
https://leetcode.com/problems/longest-palindrome/discuss/2692036/Python3-or-HashTable | class Solution:
def longestPalindrome(self, s: str) -> int:
counter = Counter(s)
total = 0
for key in counter:
value = counter[key]
if value == 1:
continue
if value % 2 == 0:
total += value
counter[key] = 0
... | longest-palindrome | Python3 | HashTable | honeybadgerofdoom | 0 | 1 | longest palindrome | 409 | 0.547 | Easy | 7,144 |
https://leetcode.com/problems/longest-palindrome/discuss/2683048/Python-Easy-O(n) | class Solution:
def longestPalindrome(self, s: str) -> int:
h = Counter(s)
odd = 0
even = 0
odd_count = 0
for i,v in h.items():
if v%2 == 0:
even += v
else:
odd_count +=1
odd += v-1
if odd_count >... | longest-palindrome | Python Easy O(n) | anu1rag | 0 | 4 | longest palindrome | 409 | 0.547 | Easy | 7,145 |
https://leetcode.com/problems/longest-palindrome/discuss/2671574/Python-Solution-or-Counter-or-Faster-than-97 | class Solution:
def longestPalindrome(self, s: str) -> int:
n=len(s)
ans=1
count=Counter(s)
# print(count)
ans=0
flag=False
for value in count.values():
if value%2==0:
ans+=value
else:
# take only even co... | longest-palindrome | Python Solution | Counter | Faster than 97% | Siddharth_singh | 0 | 8 | longest palindrome | 409 | 0.547 | Easy | 7,146 |
https://leetcode.com/problems/longest-palindrome/discuss/2666288/Simple-python-beats-99 | class Solution:
def longestPalindrome(self, s: str) -> int:
seen = set()
longest = 0
for char in s:
if char in seen:
seen.remove(char)
longest += 2
else:
seen.add(char)
return longest + int(bool(seen)) | longest-palindrome | Simple python beats 99% | AlecLeetcode | 0 | 3 | longest palindrome | 409 | 0.547 | Easy | 7,147 |
https://leetcode.com/problems/longest-palindrome/discuss/2655449/Using-Hashmap-or-Python | class Solution:
def longestPalindrome(self, s: str) -> int:
hm = dict(Counter(s))
print(hm)
odd_present = False
max_even_count = 0
for i in hm.values():
if i % 2 == 0:
max_even_count += i
else:
max_even_count += i-1
... | longest-palindrome | Using Hashmap | Python | hk_davy | 0 | 5 | longest palindrome | 409 | 0.547 | Easy | 7,148 |
https://leetcode.com/problems/longest-palindrome/discuss/2650088/Python3-Hash-bitwise-operations-O(n)-time-O(1)-space | class Solution:
def longestPalindrome(self, s: str) -> int:
a = Counter(s)
ans = 0
odd = 0
for _, fre in a.items():
ans += (fre & -2)
if fre & 1:
odd |= 1
return ans + odd | longest-palindrome | [Python3] Hash, bitwise operations, O(n) time, O(1) space | DG_stamper | 0 | 48 | longest palindrome | 409 | 0.547 | Easy | 7,149 |
https://leetcode.com/problems/longest-palindrome/discuss/2640465/Simple-Python-Explanation.-O(n)-time-O(1)-space-beats-88.74. | class Solution:
def longestPalindrome(self, s: str) -> int:
chars = {}
length = 0
for letter in s:
if letter not in chars or chars[letter] == 0:
chars[letter] = 1
else:
length += 2
chars[letter] = 0
for key ... | longest-palindrome | Simple Python Explanation. O(n) time, O(1) space beats 88.74%. | AdamPaslawski | 0 | 4 | longest palindrome | 409 | 0.547 | Easy | 7,150 |
https://leetcode.com/problems/longest-palindrome/discuss/2631092/Fast-python-solution | class Solution:
def longestPalindrome(self, s: str) -> int:
key_map = Counter(s)
odd = 0
res = 0
for value in key_map.values():
quotient, remainder = divmod(value, 2)
if remainder == 1:
odd = 1
res += quotient*2
... | longest-palindrome | Fast python solution | minghuizzzz | 0 | 9 | longest palindrome | 409 | 0.547 | Easy | 7,151 |
https://leetcode.com/problems/longest-palindrome/discuss/2604793/Python-Easy-Intuitive | class Solution:
def longestPalindrome(self, s: str) -> int:
d = Counter(s)
odd = []
c = 0
for i in d.values():
if i%2==0:
c += i
else:
odd.append(i)
if odd:
c = c + sum(odd) - len(odd)... | longest-palindrome | Python Easy Intuitive | Vedant_Aero | 0 | 59 | longest palindrome | 409 | 0.547 | Easy | 7,152 |
https://leetcode.com/problems/longest-palindrome/discuss/2552514/Easy-Understanding-Python | class Solution:
def longestPalindrome(self, s: str) -> int:
hashTable = {}
hashSet = set()
count = 0
oddCheck = 0
for c in s:
if(c in hashTable):
hashTable[c]+=1
else:
hashTable[c]=1
for c in s:
if(c ... | longest-palindrome | Easy Understanding Python | jxswxnth | 0 | 54 | longest palindrome | 409 | 0.547 | Easy | 7,153 |
https://leetcode.com/problems/longest-palindrome/discuss/2550912/Python-3-Need-little-help-with-the-solution | class Solution:
def longestPalindrome(self, s: str) -> int:
hash_map = dict()
for l in s:
if l in hash_map:
hash_map[l] += 1
else:
hash_map[l] = 1
p_len = 0
only_even = True
for key in hash_map.keys():
if has... | longest-palindrome | Python 3 - Need little help with the solution | rakshithgowdahr | 0 | 27 | longest palindrome | 409 | 0.547 | Easy | 7,154 |
https://leetcode.com/problems/longest-palindrome/discuss/2522949/I-misunderstood-this-question | class Solution:
def find_palindrome(self, word: str, left: int, right: int):
while 0 <= left and right <= len(word) - 1:
if word[left] == word[right]:
yield word[left: right + 1]
left -= 1
right += 1
def longestPalindrome(self, s: str) -> int:
len_s = len(s)
if not len_s:
... | longest-palindrome | I misunderstood this question | namashin | 0 | 59 | longest palindrome | 409 | 0.547 | Easy | 7,155 |
https://leetcode.com/problems/longest-palindrome/discuss/2517220/Python-Solution-Explained-oror-easy-to-understand | class Solution:
def longestPalindrome(self, s: str) -> int:
if len(s)==1:
return 1
d={}
for i in s: #frequency calculated of every element
if i in d:
d[i]+=1
else:
d[i]=1
v=list(d.values()) #all values array of d... | longest-palindrome | Python Solution - Explained || easy to understand ✔ | T1n1_B0x1 | 0 | 67 | longest palindrome | 409 | 0.547 | Easy | 7,156 |
https://leetcode.com/problems/longest-palindrome/discuss/2470028/Python-simple-hashmap-solution | class Solution:
def longestPalindrome(self, s: str) -> int:
count = collections.Counter(s)
res = 0
carry = 0
for k,v in count.items():
if v % 2 == 0:
res += v
else:
res += v - 1
carry = 1
return res + car... | longest-palindrome | Python simple hashmap solution | aruj900 | 0 | 58 | longest palindrome | 409 | 0.547 | Easy | 7,157 |
https://leetcode.com/problems/longest-palindrome/discuss/2462884/Using-HashMap-or-Python | class Solution:
def longestPalindrome(self, s: str) -> int:
dic = Counter(s)
even , odd = 0, 0
for i in dic:
if dic[i]%2==0:
even += dic[i]
else:
odd += (dic[i] -1)
hasOdd = any(dic[i] %2==1 for i in dic)
return even + o... | longest-palindrome | Using HashMap | Python | Abhi_-_- | 0 | 35 | longest palindrome | 409 | 0.547 | Easy | 7,158 |
https://leetcode.com/problems/longest-palindrome/discuss/2395286/Python-solution-Faster-than-96 | class Solution:
def longestPalindrome(self, s: str) -> int:
#Builds map
letters = {}
for i in s:
letters[i] = letters[i]+1 if i in letters else 1
#Calculates longest palyndrome based on the available number of each letter.
length = 0
for i in letters:... | longest-palindrome | Python solution - Faster than 96% | GMFB | 0 | 33 | longest palindrome | 409 | 0.547 | Easy | 7,159 |
https://leetcode.com/problems/longest-palindrome/discuss/2366626/Can-anybody-tell-me-what-is-wrong-with-my-python-solution-using-hashmap | class Solution:
def longestPalindrome(self, s: str) -> int:
hashmap = {}
if len(s) == 0:
return 0
for i in s:
if i not in hashmap:
hashmap[i] = 1
else:
hashmap[i]+= 1
#Longest odd chars + even chars
longest_o... | longest-palindrome | Can anybody tell me what is wrong with my python solution using hashmap? | Arana | 0 | 45 | longest palindrome | 409 | 0.547 | Easy | 7,160 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1901138/Python-Short-and-Simple-NLogSum | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def isPossible(maxSum):
curr = count = 0
for i in nums:
count += (i + curr > maxSum)
curr = curr + i if i + curr <= maxSum else i
return count + 1 <= m
l... | split-array-largest-sum | ✅ Python Short and Simple NLogSum | dhananjay79 | 3 | 112 | split array largest sum | 410 | 0.533 | Hard | 7,161 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1433241/python-memoization-%2B-binary-search-O(mn-log-n) | class Solution:
def splitArray(self, nums: [int], m: int) -> int:
prefixSum = []
curSum = 0
for num in nums:
curSum += num
prefixSum.append(curSum)
self.prefixSum = prefixSum
memo = dict()
minMax = self.helper(0, m-1, memo)
return ... | split-array-largest-sum | python memoization + binary search O(mn log n) | maxkmy | 2 | 127 | split array largest sum | 410 | 0.533 | Hard | 7,162 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1433241/python-memoization-%2B-binary-search-O(mn-log-n) | class Solution:
def splitArray(self, nums: [int], m: int) -> int:
prefixSum = []
curSum = 0
for num in nums:
curSum += num
prefixSum.append(curSum)
self.prefixSum = prefixSum
memo = [[-1] * m for i in range (len(nums))]
minMax = self.helpe... | split-array-largest-sum | python memoization + binary search O(mn log n) | maxkmy | 2 | 127 | split array largest sum | 410 | 0.533 | Hard | 7,163 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2108110/Python3-Runtime%3A-52ms-55.70-memory%3A-13.9mb-65.09 | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
array = nums
left, right = max(array), sum(array)
while left < right:
mid = (left + right) // 2
if self.canSplit(array, mid, m):
right = mid
else:
left =... | split-array-largest-sum | Python3 Runtime: 52ms 55.70% memory: 13.9mb 65.09% | arshergon | 1 | 34 | split array largest sum | 410 | 0.533 | Hard | 7,164 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1969588/Easy-To-Understand-Python-Code-oror-O(n*logn) | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def linear(maxLimit):
summ = 0
div = 1
for i in nums:
summ += i
if summ > maxLimit:
summ = i
div += 1
return ... | split-array-largest-sum | Easy To Understand Python Code || O(n*logn) | gamitejpratapsingh998 | 1 | 35 | split array largest sum | 410 | 0.533 | Hard | 7,165 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1899360/Python-Binary-Search-Solution | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
low, high = 0, 0
for n in nums:
low = max(low, n)
high += n
while low < high:
mid = (low + high) // 2
partitions = self.split_nums(nums, mid)
if partitions <= m:
... | split-array-largest-sum | Python Binary Search Solution | atiq1589 | 1 | 58 | split array largest sum | 410 | 0.533 | Hard | 7,166 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1871287/94-faster-python-solution-using-Binary-Search-approach. | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
minV = max(nums) #or min(nums)
maxV = sum(nums)
while minV <= maxV:
mid = minV+(maxV-minV)//2 #or (minV+maxV)//2
def check(nums,mid,m):
pieces = 1
... | split-array-largest-sum | 94% faster python solution using Binary Search approach. | 1903480100017_A | 1 | 44 | split array largest sum | 410 | 0.533 | Hard | 7,167 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1838981/Python-3-(50ms)-or-Binary-Search-Space-Approach-or-Similar-to-Book-Allocation-Problem | class Solution:
def splitArray(self, num: List[int], d: int) -> int:
def isPossible(num,d,m):
ws,c=0,1
for i in range(len(num)):
if ws+num[i]<=m:
ws+=num[i]
else:
c+=1
if c>d or num[i]>m:
... | split-array-largest-sum | Python 3 (50ms) | Binary Search Space Approach | Similar to Book Allocation Problem | MrShobhit | 1 | 93 | split array largest sum | 410 | 0.533 | Hard | 7,168 |
https://leetcode.com/problems/split-array-largest-sum/discuss/797905/Python3-binary-search-the-sum-space | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def fn(val):
"""Return True if it is possible to split."""
cnt = sm = 0
for x in nums:
if sm + x > val:
cnt += 1
sm = 0
... | split-array-largest-sum | [Python3] binary search the sum space | ye15 | 1 | 125 | split array largest sum | 410 | 0.533 | Hard | 7,169 |
https://leetcode.com/problems/split-array-largest-sum/discuss/797905/Python3-binary-search-the-sum-space | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
# prefix sum
prefix = [0]
for x in nums: prefix.append(prefix[-1] + x)
@lru_cache(None)
def fn(i, m):
"""Return the minimum of largest sum among m subarrays of nums[i:]."""
... | split-array-largest-sum | [Python3] binary search the sum space | ye15 | 1 | 125 | split array largest sum | 410 | 0.533 | Hard | 7,170 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2808202/Binary-Search-With-Thorough-Explanations | class Solution:
def splitArray(self, nums: List[int], k: int) -> int:
"""
Time: O(n * log(sum(n))) where sum(n) is the sum of all values in nums
Space: O(n)
"""
def can_split(cutoff):
nonlocal k
splits = 0
subarr_sum = 0
for n ... | split-array-largest-sum | Binary Search With Thorough Explanations | safarovd | 0 | 5 | split array largest sum | 410 | 0.533 | Hard | 7,171 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2713137/Py3-BinSearch-Step-by-step-Visualization-Explanation | class Solution:
def splitArray(self, nums: List[int], k: int) -> int:
def minimizable(threshold):
groups = 1
total = 0
for num in nums:
total += num
if total > threshold:
total = num
groups +... | split-array-largest-sum | ⭐[Py3] BinSearch Step by step Visualization Explanation | bromalone | 0 | 2 | split array largest sum | 410 | 0.533 | Hard | 7,172 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2684341/Python3-easy-solution | class Solution:
def splitArray(self, nums: List[int], k: int) -> int:
n=len(nums)
if n<k:return -1
def binarySearch(mid):
arrSum = 0
count = 1
for i in range(n):
if arrSum+nums[i]<=mid:
arr... | split-array-largest-sum | Python3 easy solution | shashank732001 | 0 | 15 | split array largest sum | 410 | 0.533 | Hard | 7,173 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2662877/Easy-to-understand-or-binary-search | class Solution:
def splits_for_max_sum(self, a, s, m):
i = 0
max_sum = 0
max_split = 0
while i < len(a):
# if some array ele > max_sum we are looking for (s) then spliting is not possible return 0
if a[i] > s:
return 0
while i < len... | split-array-largest-sum | Easy to understand | binary search | Ninjaac | 0 | 6 | split array largest sum | 410 | 0.533 | Hard | 7,174 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2351520/Python3-or-DP-Approach | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
n=len(nums)
ans=float('inf')
dp=[[-1 for i in range(m)] for j in range(n+1)]
def dfs(ind,lineCrossed):
if lineCrossed==m-1:
return sum(nums[ind:])
if dp[ind][lineCrossed]!=-1... | split-array-largest-sum | [Python3] | DP Approach | swapnilsingh421 | 0 | 33 | split array largest sum | 410 | 0.533 | Hard | 7,175 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2312659/Simple-Plain-Solution | class Solution(object):
def splitArray(self, nums, m):
"""
:type nums: List[int]
:type m: int
:rtype: int
"""
def cansplit(largest):
subarray = 0
curSum = 0
for n in nums:
curSum+=n
if curSum... | split-array-largest-sum | Simple Plain Solution | Abhi_009 | 0 | 34 | split array largest sum | 410 | 0.533 | Hard | 7,176 |
https://leetcode.com/problems/split-array-largest-sum/discuss/2143914/Python-Solution-using-Binary-Searchor-Efficient-than-93-solutions | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
#Helper function to split the array into m
def canSplit(largest):
subarray = 0
curSum = 0
for n in nums:
curSum += n
if curSum > largest:
subarray += 1
curSum = n
return subarray + 1 <= m
# l is the minimum value ... | split-array-largest-sum | Python Solution using Binary Search| Efficient than 93% solutions | nikhitamore | 0 | 36 | split array largest sum | 410 | 0.533 | Hard | 7,177 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1972984/Python-oror-Clean-and-Simple-Binary-Search | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
n = len(nums)
if n == m: return max(nums)
def guess(X):
if not X >= max(nums): return -1
i, sum_, split = 0, 0, 0
while i<n:
if sum_ + nums[i] <= X: sum_ += nums[i]
else:
split += 1
sum_ = nums[i]
i += 1
re... | split-array-largest-sum | Python || Clean and Simple Binary Search | morpheusdurden | 0 | 34 | split array largest sum | 410 | 0.533 | Hard | 7,178 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1933519/Python-Solution-oror-90-Faster-oror-Memory-less-than-96 | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def minNbrSubArr(mid):
sm=0 ; splits=1
for num in nums:
sm+=num
if sm>mid: sm=num ; splits+=1
return splits
lo=max(nums) ; hi=sum(nums)
while lo<... | split-array-largest-sum | Python Solution || 90% Faster || Memory less than 96% | Taha-C | 0 | 43 | split array largest sum | 410 | 0.533 | Hard | 7,179 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1901020/Python3-Solution-with-using-binary-search | class Solution:
# We check how many intervals will be obtained, provided that each of them is less than or equal to 'mid'
def helper(self, nums, mid, m):
cur_sum = 0
cuts = 1
for num in nums:
cur_sum += num
if cur_sum > mid:
cuts += 1
... | split-array-largest-sum | [Python3] Solution with using binary search | maosipov11 | 0 | 18 | split array largest sum | 410 | 0.533 | Hard | 7,180 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1899742/Python3-oror-Binary-Searchoror-O(nlogn)-time-oror | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
if len(nums) == m:
return max(nums)
#minimize the maximum: Binary Search
low, high = 0, sum(nums)
if m == 1:
return high
while low <= high:
mid = (low+high) // 2
... | split-array-largest-sum | Python3 || Binary Search|| O(nlogn) time || | s_m_d_29 | 0 | 39 | split array largest sum | 410 | 0.533 | Hard | 7,181 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1899661/Python-3-or-Binary-Search-or-Explanation | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
l, r = min(nums), sum(nums)
def ok(mx):
nonlocal m
cur = cnt = 0
for num in nums:
if num > mx:
return False
elif cur + num <= mx:
... | split-array-largest-sum | Python 3 | Binary Search | Explanation | idontknoooo | 0 | 53 | split array largest sum | 410 | 0.533 | Hard | 7,182 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1899085/Python-Simple-and-Easy-Python-Solution-Using-Binary-Search-oror-87.57-Faster | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
def max_sum_required(max_sum_value):
current_sum = 0
split_required = 0
for i in range(len(nums)):
if current_sum + nums[i] <= max_sum_value:
current_sum = current_sum + nums[i]
else:
current_sum = nums[i]
split... | split-array-largest-sum | [ Python ] ✅ Simple and Easy Python Solution Using Binary Search || 87.57 Faster 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 0 | 85 | split array largest sum | 410 | 0.533 | Hard | 7,183 |
https://leetcode.com/problems/split-array-largest-sum/discuss/1539315/Python3-Linear-search-to-Binary-search | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
"""
nums = [7,2,5,10,8], m = 2
subarray sum search range[max(nums), sum(nums)]
search for maximum subarray sum.
same number of cuts will have different maximum subbarry sum.
looking for the... | split-array-largest-sum | [Python3] Linear search to Binary search | zhanweiting | 0 | 179 | split array largest sum | 410 | 0.533 | Hard | 7,184 |
https://leetcode.com/problems/split-array-largest-sum/discuss/482582/Python3-binary-search-faster-than-98.09 | class Solution:
def splitArray(self, nums: List[int], m: int) -> int:
#return self.bruteForce(nums,m)
#self.cache=collections.defaultdict(dict)
#return self.memo(0,nums,m)
low,high,res=max(nums),sum(nums),-1
while low<=high:
pivot=(low+high)//2
... | split-array-largest-sum | Python3 binary search faster than 98.09% | jb07 | 0 | 174 | split array largest sum | 410 | 0.533 | Hard | 7,185 |
https://leetcode.com/problems/fizz-buzz/discuss/380065/Solution-in-Python-3-(beats-~98)-(one-line) | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
return ['FizzBuzz' if i%15 == 0 else 'Buzz' if i%5 == 0 else 'Fizz' if i%3 == 0 else str(i) for i in range(1,n+1)]
- Junaid Mansuri
(LeetCode ID)@hotmail.com | fizz-buzz | Solution in Python 3 (beats ~98%) (one line) | junaidmansuri | 6 | 1,900 | fizz buzz | 412 | 0.69 | Easy | 7,186 |
https://leetcode.com/problems/fizz-buzz/discuss/1786245/Python-3-(20ms)-or-Faster-than-99.7-or-Easy-to-Understand | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
r=[]
while n:
if n%3==0 and n%5==0:
r.append("FizzBuzz")
elif n%3==0:
r.append("Fizz")
elif n%5==0:
r.append("Buzz")
else:
r.append... | fizz-buzz | Python 3 (20ms) | Faster than 99.7% | Easy to Understand | MrShobhit | 5 | 490 | fizz buzz | 412 | 0.69 | Easy | 7,187 |
https://leetcode.com/problems/fizz-buzz/discuss/1707713/Easy-Without-Modulo-()-Operator | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
a=1
b=1
l=[]
for i in range(1,n+1):
if a==3 and b==5:
l.append('FizzBuzz')
a=1
b=1
elif b==5:
l.append('Buzz')
b=1
... | fizz-buzz | Easy, Without Modulo (%) Operator | vkadu68 | 4 | 99 | fizz buzz | 412 | 0.69 | Easy | 7,188 |
https://leetcode.com/problems/fizz-buzz/discuss/1103629/Python3-Solution-%3A-Faster-than-94.80-and-Less-memory-usage-than-90.13 | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
c3,c5 = 0,0
for i in range(1,n+1):
d = ""
c3 = c3+1
c5 = c5+1
if c3 == 3:
d += "Fizz"
c3 = 0
if c5 == 5:
d += "Buzz"
... | fizz-buzz | Python3 Solution : Faster than 94.80% and Less memory usage than 90.13% | bhagwataditya226 | 3 | 278 | fizz buzz | 412 | 0.69 | Easy | 7,189 |
https://leetcode.com/problems/fizz-buzz/discuss/2178012/Python3-O(n)-oror-O(1)-Runtime%3A-359ms-91.92-Memory%3A-14.3mb-78.65 | class Solution:
# O(n) || O(n)
# Runtime: 44ms 90.88ms ; Memory: 14.9mb 84.93%
def fizzBuzz(self, n: int) -> List[str]:
result = []
for i in range(1, n + 1):
if i % 15 == 0:
char = "FizzBuzz"
elif i % 3 == 0:
char = "Fizz"
... | fizz-buzz | Python3 O(n) || O(1) # Runtime: 359ms 91.92% ; Memory: 14.3mb 78.65% | arshergon | 2 | 240 | fizz buzz | 412 | 0.69 | Easy | 7,190 |
https://leetcode.com/problems/fizz-buzz/discuss/1623086/Python3-one-liner-easily-expandable | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
return ["".join(s for d,s in [(3,"Fizz"),(5,"Buzz")] if i%d == 0) or str(i) for i in range(1,n+1)] | fizz-buzz | Python3 one liner easily expandable | pknoe3lh | 2 | 113 | fizz buzz | 412 | 0.69 | Easy | 7,191 |
https://leetcode.com/problems/fizz-buzz/discuss/1323879/Python-or-Most-Production-Ready-Code-or-Beats-99-in-Time-or-Compatibility-Awesome | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
ans, num1, num2, bothTrue, firstTrue, secondTrue = [0]*n, 3, 5, "FizzBuzz", "Fizz", "Buzz"
for i in range(1,n+1):
first, second = i % num1 == 0, i % num2 == 0
if first and second: ans[i-1] = bothTrue
eli... | fizz-buzz | Python | Most Production Ready Code | Beats 99% in Time | Compatibility Awesome | paramvs8 | 2 | 501 | fizz buzz | 412 | 0.69 | Easy | 7,192 |
https://leetcode.com/problems/fizz-buzz/discuss/2698601/Python-oror-O(N)ororRuntime-43-ms-Beats-95.46-Memory-15.1-MB-Beats-42.94 | 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%5==0:
ans.append("Buzz")
elif i%3==0:
ans.append("Fizz")
else:
... | fizz-buzz | Python || O(N)||Runtime 43 ms Beats 95.46% Memory 15.1 MB Beats 42.94% | Sneh713 | 1 | 250 | fizz buzz | 412 | 0.69 | Easy | 7,193 |
https://leetcode.com/problems/fizz-buzz/discuss/2611048/Python3-most-easy-solution | 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 most easy solution | khushie45 | 1 | 334 | fizz buzz | 412 | 0.69 | Easy | 7,194 |
https://leetcode.com/problems/fizz-buzz/discuss/2373799/Memory-efficient-Python3-solution-oror-15MB-oror-Faster-than-65-oror-Explained! | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
# Create dictionary to store mappings
dict_maps = {3: "Fizz", 5:"Buzz"}
fizzBuzz_list = []
#iterate over the keys
for i in range(1, n+1):
temp_str = ""
for ... | fizz-buzz | Memory efficient Python3 solution || 15MB || Faster than 65% || Explained! | harishmanjunatheswaran | 1 | 148 | fizz buzz | 412 | 0.69 | Easy | 7,195 |
https://leetcode.com/problems/fizz-buzz/discuss/2155863/Python-or-very-simple-solution-95-faster | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
res = []
for i in range(1,n+1):
if i % 15 == 0:
res.append('FizzBuzz')
elif i % 3 == 0:
res.append('Fizz')
elif i % 5 == 0:
res.append('Buzz')
else... | fizz-buzz | Python | very simple solution 95% faster | __Asrar | 1 | 145 | fizz buzz | 412 | 0.69 | Easy | 7,196 |
https://leetcode.com/problems/fizz-buzz/discuss/1960630/Python-99-faster-and-99-less-memory-usage | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
for i in range(1,n+1):
to_yield = ''
if i % 3 == 0:to_yield += 'Fizz'
if i % 5 == 0:to_yield += 'Buzz'
elif to_yield == '': to_yield = str(i)
yield to_yield | fizz-buzz | Python 99% faster and 99% less memory usage | Ipicon | 1 | 238 | fizz buzz | 412 | 0.69 | Easy | 7,197 |
https://leetcode.com/problems/fizz-buzz/discuss/1960568/Python-solution-using-yield-operator | class Solution:
def fizzBuzz(self, n: int) -> List[str]:
for i in range(1, n+1):
if i % 3 == 0 and i % 5 == 0:
yield 'FizzBuzz'
elif i%3==0:
yield 'Fizz'
elif i%5==0:
yield 'Buzz'
else:
yield f'{i... | fizz-buzz | Python solution using yield operator | constantine786 | 1 | 54 | fizz buzz | 412 | 0.69 | Easy | 7,198 |
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]:
d = {3 : "Fizz", 5 : "Buzz"}
res = []
for i in range(1,n+1):
ans = ''
if i % 3 == 0:
ans += d[3]
if i % 5 == 0:
ans += d[5]
if not ans:
ans... | fizz-buzz | Python3 simple solution using "dictionary" and "if-else" | EklavyaJoshi | 1 | 97 | fizz buzz | 412 | 0.69 | Easy | 7,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.