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/number-complement/discuss/1203432/99.20-faster-python-MUCH-easy-solution.
class Solution: def findComplement(self, num: int) -> int: complement="" for i in bin(num)[2:]: if i is "0": complement+="1" else: complement+="0" return int(complement,2)
number-complement
99.20% faster python MUCH easy solution.
_jorjis
6
271
number complement
476
0.672
Easy
8,400
https://leetcode.com/problems/number-complement/discuss/1649805/Python3-or-1-liner-or-xor-logic
class Solution: def findComplement(self, num: int) -> int: return num^int('1'*len(bin(num)[2:]), 2)
number-complement
Python3 | 1 liner | xor logic
nandhakiran366
3
190
number complement
476
0.672
Easy
8,401
https://leetcode.com/problems/number-complement/discuss/1649928/Python-3-oror-self-Understandable-oror-No-Inbuilt-function-Is-Used
class Solution: def findComplement(self, num: int) -> int: #Function to get the binary value def getBinary(n): if n==0 or n==1: return str(n) if n%2==0: return '0'+getBinary(n//2) else: return '1'+getBinary...
number-complement
Python 3 || self-Understandable || No-Inbuilt-function-Is-Used
bug_buster
1
79
number complement
476
0.672
Easy
8,402
https://leetcode.com/problems/number-complement/discuss/1649527/Python-or-One-line-orEasy-or-3-ways
class Solution(object): def findComplement(self, num): """ :type num: int :rtype: int """ return 2**int(math.log(num,2)+1) -1 - num
number-complement
Python | One line |Easy | 3 ways
mshanker
1
101
number complement
476
0.672
Easy
8,403
https://leetcode.com/problems/number-complement/discuss/1649527/Python-or-One-line-orEasy-or-3-ways
class Solution: def findComplement(self, num: int) -> int: return 2**num.bit_length() - num -1
number-complement
Python | One line |Easy | 3 ways
mshanker
1
101
number complement
476
0.672
Easy
8,404
https://leetcode.com/problems/number-complement/discuss/1649527/Python-or-One-line-orEasy-or-3-ways
class Solution: def findComplement(self, num: int) -> int: return int('1'*num.bit_length(), 2 ) ^ num
number-complement
Python | One line |Easy | 3 ways
mshanker
1
101
number complement
476
0.672
Easy
8,405
https://leetcode.com/problems/number-complement/discuss/1304589/Easy-Python-Solution(99.25)
class Solution: def findComplement(self, num: int) -> int: s=bin(num)[2:] m=0 for i in range(len(s)): if(s[i]=='0'): x=2**(len(s)-i-1) m+=x return m
number-complement
Easy Python Solution(99.25%)
Sneh17029
1
225
number complement
476
0.672
Easy
8,406
https://leetcode.com/problems/number-complement/discuss/1279426/Python3-dollarolution
class Solution: def findComplement(self, num: int) -> int: num = bin(num)[2:] s = '' for i in num: if i == '1': s += '0' else: s += '1' return (int(s,2))
number-complement
Python3 $olution
AakRay
1
105
number complement
476
0.672
Easy
8,407
https://leetcode.com/problems/number-complement/discuss/554925/O(log-n)-Python-Solution
class Solution: def findComplement(self, num: int) -> int: place = 0 complement = 0 while num > 0: remainder = num % 2 if remainder == 0: complement += 2 ** place place += 1 num = num // 2 return complement ```
number-complement
O(log n) Python Solution
ericadmore
1
131
number complement
476
0.672
Easy
8,408
https://leetcode.com/problems/number-complement/discuss/405282/Python-simplest-solution
class Solution: def findComplement(self, num: int) -> int: b = bin(num)[2:] b = b.replace('1','2') b = b.replace('0', '1') b = b.replace('2','0') return int(b,2)
number-complement
Python simplest solution
saffi
1
367
number complement
476
0.672
Easy
8,409
https://leetcode.com/problems/number-complement/discuss/2806776/One-line-code-in-python
class Solution: def findComplement(self, s: int) -> int: return int(bin(s)[2:].replace('1', 'temp').replace('0', '1').replace('temp', '0'),2)
number-complement
One line code in python
MayuD
0
4
number complement
476
0.672
Easy
8,410
https://leetcode.com/problems/number-complement/discuss/2802885/Handled-by-list
class Solution: def findComplement(self, num: int) -> int: ret = [] sNum = list(str(bin(num))) for i in range(2, len(sNum)): if sNum[i] == '1': sNum[i] = '0' elif sNum[i] == '0': sNum[i] = '1' return int(''.join(sNum),...
number-complement
Handled by list
kardenmui
0
1
number complement
476
0.672
Easy
8,411
https://leetcode.com/problems/number-complement/discuss/2779499/Python-Simple-One-Liner-Solition
class Solution: def findComplement(self, num: int) -> int: return (2**(int(log(num,2))+1)-1)^num
number-complement
Python Simple One Liner Solition
paradox890
0
8
number complement
476
0.672
Easy
8,412
https://leetcode.com/problems/number-complement/discuss/2744504/Simple-Python-Solution
class Solution: def findComplement(self, num: int) -> int: i = 1 while i <= num: i = i << 1 return (i - 1) ^ num
number-complement
Simple Python Solution
dnvavinash
0
2
number complement
476
0.672
Easy
8,413
https://leetcode.com/problems/number-complement/discuss/2735026/Python-simple-straight-forward-solution
class Solution: def findComplement(self, num: int) -> int: num = bin(num)[2:] ans = '' for i in num: ans += str(1 - int(i)) return int(ans, 2)
number-complement
Python simple straight-forward solution
Mark_computer
0
3
number complement
476
0.672
Easy
8,414
https://leetcode.com/problems/number-complement/discuss/2657658/Simple-one-line-solution-Python
class Solution: def findComplement(self, num: int) -> int: return ((2**(len(bin(num))-2))-1) - num
number-complement
Simple one line solution Python
choisauce
0
4
number complement
476
0.672
Easy
8,415
https://leetcode.com/problems/number-complement/discuss/2502206/Faster-then-84-solutions-oror-Python
class Solution: def findComplement(self, num: int) -> int: b=list(str(bin(num))) for i in range(2,len(b)): if b[i]=="0": b[i]="1" else: b[i]="0" s="".join(b) return int(s,2)
number-complement
Faster then 84% solutions || Python
keertika27
0
33
number complement
476
0.672
Easy
8,416
https://leetcode.com/problems/number-complement/discuss/2475575/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def findComplement(self, num: int) -> int: strr = bin(num) output = '' for i in strr[2:]: if i == '0': output+='1' else: output+='0' return int('0b'+output,2)
number-complement
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
50
number complement
476
0.672
Easy
8,417
https://leetcode.com/problems/number-complement/discuss/2381189/Number-Complement
class Solution: def findComplement(self, num: int) -> int: x=bin(num) fin=str(x[2:]) print(fin) res="" for i in range(len(fin)): if fin[i]=="0": res+="1" else:res+="0" return (int(res,2))
number-complement
Number Complement
Faraz369
0
12
number complement
476
0.672
Easy
8,418
https://leetcode.com/problems/number-complement/discuss/2310698/Python3-one-liner-with-Explanation
class Solution: def findComplement(self, num: int) -> int: return num ^ ( 2 ** math.floor(math.log(num, 2) + 1) - 1 )
number-complement
Python3 one liner with Explanation
PrashantSinghai
0
35
number complement
476
0.672
Easy
8,419
https://leetcode.com/problems/number-complement/discuss/2264666/complementnum
class Solution: def findComplement(self, num: int) -> int: bn = bin(num).replace('0b', '') bnlist = [i for i in bn] for i, c in enumerate(bnlist): if c == '0': bnlist[i] = '1' else: bnlist[i] = '0' ...
number-complement
complementnum
eerla
0
10
number complement
476
0.672
Easy
8,420
https://leetcode.com/problems/number-complement/discuss/2157895/Python-One-Liner-Efficient-Code
class Solution: def findComplement(self, num: int) -> int: return int(''.join({'0': '1', '1': '0'}[x] for x in str(bin(num))[2:]), 2)
number-complement
Python One Liner Efficient Code
JanmayBhatt
0
90
number complement
476
0.672
Easy
8,421
https://leetcode.com/problems/number-complement/discuss/2053420/Python-oneliner
class Solution: def findComplement(self, num: int) -> int: return int(bin(num)[2:].replace('1','2').replace('0','1').replace('2','0'),2)
number-complement
Python oneliner
StikS32
0
70
number complement
476
0.672
Easy
8,422
https://leetcode.com/problems/number-complement/discuss/1902552/Python-Bitwise-%2B-One-Liner!
class Solution: def findComplement(self, num): num_copy, i = num, 0 while num_copy: i += 1 num_copy >>= 1 return num ^ 2**i-1
number-complement
Python - Bitwise + One Liner!
domthedeveloper
0
76
number complement
476
0.672
Easy
8,423
https://leetcode.com/problems/number-complement/discuss/1902552/Python-Bitwise-%2B-One-Liner!
class Solution: def findComplement(self, n): return int(bin(n)[2:].translate(str.maketrans("01","10")), 2)
number-complement
Python - Bitwise + One Liner!
domthedeveloper
0
76
number complement
476
0.672
Easy
8,424
https://leetcode.com/problems/number-complement/discuss/1870920/Python-easy-solution-for-beginners
class Solution: def findComplement(self, num: int) -> int: binary = list(bin(num)[2:]) for i in range(len(binary)): if binary[i] == '0': binary[i] = '1' else: binary[i] = '0' res = '0b' + ''.join(binary) return int(res, 2)
number-complement
Python easy solution for beginners
alishak1999
0
58
number complement
476
0.672
Easy
8,425
https://leetcode.com/problems/number-complement/discuss/1854517/1-Line-Python-Solution-oror-80-Faster-oror-Memory-less-than-70
class Solution: def findComplement(self, num: int) -> int: return int("".join(['1' if c=='0' else '0' for c in bin(num)[2:]]),2)
number-complement
1-Line Python Solution || 80% Faster || Memory less than 70%
Taha-C
0
46
number complement
476
0.672
Easy
8,426
https://leetcode.com/problems/number-complement/discuss/1854517/1-Line-Python-Solution-oror-80-Faster-oror-Memory-less-than-70
class Solution: def findComplement(self, num: int) -> int: return ~num + (1<<len(bin(num)[2:]))
number-complement
1-Line Python Solution || 80% Faster || Memory less than 70%
Taha-C
0
46
number complement
476
0.672
Easy
8,427
https://leetcode.com/problems/number-complement/discuss/1760914/PYTHON-SIMPLE-ONE-LINER
class Solution: def findComplement(self, num: int) -> int: return 2**(len(bin(num).replace("0b","")))-num-1
number-complement
PYTHON SIMPLE ONE-LINER
vijayvardhan6
0
79
number complement
476
0.672
Easy
8,428
https://leetcode.com/problems/number-complement/discuss/1649442/python-4-lines
class Solution: def findComplement(self, num: int) -> int: ones = 0 for i in range(len(bin(num)) - 2): ones += 2**i return ones ^ num
number-complement
python 4 lines
kellygnaw
0
50
number complement
476
0.672
Easy
8,429
https://leetcode.com/problems/number-complement/discuss/1649358/python-bit-wise-or-solution
class Solution: def findComplement(self, num: int) -> int: output_rev = '' while num: output_rev += str(int(bin(num)[-1])^1) num = num >> 1 output = output_rev[::-1] return int(output,base=2)
number-complement
python bit wise or solution
yingziqing123
0
64
number complement
476
0.672
Easy
8,430
https://leetcode.com/problems/number-complement/discuss/1551482/Simple-Python-solution-faster-than-99.89
class Solution: def findComplement(self, num: int) -> int: b = bin(num)[2:] bc = "0b" for i in b: if i=='0': bc += '1' else: bc += '0' return int(bc,2)
number-complement
Simple Python solution - faster than 99.89%
Pritish0173
0
73
number complement
476
0.672
Easy
8,431
https://leetcode.com/problems/number-complement/discuss/1445665/Python3-Beats-100
class Solution: def findComplement(self, num: int) -> int: num = str(bin(num)) s = '' for let in num[2:]: if let == '0': s += '1' else: s += '0' return int(s,2)
number-complement
Python3 Beats 100%
yjin232
0
52
number complement
476
0.672
Easy
8,432
https://leetcode.com/problems/number-complement/discuss/1067064/Python3-simple-solution
class Solution: def findComplement(self, num: int) -> int: s = bin(num).replace('0b','') return int('1'*len(s),2) - num
number-complement
Python3 simple solution
EklavyaJoshi
0
53
number complement
476
0.672
Easy
8,433
https://leetcode.com/problems/number-complement/discuss/880995/Python-solution-in-functional-style-with-walrus-operator!
class Solution: def findComplement(self, num: int) -> int: if num == 0: return 1 power = 1 data = (num, 0) return sum( # keep only first element from tuple to sum map(lambda x: x[0], # this function terminates loop when all bits...
number-complement
Python solution in functional style with walrus operator!
vav1288
0
89
number complement
476
0.672
Easy
8,434
https://leetcode.com/problems/number-complement/discuss/612775/Python-3-90-best-solution.-6-lines.-One-loop.-No-additional-space.-With-explanations
class Solution: def findComplement(self, num): # For 0 and 1 if num <= 1: return abs(num - 1) # Find first greater power of two (first bit is 1 other bits are 0) power = 0 while(num >= pow(2, power)): power += 1 # Return XOR for num and number with all bit...
number-complement
[Python 3] 90% best solution. 6 lines. One loop. No additional space. With explanations
abstractart
0
32
number complement
476
0.672
Easy
8,435
https://leetcode.com/problems/number-complement/discuss/294347/python3-one-line-97-using-list-comprehension
class Solution: def findComplement(self, num: int) -> int: return int("".join(["1" if i == "0" else "0" for i in list(bin(num))[2:]]),2)
number-complement
python3 one line 97% using list comprehension
Andily
0
62
number complement
476
0.672
Easy
8,436
https://leetcode.com/problems/total-hamming-distance/discuss/851194/Python-3-or-Bit-Manipulation-O(N)-or-Explanations
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: ans = 0 for i in range(32): zero = one = 0 mask = 1 << i for num in nums: if mask &amp; num: one += 1 else: zero += 1 ans += one * zero ...
total-hamming-distance
Python 3 | Bit Manipulation O(N) | Explanations
idontknoooo
26
1,300
total hamming distance
477
0.522
Medium
8,437
https://leetcode.com/problems/total-hamming-distance/discuss/853198/Python3-O(N)
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: freq = [0]*32 # count of "1" (32-bit "overkill") for x in nums: x = bin(x)[2:].zfill(32) # 32-bit binary for i in range(32): freq[i] += x[i] == "1" # count of 1 return sum(freq[i] * (len(nums...
total-hamming-distance
[Python3] O(N)
ye15
3
167
total hamming distance
477
0.522
Medium
8,438
https://leetcode.com/problems/total-hamming-distance/discuss/853198/Python3-O(N)
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: ans = 0 freq = [0]*32 # count of "1" (32-bit "overkill") for k, x in enumerate(nums): x = bin(x)[2:].zfill(32) # 32-bit binary for i in range(32): if x[i] == "0": ans += freq[i...
total-hamming-distance
[Python3] O(N)
ye15
3
167
total hamming distance
477
0.522
Medium
8,439
https://leetcode.com/problems/total-hamming-distance/discuss/1586104/Simple-Python-Solution-with-comments-or-Time%3A-O(n)-or-Space%3A-O(1)
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: # bits stores the count of numbers in nums for which the ith bit is set bits = [0]*32 # consider [4,14,2] # # 3 2 1 0 (ith bit) # --------------- # 0 1 0 0 ...
total-hamming-distance
Simple Python Solution with comments | Time: O(n) | Space: O(1)
gangasingh1807
1
278
total hamming distance
477
0.522
Medium
8,440
https://leetcode.com/problems/total-hamming-distance/discuss/1341047/Python-O(n)-or-Bit-Manipulation-or-Explanation
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: distance = 0 #hamming distance for i in range(30): mask = 1 << i #mask will be power ith power of 2 one , zero = 0 , 0 for num in nums: ...
total-hamming-distance
Python O(n) | Bit Manipulation | Explanation
abhiGamez
1
315
total hamming distance
477
0.522
Medium
8,441
https://leetcode.com/problems/total-hamming-distance/discuss/1470728/o(32*n)
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: ans = 0 for i in range(32): zero_count = one_count = 0 for num in nums: x = (num &amp; (1<<i)) if x == 0: zero_count +=1 else: ...
total-hamming-distance
o(32*n)
Sanjaychandak95
0
69
total hamming distance
477
0.522
Medium
8,442
https://leetcode.com/problems/total-hamming-distance/discuss/470106/Python3-89.70-(368-ms)100.00-(14.1-MB)-O(n)-time-O(1)-space
class Solution: def totalHammingDistance(self, nums: List[int]) -> int: nums_len = len(nums) if (nums_len == 0): return 0 current_bit = 1 ret = 0 max_ = max(nums) while (current_bit <= max_): counte...
total-hamming-distance
Python3 89.70% (368 ms)/100.00% (14.1 MB) -- O(n) time / O(1) space
numiek_p
0
179
total hamming distance
477
0.522
Medium
8,443
https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/1113715/Python-wrong-test-cases
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.x = x_center self.y = y_center self.radius = radius def randPoint(self) -> List[float]: first = random.uniform(-self.radius, self.radius) secondmax = (self.radius ** 2 - first ** 2...
generate-random-point-in-a-circle
Python, wrong test cases?
warmr0bot
4
322
generate random point in a circle
478
0.396
Medium
8,444
https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/1921960/Python-easy-understanding-solution-with-detailed-explanation.
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.x = x_center self.y = y_center self.r = radius def randPoint(self) -> List[float]: r = sqrt(random.random()) * self.r angle = random.uniform(0, 2*pi) return [self.x + r * m...
generate-random-point-in-a-circle
Python easy - understanding solution with detailed explanation.
byroncharly3
3
154
generate random point in a circle
478
0.396
Medium
8,445
https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/2645750/Python3-Solution
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.r = radius self.x, self.y = x_center, y_center def randPoint(self) -> List[float]: theta = uniform(0,2*pi) R = sqrt(uniform(0,self.r**2)) return [self.x+R*cos(theta), self.y+R*sin(theta)]
generate-random-point-in-a-circle
Python3 Solution
raghavdabra
2
77
generate random point in a circle
478
0.396
Medium
8,446
https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/1005436/Python-rejection-sampling-and-polar-coordinates
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.r = radius self.x_center = x_center self.y_center = y_center def randPoint(self) -> List[float]: x, y = 1, 1 while x*x + y*y > 1: x = 2*random.random() - 1 ...
generate-random-point-in-a-circle
Python, rejection sampling and polar coordinates
blue_sky5
0
205
generate random point in a circle
478
0.396
Medium
8,447
https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/1005436/Python-rejection-sampling-and-polar-coordinates
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.r = radius self.x_c = x_center self.y_c = y_center def randPoint(self) -> List[float]: r = self.r * math.sqrt(random.random()) phi = 2 * math.pi * random.random() x...
generate-random-point-in-a-circle
Python, rejection sampling and polar coordinates
blue_sky5
0
205
generate random point in a circle
478
0.396
Medium
8,448
https://leetcode.com/problems/generate-random-point-in-a-circle/discuss/853211/Python3-polar-coordinates
class Solution: def __init__(self, radius: float, x_center: float, y_center: float): self.radius = radius self.x_center = x_center self.y_center = y_center def randPoint(self) -> List[float]: radius = self.radius*sqrt(random.random()) # sample radius as r*sqrt(x) thet...
generate-random-point-in-a-circle
[Python3] polar coordinates
ye15
0
218
generate random point in a circle
478
0.396
Medium
8,449
https://leetcode.com/problems/largest-palindrome-product/discuss/1521512/Python3-Solution-with-explanation
class Solution: def largestPalindrome(self, n: int) -> int: # just to forget about 1-digit case if n == 1: return 9 # minimal number with n digits (for ex. for n = 4, min_num = 1000) min_num = 10 ** (n - 1) # maximal number with n digits...
largest-palindrome-product
Python3 Solution with explanation
frolovdmn
8
741
largest palindrome product
479
0.317
Hard
8,450
https://leetcode.com/problems/largest-palindrome-product/discuss/1618527/Python-Fast.-Swift-100-faster-100-less-memory.-No-cheating.
class Solution: def largestPalindrome(self, n: int) -> int: if n == 1: return 9 maxi = 10 ** n # store the value of 10ⁿ for z in range(2, maxi): # since both x, y > 0 and z = x + y; which implies that z has a minimum value of 2 left = maxi - z right = i...
largest-palindrome-product
✅Python Fast. Swift 100% faster 100% less memory. No cheating.🔥
blest
4
230
largest palindrome product
479
0.317
Hard
8,451
https://leetcode.com/problems/largest-palindrome-product/discuss/2368960/faster-than-87.88-or-python3
class Solution: def largestPalindrome(self, n: int) -> int: return [0, 9, 987, 123, 597, 677, 1218, 877, 475][n] def isPalindrome(x): return str(x) == str(x)[::-1] def solve(n): best = 0 for i in range(10**n-1, 0, -1): for j in range(max(i, (best-1)//i+...
largest-palindrome-product
faster than 87.88% | python3
vimla_kushwaha
-2
148
largest palindrome product
479
0.317
Hard
8,452
https://leetcode.com/problems/sliding-window-median/discuss/1942580/Easiest-Python-O(n-log-k)-Two-Heaps-(Lazy-Removal)-96.23
class Solution: # TC - O((n - k)*log(k)) # SC - O(k) # 121 ms, faster than 96.23% def find_median(self, max_heap, min_heap, heap_size): if heap_size % 2 == 1: return -max_heap[0] else: return (-max_heap[0] + min_heap[0]) / 2 def medianSlidingWindow(self, nums: ...
sliding-window-median
✅ Easiest Python O(n log k) Two Heaps (Lazy Removal), 96.23%
AntonBelski
4
480
sliding window median
480
0.414
Hard
8,453
https://leetcode.com/problems/sliding-window-median/discuss/2795456/Python-or-Sort
class Solution: def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: res=[] i=0 j=k while j<=len(nums): med=self.median(nums[i:j],k) res.append(med) i+=1;j+=1 return res def median(self,num,k): num.sort() ...
sliding-window-median
Python | Sort
jainsiddharth99
0
4
sliding window median
480
0.414
Hard
8,454
https://leetcode.com/problems/sliding-window-median/discuss/2789590/Python-with-custom-SortedList-implementation
class Solution: def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: window = OrderedList(nums[:k]) medians = [window.median] for i in range(k, len(nums)): window.remove(nums[i - k]) window.append(nums[i]) medians.append(window.median) ...
sliding-window-median
Python with custom SortedList implementation
user2341El
0
3
sliding window median
480
0.414
Hard
8,455
https://leetcode.com/problems/sliding-window-median/discuss/2744218/Two-Heaps-Fast-and-Easy-Solution
class Solution: def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: result = deque([]) minheap = [] maxheap = [] # ---------------------------------------------------------------- def balance(): if (maxheap and minheap) and (maxheap[0] * -1) > m...
sliding-window-median
Two Heaps - Fast and Easy Solution
user6770yv
0
12
sliding window median
480
0.414
Hard
8,456
https://leetcode.com/problems/sliding-window-median/discuss/2641986/Python-Simple-Clean-Easy-T%3A-O(Nk)%3A-S%3A-O(k)
class Solution: def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: sum_ = 0 length = len(nums) for idx, val in enumerate(nums): if idx > length-k: break temp = sorted(nums[idx:idx+k]) if k%2==0: nums[idx]...
sliding-window-median
✅ [Python] Simple, Clean, Easy T: O(Nk): S: O(k)
girraj_14581
0
47
sliding window median
480
0.414
Hard
8,457
https://leetcode.com/problems/sliding-window-median/discuss/2439738/Clean-Simple-Python3-or-Bisect-or-O(n-log(k))-or-Faster-than-95
class Solution: def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: odd, mid = k % 2, k // 2 def current_median(): nonlocal odd, mid, window if odd: return window[mid] return (window[mid - 1] + window[mid]) / 2 ...
sliding-window-median
Clean, Simple Python3 | Bisect | O(n log(k)) | Faster than 95%
ryangrayson
0
30
sliding window median
480
0.414
Hard
8,458
https://leetcode.com/problems/sliding-window-median/discuss/1450291/Python3Python-Solution-using-bisect.insort-w-comments
class Solution: def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]: # Init medians = [] n = len(nums) # Make median indexes m1,m2 = (k//2,k//2) if k%2 else ((k//2)-1,k//2) # Sort the array and find the first median ...
sliding-window-median
[Python3/Python] Solution using bisect.insort w/ comments
ssshukla26
0
79
sliding window median
480
0.414
Hard
8,459
https://leetcode.com/problems/magical-string/discuss/2558509/481.-Magical-String
class Solution: def magicalString(self, n: int) -> int: arr, i = [1,2,2], 2 while len(arr) < n: arr.extend([arr[-1]^3]*arr[i]) i += 1 return arr[:n].count(1)
magical-string
481. Magical String
warrenruud
3
417
magical string
481
0.505
Medium
8,460
https://leetcode.com/problems/magical-string/discuss/1250211/Python3-Explained-Very-Intuitive-Simulating-the-process
class Solution: def magicalString(self, n: int) -> int: ref = "122112" actual = "" start = 0 one = True while(len(ref) < n): for i in range(start, len(ref)): if(one): actual += int(ref[i]) * "1" one ...
magical-string
[Python3] - Explained - Very Intuitive - Simulating the process
vs152
2
305
magical string
481
0.505
Medium
8,461
https://leetcode.com/problems/magical-string/discuss/1779346/Two-Pointer-or-One-pass
class Solution: def magicalString(self, n: int) -> int: if n in [1,2,3]: return 1 s, p1, p2, curr, count = '122', 2, 3, '1', 1 while p2 < n: s += curr * int(s[p1]) p2 += int(s[p1]) if curr == '1': if p2 > n: ...
magical-string
Two Pointer | One pass
kaichamp101
1
192
magical string
481
0.505
Medium
8,462
https://leetcode.com/problems/magical-string/discuss/1565969/Python-or-Simulation-%2B-Count-Ones-or-6-Lines
class Solution: def magicalString(self, n: int) -> int: s = ['1', '2', '2'] for i in range(2, n): add_two = s[-1] == '1' s.extend(list(int(s[i]) * ('2' if add_two else '1'))) if len(s) >= n: break return s[:n].count('1')
magical-string
Python | Simulation + Count Ones | 6 Lines
leeteatsleep
1
230
magical string
481
0.505
Medium
8,463
https://leetcode.com/problems/magical-string/discuss/979966/Python3-generate-S-O(N)
class Solution: def magicalString(self, n: int) -> int: if n == 0: return 0 # edge case S = [1,2,2] i = 2 while len(S) < n: S.extend(S[i] * [3 ^ S[-1]]) i += 1 return S[:n].count(1)
magical-string
[Python3] generate S O(N)
ye15
1
277
magical string
481
0.505
Medium
8,464
https://leetcode.com/problems/magical-string/discuss/515302/Python3-simple-short-solution
class Solution: def magicalString(self, n: int) -> int: if n==0: return 0 if n<=3: return 1 s,index = [1,2,2],2 while len(s)<n: s+=[3-s[-1]]*s[index] index+=1 return s[:n].count(1)
magical-string
Python3 simple short solution
jb07
0
205
magical string
481
0.505
Medium
8,465
https://leetcode.com/problems/license-key-formatting/discuss/540266/PythonJSC%2B%2B-O(n)-by-string-operation.-w-Explanation
class Solution: def licenseKeyFormatting(self, S: str, K: int) -> str: # Eliminate all dashes S = S.replace('-', '') head = len(S) % K grouping = [] # Special handle for first group if head: grouping.append( S[:head] ) ...
license-key-formatting
Python/JS/C++ O(n) by string operation. [w/ Explanation]
brianchiang_tw
35
1,700
license key formatting
482
0.432
Easy
8,466
https://leetcode.com/problems/license-key-formatting/discuss/1831097/Python-3-Solution-or-93-lesser-memory
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = (s.upper()).replace("-","")[::-1] ans = str() for i in range(0,len(s),k): ans += s[i:i+k]+"-" return ans[::-1][1:]
license-key-formatting
✔Python 3 Solution | 93% lesser memory
Coding_Tan3
6
370
license key formatting
482
0.432
Easy
8,467
https://leetcode.com/problems/license-key-formatting/discuss/2752775/Clean-Python-Solution
class Solution: # Time : O(n) | Space : O(n) def licenseKeyFormatting(self, s: str, k: int) -> str: result = [] count = 0 s = s.replace("-", "") for i in reversed(range(len(s))): result.append(s[i].upper()) count += 1 # we don't want to put a d...
license-key-formatting
Clean Python Solution
SuvroBaner
3
129
license key formatting
482
0.432
Easy
8,468
https://leetcode.com/problems/license-key-formatting/discuss/1286015/Python-Solution-easiest-greater-Google-greaterLicense-Key-Formatting
class Solution: def licenseKeyFormatting(self, our_str: str, k: int) -> str: our_str = our_str.replace('-','').upper()[::-1] res = '' count = 0 for license_key_elem in our_str: if count == k: res += '-' count = 0 ...
license-key-formatting
Python Solution easiest -> Google ->[License Key Formatting}
avEraGeC0der
3
247
license key formatting
482
0.432
Easy
8,469
https://leetcode.com/problems/license-key-formatting/discuss/2178557/Python3-O(n)-oror-O(n)-Runtime%3A-77ms-53.47-Memory%3A-14.7mb-41.07
class Solution: # O(n) || O(n) # Runtime: 77ms 53.47% ; Memory: 14.7mb 41.07% def licenseKeyFormatting(self, string: str, k: int) -> str: newString = string.upper().replace('-', '')[::-1] group = [] for i in range(0, len(newString), k): group.append(newString[i:i+k]) ...
license-key-formatting
Python3 O(n) || O(n) # Runtime: 77ms 53.47% ; Memory: 14.7mb 41.07%
arshergon
2
104
license key formatting
482
0.432
Easy
8,470
https://leetcode.com/problems/license-key-formatting/discuss/1279558/Python3-dollarolution
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = s.replace('-','') s = list(s) for i in range(1,len(s)): if i % k == 0: s[-i] = '-' + s[-i] return (''.join(s)).upper()
license-key-formatting
Python3 $olution
AakRay
2
177
license key formatting
482
0.432
Easy
8,471
https://leetcode.com/problems/license-key-formatting/discuss/1246243/Python3-simple-solution
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: n = s.replace('-','').upper() res = [] x = len(n) if x%k != 0: res.append(n[:x%k]) n = n[x%k:] x = len(n) for i in range(0,x,k): res.append(n[i:i+k]) ...
license-key-formatting
Python3 simple solution
EklavyaJoshi
2
122
license key formatting
482
0.432
Easy
8,472
https://leetcode.com/problems/license-key-formatting/discuss/2434019/PYTHON-code-faster-than-99.18
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = s.replace("-","") if len(s) <= k : return s.upper() if len(s)%k == 0 : return "-".join(s[i:i+k].upper() for i in range(0,len(s),k)) else : return s[:len(s)%k].upper() ...
license-key-formatting
PYTHON code faster than 99.18% ❤️‍🔥❤️‍🔥❤️‍🔥
bharath391
1
115
license key formatting
482
0.432
Easy
8,473
https://leetcode.com/problems/license-key-formatting/discuss/1925287/Python-or-using-%3A%3A-1-and-divmod
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s=''.join(s.split('-')).upper()[::-1] q,r=divmod(len(s),k) if r==0: return '-'.join([s[k*i:k*(i+1)] for i in range(q)])[::-1] else: return '-'.join([s[k*i:k*(i+1)] for i in range(q)]+[s[-r:...
license-key-formatting
Python | using [::-1] and divmod
ginaaunchat
1
68
license key formatting
482
0.432
Easy
8,474
https://leetcode.com/problems/license-key-formatting/discuss/1472448/python-3-or-36-ms-faster-than-92.29
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = s.upper().replace('-', '')[::-1] slots = math.ceil(len(s) / k) start = 0 end = k res = '' for i in range(1, slots+1): if i == slots: res += s[start:end] ...
license-key-formatting
python 3 | 36 ms, faster than 92.29%
deep765
1
171
license key formatting
482
0.432
Easy
8,475
https://leetcode.com/problems/license-key-formatting/discuss/1304636/python-easy-to-understand
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = s.upper() result = [] count = 0 s = s.replace('-', '') for index,char in enumerate(reversed(s)): result.append(char) count +=1 if count ==k and index != le...
license-key-formatting
python easy to understand
moonchild_1
1
168
license key formatting
482
0.432
Easy
8,476
https://leetcode.com/problems/license-key-formatting/discuss/2821754/Simple-Python-Solution
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: uppercase_s = '' for char in s: if char.isalnum(): uppercase_s += char.upper() len_upper = len(uppercase_s) if len_upper <= k: return uppercase_s if len_upper % k ==...
license-key-formatting
Simple Python Solution
don_masih
0
4
license key formatting
482
0.432
Easy
8,477
https://leetcode.com/problems/license-key-formatting/discuss/2811167/Simple-solution
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s=s.upper().replace('-','') init=len(s)%k or k t=s[:init] for z in range(init,len(s),k):t+='-'+s[z:z+k] return t
license-key-formatting
Simple solution
alex41542
0
3
license key formatting
482
0.432
Easy
8,478
https://leetcode.com/problems/license-key-formatting/discuss/2771704/License-Key-Formatting-python-O(n)
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: output = "" n = len(s) i = 0 pos = n-1 while pos>=0: char = s[pos] pos -= 1 if char == "-": continue char = char.upper() if i%k=...
license-key-formatting
License Key Formatting - python - O(n)
DavidCastillo
0
3
license key formatting
482
0.432
Easy
8,479
https://leetcode.com/problems/license-key-formatting/discuss/2688249/Simplest-Python3-solution-82.71-of-Python3-online-submissions
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = s[::-1] res = "" count = 0 for i, c in enumerate(s): if c == "-": continue if count == k: res += "-" count = 0 res += c.uppe...
license-key-formatting
Simplest Python3 solution - 82.71% of Python3 online submissions
himeldas
0
5
license key formatting
482
0.432
Easy
8,480
https://leetcode.com/problems/license-key-formatting/discuss/2671273/Using-replace-method
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s = s.replace('-', "") reminder = len(s) % k i = 0 j = i + k result = s[:reminder].upper() + '-' s = s[reminder:] while i < len(s): result += s[i : j].upper() + '-' ...
license-key-formatting
Using replace method
jokatty
0
6
license key formatting
482
0.432
Easy
8,481
https://leetcode.com/problems/license-key-formatting/discuss/2227784/Python-solution
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: arr = [x for x in s if x != '-'] if not arr: return '' arr = [x if x.isdigit() else x.upper() for x in arr] first_len = len(arr)%k ans = ''.join(arr[:first_len]) arr = arr[first_len:] for i...
license-key-formatting
Python solution
StikS32
0
63
license key formatting
482
0.432
Easy
8,482
https://leetcode.com/problems/license-key-formatting/discuss/2073224/One-more-Python3-solution
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: new_s = s.replace('-', '')[::-1].upper() # Remvoe '-', reverse and uppercase list_s = [new_s[i:i+k] for i in range(0, len(new_s), k)] # Split string into groups of k chars key = '-'.join(list_s) # join with '-' char ...
license-key-formatting
One more Python3 solution
thanhinterpol
0
51
license key formatting
482
0.432
Easy
8,483
https://leetcode.com/problems/license-key-formatting/discuss/1922961/Python-solution-faster-than-96
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: key_str = ("".join(s.split('-'))).upper()[::-1] res = "" for i in range(0, len(key_str), k): res += key_str[i:i+k] + "-" res = res[:len(res)-1] return res[::-1]
license-key-formatting
Python solution faster than 96%
alishak1999
0
127
license key formatting
482
0.432
Easy
8,484
https://leetcode.com/problems/license-key-formatting/discuss/1762704/Python-Bored-solution-40ms
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s=s.replace("-","")[::-1] rel="" for i in range(0,len(s),k): rel+=s[i:i+k] rel+="-" rel=rel[:len(rel)-1] rel=rel[::-1] return rel.upper()
license-key-formatting
Python Bored solution 40ms
AjayKadiri
0
74
license key formatting
482
0.432
Easy
8,485
https://leetcode.com/problems/license-key-formatting/discuss/1744847/python-one-pass-without-indexing-beats-90
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: rev = ''.join(s.split('-'))[::-1] j=0 newrev = '' for char in rev: if j==k: newrev=newrev+'-'+char j=0 else: newrev+=char j+=1 ...
license-key-formatting
python one-pass without indexing, beats 90%
jBloodless
0
119
license key formatting
482
0.432
Easy
8,486
https://leetcode.com/problems/license-key-formatting/discuss/1738869/beats-87-python3-submission
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s=s.replace("-","").upper() s=s[::-1] ans=[] for i in range(0,len(s),k): (ans.append(s[i:i+k])) ans.append('-') if(len(ans)!=0): ans.pop() ans=("".join(str(e) fo...
license-key-formatting
beats 87% python3 submission
aashutoshjha21022002
0
59
license key formatting
482
0.432
Easy
8,487
https://leetcode.com/problems/license-key-formatting/discuss/1444933/Python3-Faster-Than-99
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: s, ss = s.replace('-', ''), "" if k == 1: return "".join([i.upper() + '-' for i in s])[:-1] ratio, left = divmod(len(s), k) j, z = 0, k if left == 0: for i i...
license-key-formatting
Python3 Faster Than 99%
Hejita
0
107
license key formatting
482
0.432
Easy
8,488
https://leetcode.com/problems/license-key-formatting/discuss/1222422/python3-472ms
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: res = [] stack = [] for each in s: if each.isalnum(): stack.append(each) str_tracker = '' while stack: runner = stack.pop() str_tracker = runner + str_tr...
license-key-formatting
python3 472ms
dev-shrestha
0
49
license key formatting
482
0.432
Easy
8,489
https://leetcode.com/problems/license-key-formatting/discuss/1011458/Easy-and-simple-solution-with-explanation
class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: if set(s)=={'-'}: #This is for those cases in which string contains only "-" character return "" s1="".join(s.split("-")) #This returns string s but without the dashes q,r=divmod(len(s1),k) l=[] '''...
license-key-formatting
Easy and simple solution with explanation
thisisakshat
0
99
license key formatting
482
0.432
Easy
8,490
https://leetcode.com/problems/license-key-formatting/discuss/374472/Simon's-Note-Python3-85.24-Easy-to-understand
class Solution: def licenseKeyFormatting(self, S: str, K: int) -> str: S=S.replace('-','') S=S.upper() keep_num=len(S)%K temp=[] if keep_num!=0: temp.append(S[:keep_num]) idx=keep_num while idx<len(S): temp.append(S[idx:idx+K]) ...
license-key-formatting
[🎈Simon's Note🎈] Python3 85.24% Easy to understand
SunTX
0
103
license key formatting
482
0.432
Easy
8,491
https://leetcode.com/problems/license-key-formatting/discuss/332289/Solution-using-Python-3
class Solution: def licenseKeyFormatting(self, S: str, K: int) -> str: S = "".join([i for i in S if i != '-']).upper() L = len(S) t = L%K if t == 0: t = K T = S[0:t] + '-' for i in range(t,L,K): T = T + S[i:i+K] + '-' return(T[0:-1]) - Python 3 - Junaid Mansuri
license-key-formatting
Solution using Python 3
junaidmansuri
0
479
license key formatting
482
0.432
Easy
8,492
https://leetcode.com/problems/smallest-good-base/discuss/2368982/faster-than-97.27-or-python
class Solution: def smallestGoodBase(self, n: str) -> str: import math n = int(n) max_m = math.floor(math.log(n, 2)) ans = 0 for m in range(max_m, 0, -1): k = int(n ** (1 / m)) if (k ** (m + 1) - 1) // (k - 1) == n: return str(k) ...
smallest-good-base
faster than 97.27% | python
vimla_kushwaha
1
107
smallest good base
483
0.384
Hard
8,493
https://leetcode.com/problems/smallest-good-base/discuss/1605051/Python3-enumerate-powers
class Solution: def smallestGoodBase(self, n: str) -> str: n = int(n) for p in range(int(log2(n)), 1, -1): k = int(n**(1/p)) if (k**(p+1)-1)//(k-1) == n: return str(k) return str(n-1)
smallest-good-base
[Python3] enumerate powers
ye15
1
187
smallest good base
483
0.384
Hard
8,494
https://leetcode.com/problems/max-consecutive-ones/discuss/1011637/Simple-and-easy-if-else-solution-faster-than-99.91
class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: c1,c2=0,0 for i in nums: if i==1: c1+=1 elif i==0: c1=0 if c1>c2: c2=c1 return c2
max-consecutive-ones
Simple and easy if-else solution, faster than 99.91%
thisisakshat
8
688
max consecutive ones
485
0.561
Easy
8,495
https://leetcode.com/problems/max-consecutive-ones/discuss/2177951/Python3-O(n)-oror-O(1)-Runtime%3A-359ms-91.92-Memory%3A-14.3mb-78.65
class Solution: # O(n) || O(1) # Runtime: 359ms 91.92% ; Memory: 14.3mb 78.65% def findMaxConsecutiveOnes(self, nums: List[int]) -> int: count = 0 maxCount = 0 for idx, num in enumerate(nums): if num == 1: count += 1 if num == 0 or idx == len(nums...
max-consecutive-ones
Python3 O(n) || O(1) # Runtime: 359ms 91.92% ; Memory: 14.3mb 78.65%
arshergon
4
192
max consecutive ones
485
0.561
Easy
8,496
https://leetcode.com/problems/max-consecutive-ones/discuss/2028572/Easy-Python-Solution!
class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: res = 0 tmp = 0 for num in nums: if num == 0: tmp = 0 else: tmp += 1 res = max(res, tmp) return res # Time: O(N) # Spac...
max-consecutive-ones
Easy Python Solution!
samirpaul1
3
203
max consecutive ones
485
0.561
Easy
8,497
https://leetcode.com/problems/max-consecutive-ones/discuss/2193210/Python-oror-Easy-Approach
class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: ans = 0 j = 0 for i in range(len(nums)): if nums[i] == 1: j += 1 else: j = 0 ans = ma...
max-consecutive-ones
✅Python || Easy Approach
chuhonghao01
2
78
max consecutive ones
485
0.561
Easy
8,498
https://leetcode.com/problems/max-consecutive-ones/discuss/1129824/Simple-solution-in-python-with-explanation-or-Faster-than-97
class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: max_ = 0 #maximum count c = 0 #counter for i in nums: if i == 1: c += 1 if max_ < c: max_ = c else: ...
max-consecutive-ones
Simple solution in python with explanation | Faster than 97%
Annushams
2
463
max consecutive ones
485
0.561
Easy
8,499