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/missing-number/discuss/2137533/python-xor-explaination
class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) return n*(n+1) // 2 - sum(nums)
missing-number
python xor explaination
writemeom
1
102
missing number
268
0.617
Easy
4,900
https://leetcode.com/problems/missing-number/discuss/2092996/PYTHON-1-Line-Solution
class Solution: def missingNumber(self, nums: List[int]) -> int: return [el for el in set([x for x in range(0,len(nums)+1)]) ^ set(nums)][0];
missing-number
PYTHON 1 Line Solution
zeyf
1
107
missing number
268
0.617
Easy
4,901
https://leetcode.com/problems/missing-number/discuss/2081411/Python-solution-using-sum-of-n-natural-numbers
class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) act = (n*n + n)//2 given_sum = sum(nums) missing = act - given_sum return missing
missing-number
Python solution using sum of n natural numbers
testbugsk
1
34
missing number
268
0.617
Easy
4,902
https://leetcode.com/problems/missing-number/discuss/2025573/PYTHONor-SIMPLE-SUM-SOLUTION
class Solution: def missingNumber(self, nums: List[int]) -> int: res = len(nums) for i in range(len(nums)): res += (i - nums[i]) return res
missing-number
PYTHON| SIMPLE SUM SOLUTION
shikha_pandey
1
118
missing number
268
0.617
Easy
4,903
https://leetcode.com/problems/missing-number/discuss/1815479/Simplest-Python-Solution-oror-Beg-to-Adv
class Solution: def missingNumber(self, nums: List[int]) -> int: numofelem = len(nums) sumofnums = sum(nums) total = numofelem * (numofelem + 1) // 2 return total - sumofnums
missing-number
Simplest Python Solution || Beg to Adv
rlakshay14
1
190
missing number
268
0.617
Easy
4,904
https://leetcode.com/problems/missing-number/discuss/1642325/Python-3-oror-2-line-Solution-oror-Using-Mathemtical-calculation-oror-Self-understandable
class Solution: def missingNumber(self, nums: List[int]) -> int: return (len(nums)*(len(nums)+1))//2-sum(nums)
missing-number
Python 3 || 2-line Solution || Using Mathemtical calculation || Self understandable
bug_buster
1
79
missing number
268
0.617
Easy
4,905
https://leetcode.com/problems/missing-number/discuss/1637692/Python3-Bitwise-Solution-Easy
class Solution: def missingNumber(self, nums: List[int]) -> int: xor_of_all = 0 xor_of_arr = 0 n = len(nums) for i in range(n + 1): xor_of_all ^= i for n in nums: xor_of_arr ^= n return xor_of_all ^ xor_of_arr
missing-number
Python3 Bitwise Solution Easy
dahal_
1
112
missing number
268
0.617
Easy
4,906
https://leetcode.com/problems/missing-number/discuss/1463792/compare-both-code-python3-c%2B%2B-and-different-ways-easy-to-understand-beats-99
class Solution: def missingNumber(self, nums: List[int]) -> int: ans=0 for a,b in enumerate(nums): ans^=a+1 ans^=b return ans
missing-number
compare both code [python3 /c++] and different ways easy to understand beats 99%
sagarhparmar12345
1
65
missing number
268
0.617
Easy
4,907
https://leetcode.com/problems/missing-number/discuss/1463792/compare-both-code-python3-c%2B%2B-and-different-ways-easy-to-understand-beats-99
class Solution: def missingNumber(self, nums: List[int]) -> int: n=len(nums) return (n**2 +n)//2 - sum(nums)
missing-number
compare both code [python3 /c++] and different ways easy to understand beats 99%
sagarhparmar12345
1
65
missing number
268
0.617
Easy
4,908
https://leetcode.com/problems/missing-number/discuss/1399688/Python-or-XOR
class Solution: def missingNumber(self, nums: List[int]) -> int: xor = 0 for i in range(len(nums)): xor ^= (i+1)^nums[i] return xor
missing-number
Python | XOR
sathwickreddy
1
307
missing number
268
0.617
Easy
4,909
https://leetcode.com/problems/missing-number/discuss/1308515/Python3-faster-than-90.56-O(2n)
class Solution: def missingNumber(self, nums: List[int]) -> int: allSum = sum(nums) expect = 0 for i in range(len(nums) + 1): expect += i return expect - allSum
missing-number
Python3 - faster than 90.56%, O(2n)
CC_CheeseCake
1
58
missing number
268
0.617
Easy
4,910
https://leetcode.com/problems/missing-number/discuss/1158407/Python3-Single-Line-Solution
class Solution: def missingNumber(self, nums: List[int]) -> int: return sum(range(len(nums) + 1)) - sum(nums)
missing-number
[Python3] Single Line Solution
Lolopola
1
61
missing number
268
0.617
Easy
4,911
https://leetcode.com/problems/missing-number/discuss/1138517/One-line-python-solution
class Solution: def missingNumber(self, nums: List[int]) -> int: n=len(nums) return (n*(n+1)//2)-sum(nums)
missing-number
One line python solution
samarthnehe
1
111
missing number
268
0.617
Easy
4,912
https://leetcode.com/problems/missing-number/discuss/1092310/Python-A-couple-clean-one-liners
class Solution: def missingNumber(self, nums: List[int]) -> int: return sum(x - y for x,y in enumerate(nums, start=1))
missing-number
Python - A couple clean one liners
jep4444
1
42
missing number
268
0.617
Easy
4,913
https://leetcode.com/problems/missing-number/discuss/1092310/Python-A-couple-clean-one-liners
class Solution: def missingNumber(self, nums: List[int]) -> int: return functools.reduce(operator.xor, itertools.starmap(operator.xor, enumerate(nums, start=1)))
missing-number
Python - A couple clean one liners
jep4444
1
42
missing number
268
0.617
Easy
4,914
https://leetcode.com/problems/missing-number/discuss/1091215/Missing-NumberPython-Hints-and-easy-explanation
class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) sum_n = n*(n+1)//2 sum_nums = sum(nums) return sum_n - sum_nums
missing-number
[Missing Number][Python] Hints and easy explanation
sahilnishad
1
33
missing number
268
0.617
Easy
4,915
https://leetcode.com/problems/missing-number/discuss/428261/Python-simple-and-easy-to-understand
class Solution: def missingNumber(self, nums): expected = 0 actual = 0 for i,num in enumerate(nums): expected += i+1 actual += num return expected-actual
missing-number
Python - simple and easy to understand
domthedeveloper
1
114
missing number
268
0.617
Easy
4,916
https://leetcode.com/problems/missing-number/discuss/428261/Python-simple-and-easy-to-understand
class Solution: def missingNumber(self, nums): return sum(range(len(nums)+1))-sum(nums)
missing-number
Python - simple and easy to understand
domthedeveloper
1
114
missing number
268
0.617
Easy
4,917
https://leetcode.com/problems/missing-number/discuss/428261/Python-simple-and-easy-to-understand
class Solution: def missingNumber(self, nums): n,s = len(nums),sum(nums) return n*(n+1)//2-s
missing-number
Python - simple and easy to understand
domthedeveloper
1
114
missing number
268
0.617
Easy
4,918
https://leetcode.com/problems/missing-number/discuss/428261/Python-simple-and-easy-to-understand
class Solution: def missingNumber(self, nums): return (lambda n,s : n*(n+1)//2-s)(len(nums),sum(nums))
missing-number
Python - simple and easy to understand
domthedeveloper
1
114
missing number
268
0.617
Easy
4,919
https://leetcode.com/problems/missing-number/discuss/2845604/python-beats-98.68-or-beats-5
class Solution: def missingNumber(self, nums: List[int]) -> int: return sum(list(range(len(nums)+1))) - sum(nums)
missing-number
[python] - beats 98.68% | beats 5%
ceolantir
0
2
missing number
268
0.617
Easy
4,920
https://leetcode.com/problems/missing-number/discuss/2845604/python-beats-98.68-or-beats-5
class Solution: def missingNumber(self, nums: List[int]) -> int: for i in sum(len(range(len(nums)+1))): if i not in nums: return i
missing-number
[python] - beats 98.68% | beats 5%
ceolantir
0
2
missing number
268
0.617
Easy
4,921
https://leetcode.com/problems/missing-number/discuss/2841207/python-oror-simple-solution-oror-o(n)-time-o(1)-space
class Solution: def missingNumber(self, nums: List[int]) -> int: ans = 0 # answer # calculate triangle num for size of nums, then subtract every num in nums for i in range(len(nums)): ans += i + 1 - nums[i] # left with missing num return ans
missing-number
python || simple solution || o(n) time, o(1) space
wduf
0
4
missing number
268
0.617
Easy
4,922
https://leetcode.com/problems/missing-number/discuss/2840188/Super-Simple-Python-Solution
class Solution: def missingNumber(self, nums: List[int]) -> int: length = len(nums) sum_true_nums = 0 for num in range(0, length + 1): sum_true_nums += num return sum_true_nums - sum(nums)
missing-number
Super Simple Python Solution
corylynn
0
3
missing number
268
0.617
Easy
4,923
https://leetcode.com/problems/missing-number/discuss/2837363/Python-Beats-97.73-of-runtime-complexity
class Solution: def missingNumber(self, nums: List[int]) -> int: expected_nums = { n: 0 for n in range(0, len(nums)+1) } for n in nums: expected_nums[n] = 1 for n, seen in expected_nums.items(): if not seen: return n
missing-number
[Python] Beats 97.73% of runtime complexity
jyoo
0
3
missing number
268
0.617
Easy
4,924
https://leetcode.com/problems/missing-number/discuss/2837140/python-easy-solution
class Solution: def missingNumber(self, nums): a = sum(range(len(nums)+1)); b = sum(nums); return (a-b);
missing-number
python easy solution
seifsoliman
0
1
missing number
268
0.617
Easy
4,925
https://leetcode.com/problems/missing-number/discuss/2833795/Python-solution
class Solution: def missingNumber(self, nums: List[int]) -> int: #nums.sort() list1=[x for x in range(0,len(nums)+1) if x not in nums] print(list1[0]) return list1[0]
missing-number
Python solution
user1079z
0
1
missing number
268
0.617
Easy
4,926
https://leetcode.com/problems/integer-to-english-words/discuss/1990823/JavaC%2B%2BPythonJavaScriptKotlinSwiftO(n)timeBEATS-99.97-MEMORYSPEED-0ms-APRIL-2022
class Solution: def numberToWords(self, num: int) -> str: mp = {1: "One", 11: "Eleven", 10: "Ten", 2: "Two", 12: "Twelve", 20: "Twenty", 3: "Three", 13: "Thirteen", 30: "Thirty", 4: "Four", 14: "Fourteen", 40: "Forty", 5: "Five", 15: "...
integer-to-english-words
[Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022
cucerdariancatalin
8
677
integer to english words
273
0.299
Hard
4,927
https://leetcode.com/problems/integer-to-english-words/discuss/1939386/short-and-easy-to-understand-Python-code-using-dictionary
class Solution: def numberToWords(self, num: int) -> str: if num == 0 : return 'Zero' #if condition to handle zero d = {1000000000 : 'Billion',1000000 : 'Million',1000 : 'Thousand',100 : 'Hundred', 90:'Ninety',80:'Eighty',70:'Seventy',60:'Sixty',50: 'Fifty', 40 : 'Forty', 30 : 'Thirty', 2...
integer-to-english-words
short & easy to understand Python code using dictionary
TheBatmanIRL
7
397
integer to english words
273
0.299
Hard
4,928
https://leetcode.com/problems/integer-to-english-words/discuss/764375/Python3-straightforward
class Solution: def numberToWords(self, num: int) -> str: mp = {1: "One", 11: "Eleven", 10: "Ten", 2: "Two", 12: "Twelve", 20: "Twenty", 3: "Three", 13: "Thirteen", 30: "Thirty", 4: "Four", 14: "Fourteen", 40: "Forty", 5: "Five", 15: "...
integer-to-english-words
[Python3] straightforward
ye15
4
211
integer to english words
273
0.299
Hard
4,929
https://leetcode.com/problems/integer-to-english-words/discuss/2021835/Easy-to-understand-recursive-python-solution
class Solution: def numberToWords(self, num: int) -> str: if not num: return 'Zero' ones = {1:' One', 2:' Two', 3:' Three', 4:' Four', 5:' Five', 6:' Six', 7:' Seven', 8:' Eight', 9:' Nine', 10:' Ten', 11:' Eleven', 12:' Twelve', 13:' Thirteen', 14:' Fourteen', 15:' Fifteen', 16:' Sixteen', ...
integer-to-english-words
Easy to understand recursive python solution
Kasra_G
2
214
integer to english words
273
0.299
Hard
4,930
https://leetcode.com/problems/integer-to-english-words/discuss/2167564/Elegant-Recursive-Solution-in-Python
class Solution: def numberToWords(self, num: int) -> str: if num == 0: return 'Zero' billion, million, thousand, hundred = 10 ** 9, 10 ** 6, 10 ** 3, 10 ** 2 mapping = { 0: '', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Fou...
integer-to-english-words
Elegant Recursive Solution in Python
wangzhihao0629
1
197
integer to english words
273
0.299
Hard
4,931
https://leetcode.com/problems/integer-to-english-words/discuss/1918243/Python-Straightforward-Solution
class Solution: def numberToWords(self, num: int) -> str: if num == 0: return 'Zero' ones = {1: "One", 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: ...
integer-to-english-words
Python Straightforward Solution
zoran3
1
126
integer to english words
273
0.299
Hard
4,932
https://leetcode.com/problems/integer-to-english-words/discuss/2825187/Easy-To-Follow-Recursive-Python
class Solution: def numberToWords(self, num: int) -> str: to_19 = {0:'Zero', 1:'One',2:'Two',3:'Three',4:'Four', 5:'Five', 6:'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10:'Ten', 11: 'Eleven', 12: 'Twelve', 13:'Thirteen', 14:"Fourteen",15:"Fifteen",16:"Sixteen",17:"Seventeen",18:"Eighteen",19:"Nineteen"} ...
integer-to-english-words
Easy To Follow Recursive Python
taabish_khan22
0
12
integer to english words
273
0.299
Hard
4,933
https://leetcode.com/problems/integer-to-english-words/discuss/2729086/PYTHON-Simple-Fast-Solution-Cultural-%22elibelinde%22-approach
class Solution: def numberToWords(self, num: int) -> str: tens = {11: "Eleven", 12: "Twelve", 13: "Thirteen", 14: "Fourteen", 15: "Fifteen", 16: "Sixteen", 17: "Seventeen", 18: "Eighteen", 19: "Nineteen"} o = ["Zero","One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] t ...
integer-to-english-words
PYTHON - Simple Fast Solution Cultural "elibelinde" approach
BladeRunner61
0
35
integer to english words
273
0.299
Hard
4,934
https://leetcode.com/problems/integer-to-english-words/discuss/2593570/Python3-Solution-still-needs-a-lot-of-refacturing-but-works
class Solution: def numberToWords(self, num: int) -> str: pairs = {0:"", 1:"One", 2:"Two", 3:"Three", 4:"Four", 5:"Five", 6:"Six", 7:"Seven", 8:"Eight", 9:"Nine",10:"Ten",11:"Eleven", 12:"Twelve", 13:"Thirteen", 14:"Fourteen", 15:"Fifteen", 16:"Sixteen", 17:"Seventeen", 18:"Eighteen", ...
integer-to-english-words
Python3 Solution- still needs a lot of refacturing but works
biobox
0
103
integer to english words
273
0.299
Hard
4,935
https://leetcode.com/problems/integer-to-english-words/discuss/2581813/Python-runtime-O(n)-memory-O(1)
class Solution: def numberToWords(self, num: int) -> str: def one(num): switcher = { 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eig...
integer-to-english-words
Python, runtime O(n), memory O(1)
tsai00150
0
137
integer to english words
273
0.299
Hard
4,936
https://leetcode.com/problems/integer-to-english-words/discuss/2546738/Python-3-or-Partial-Process-or-Keep-the-details-on-mind.-OwO
class Solution: def numberToWords(self, num: int) -> str: if num == 0: return 'Zero' base = ( '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'...
integer-to-english-words
Python 3 | Partial Process | Keep the details on mind. OwO
km4sh
0
96
integer to english words
273
0.299
Hard
4,937
https://leetcode.com/problems/integer-to-english-words/discuss/2479438/Python3-Clear-solution-with-explanation
class Solution: def numberToWords(self, num: int) -> str: # This approach handles the number in 3 digit chunks. Let's start by creating some maps of english words - ones, tens, yuck, and mult # These have aligned indecies. ones[1] = "one". tens[4] = "forty". # the 'yuck' map will help us...
integer-to-english-words
[Python3] Clear solution with explanation
connorthecrowe
0
102
integer to english words
273
0.299
Hard
4,938
https://leetcode.com/problems/integer-to-english-words/discuss/2245534/Python-table-solution
class Solution: def convert(self, rem): d = { 0: ["Zero", "", "Ten"], 1: ["One", "", "Eleven"], 2: ["Two", "Twenty", "Twelve"], 3: ["Three", "Thirty", "Thirteen"], 4: ["Four", "Forty", "Fourteen"], 5: ["Five", "Fifty", "Fifteen"], ...
integer-to-english-words
Python table solution
corvofeng
0
105
integer to english words
273
0.299
Hard
4,939
https://leetcode.com/problems/integer-to-english-words/discuss/2124014/Easy-divide-and-conquer-Python3-solution
class Solution: def __init__(self): self.words = { 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', 11: 'Eleven', ...
integer-to-english-words
Easy divide and conquer Python3 solution
mwgarrett9936
0
209
integer to english words
273
0.299
Hard
4,940
https://leetcode.com/problems/integer-to-english-words/discuss/1989619/Python-Ad-hoc-approach
class Solution: ones = { "1": "One", "2": "Two", "3": "Three", "4": "Four", "5": "Five", "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine" } tens = { "1": "Ten", "2": "Twenty", "3": "Thirty", "4": "Fort...
integer-to-english-words
Python Ad-hoc approach
SajLeet
0
116
integer to english words
273
0.299
Hard
4,941
https://leetcode.com/problems/integer-to-english-words/discuss/1976269/Python-Solution
class Solution: def numberToWords(self, num: int) -> str: word_bank = { 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", ...
integer-to-english-words
Python Solution
EddieAtari
0
125
integer to english words
273
0.299
Hard
4,942
https://leetcode.com/problems/integer-to-english-words/discuss/1962240/Python-or-Dictionary-or-Easy-Solution
class Solution: def numberToWords(self, num: int) -> str: """ English words has different names for 1 - 19 Then 20, 30, 40, 50, 60, 70, 80, 90, 100 1000 1000000 1000000 Consider them as bills, then this problem transform into...
integer-to-english-words
Python | Dictionary | Easy Solution
showing_up_each_day
0
204
integer to english words
273
0.299
Hard
4,943
https://leetcode.com/problems/integer-to-english-words/discuss/1941018/Python3-Solution
class Solution: def numberToWords(self, num: int) -> str: if num == 0: return "Zero" placeInArraySuffix = {1:"Thousand", 2:"Million", 3:"Billion"} tens = {2:"Twenty",3:"Thirty", 4:"Forty", 5:"Fifty", 6:"Sixty",7:"Seventy",8:"Eighty",9:"Ninety"} ones = {1...
integer-to-english-words
Python3 Solution
DietCoke777
0
91
integer to english words
273
0.299
Hard
4,944
https://leetcode.com/problems/integer-to-english-words/discuss/1846853/Python-Simple-Solution!-Helper-Function
class Solution(object): def numberToWords(self, num): if num == 0: return "Zero" powersOfThousand = ["", "Thousand", "Million", "Billion"] fullName, powerOfThousand = "", 0 while num: name = self.helper(num%1000) if len(name) >= 1: ...
integer-to-english-words
Python - Simple Solution! Helper Function
domthedeveloper
0
174
integer to english words
273
0.299
Hard
4,945
https://leetcode.com/problems/integer-to-english-words/discuss/1494424/Simple-Python-Solution
class Solution: def numberToWords(self, num: int) -> str: mapping = { 10**9: "Billion", 10**6: "Million", 10**3: "Thousand", 10**2: "Hundred", 90: "Ninety", 80: "Eighty", 70: "Seventy", 60: "Sixty", ...
integer-to-english-words
Simple Python Solution
iahouma
0
243
integer to english words
273
0.299
Hard
4,946
https://leetcode.com/problems/integer-to-english-words/discuss/914458/Python-easy-solution-(split-by-functions)
class Solution: def numberToWords(self, num: int) -> str: def ones(num): vals = { 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven",...
integer-to-english-words
Python easy solution (split by functions)
ermolushka2
0
199
integer to english words
273
0.299
Hard
4,947
https://leetcode.com/problems/integer-to-english-words/discuss/389550/Python-Solution-(Dictionaries)
class Solution: def numberToWords(self, num: int) -> str: ones = {1:'One', 2:'Two', 3:'Three', 4:'Four', 5:'Five', 6:'Six', 7:'Seven', 8:'Eight', 9:'Nine'} teens = {10:'Ten', 11:'Eleven', 12:'Twelve', 13:'Thirteen', 14:'Fourteen', 15:'Fifteen', 16:'Sixteen', 17:'Sev...
integer-to-english-words
Python Solution (Dictionaries)
FFurus
0
184
integer to english words
273
0.299
Hard
4,948
https://leetcode.com/problems/h-index/discuss/785586/Python3-O(n)-without-sorting!
class Solution: def hIndex(self, citations: List[int]) -> int: tmp = [0] * (len(citations) + 1) for i in range(len(citations)): if citations[i] > len(citations): tmp[len(citations)] += 1 else: tmp[citations[i]] += 1 sum_ = 0 ...
h-index
Python3 O(n) without sorting!
DebbieAlter
5
489
h index
274
0.382
Medium
4,949
https://leetcode.com/problems/h-index/discuss/1872013/O(n)-Easy-short-Python-Code-with-Explanation
class Solution: def hIndex(self, citations: List[int]) -> int: n = len(citations) + 1 arr = [0] * n for c in citations: if c >= n: arr[n-1] += 1 else: arr[c] += 1 total = 0 for i in range(n-1, -1, -1): ...
h-index
O(n) Easy short Python Code with Explanation
jlu56
1
207
h index
274
0.382
Medium
4,950
https://leetcode.com/problems/h-index/discuss/694269/Python3-four-1-liners
class Solution: def hIndex(self, citations: List[int]) -> int: return sum(i < x for i, x in enumerate(sorted(citations, reverse=True)))
h-index
[Python3] four 1-liners
ye15
1
54
h index
274
0.382
Medium
4,951
https://leetcode.com/problems/h-index/discuss/694269/Python3-four-1-liners
class Solution: def hIndex(self, citations: List[int]) -> int: return next((len(citations)-i for i, x in enumerate(sorted(citations)) if len(citations)-i <= x), 0)
h-index
[Python3] four 1-liners
ye15
1
54
h index
274
0.382
Medium
4,952
https://leetcode.com/problems/h-index/discuss/694269/Python3-four-1-liners
class Solution: def hIndex(self, citations: List[int]) -> int: return max((i+1 for i, x in enumerate(sorted(citations, reverse=True)) if i < x), default=0)
h-index
[Python3] four 1-liners
ye15
1
54
h index
274
0.382
Medium
4,953
https://leetcode.com/problems/h-index/discuss/694269/Python3-four-1-liners
class Solution: def hIndex(self, citations: List[int]) -> int: return max((min(i, x) for i, x in enumerate(sorted(citations, reverse=True), 1)), default=0)
h-index
[Python3] four 1-liners
ye15
1
54
h index
274
0.382
Medium
4,954
https://leetcode.com/problems/h-index/discuss/694269/Python3-four-1-liners
class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort() n = len(citations) lo, hi = 0, n while lo < hi: mid = (lo + hi)//2 if citations[mid] >= n - mid: hi = mid else: lo = mid + 1 return n - lo
h-index
[Python3] four 1-liners
ye15
1
54
h index
274
0.382
Medium
4,955
https://leetcode.com/problems/h-index/discuss/387453/Solution-in-Python-3-(beats-~99)-(four-lines)
class Solution: def hIndex(self, c: List[int]) -> int: L = len(c) for i,j in enumerate(sorted(c)): if L - i <= j: return L - i return 0 - Junaid Mansuri (LeetCode ID)@hotmail.com
h-index
Solution in Python 3 (beats ~99%) (four lines)
junaidmansuri
1
517
h index
274
0.382
Medium
4,956
https://leetcode.com/problems/h-index/discuss/2801874/Easy-python-solution-time%3AO(nlogn)-and-space%3AO(1)
class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort() ans = len(citations) for num in citations: if num < ans: ans -= 1 else: break return ans
h-index
Easy python solution, time:O(nlogn) and space:O(1)
jackie890621
0
6
h index
274
0.382
Medium
4,957
https://leetcode.com/problems/h-index/discuss/2353282/Python-implementation-using-bucketing-with-proper-explanation
class Solution: def hIndex(self, citations: List[int]) -> int: """ citations = [3,0,6,1,5] n : length of citations H - index defination: A scientist has an index h if h of their n papers have at least h citations each, and the other n − h papers ha...
h-index
Python implementation using bucketing with proper explanation
Pratyush_Priyam_Kuanr
0
22
h index
274
0.382
Medium
4,958
https://leetcode.com/problems/h-index/discuss/1420745/binary-search-or-python-or-betterthan-92
class Solution: def hIndex(self, citations: List[int]) -> int: low=0 n=len(citations) high=min(n,max(citations)) citations.sort() def fun(mid): b=bisect.bisect_left(citations,mid) if n-b>=mid: return T...
h-index
binary search | python | betterthan 92%
heisenbarg
0
173
h index
274
0.382
Medium
4,959
https://leetcode.com/problems/h-index/discuss/764403/Python3-1-line
class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort(reverse=True) ans = 0 for i, c in enumerate(citations, 1): if c >= i: ans = i return ans
h-index
[Python3] 1-line
ye15
0
94
h index
274
0.382
Medium
4,960
https://leetcode.com/problems/h-index/discuss/764403/Python3-1-line
class Solution: def hIndex(self, citations: List[int]) -> int: return next((len(citations)-i for i, x in enumerate(sorted(citations)) if len(citations)-i <= x), 0)
h-index
[Python3] 1-line
ye15
0
94
h index
274
0.382
Medium
4,961
https://leetcode.com/problems/h-index/discuss/764403/Python3-1-line
class Solution: def hIndex(self, citations: List[int]) -> int: return bisect_right([i-c for i, c in enumerate(sorted(citations, reverse=True), 1)], 0)
h-index
[Python3] 1-line
ye15
0
94
h index
274
0.382
Medium
4,962
https://leetcode.com/problems/h-index/discuss/507343/Python3-4-lines-using-sort()-function
class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort(reverse=True) while citations and citations[-1]<len(citations): citations.pop() return len(citations)
h-index
Python3 4-lines using sort() function
jb07
0
83
h index
274
0.382
Medium
4,963
https://leetcode.com/problems/h-index/discuss/785986/Simple-Python-Solution-Explained
class Solution: def hIndex(self, citations: List[int]) -> int: citations.sort(reverse = True) for indx, citation in enumerate(citations): if indx >= citation: return indx return len(citations)
h-index
Simple Python Solution Explained
spec_he123
-1
197
h index
274
0.382
Medium
4,964
https://leetcode.com/problems/h-index-ii/discuss/2729382/Python3-Solution-or-Binary-Search-or-O(logn)
class Solution: def hIndex(self, A): n = len(A) l, r = 0, n - 1 while l < r: m = (l + r + 1) // 2 if A[m] > n - m: r = m - 1 else: l = m return n - l - (A[l] < n - l)
h-index-ii
✔ Python3 Solution | Binary Search | O(logn)
satyam2001
2
109
h index ii
275
0.374
Medium
4,965
https://leetcode.com/problems/h-index-ii/discuss/694273/Python3-bisect_right-and-bisect_left-H-Index-II
class Solution: def hIndex(self, citations: List[int]) -> int: if not citations: return 0 n = len(citations) lo, hi = 0, n+1 while lo < hi: mid = (lo + hi)//2 if citations[-mid] < mid: hi = mid else: lo =...
h-index-ii
Python3 bisect_right and bisect_left - H-Index II
r0bertz
1
107
h index ii
275
0.374
Medium
4,966
https://leetcode.com/problems/h-index-ii/discuss/694273/Python3-bisect_right-and-bisect_left-H-Index-II
class Solution: def hIndex(self, citations: List[int]) -> int: n = len(citations) lo, hi = 0, n while lo < hi: mid = (lo + hi)//2 if citations[mid] < n - mid: lo = mid + 1 else: hi = mid return n - lo
h-index-ii
Python3 bisect_right and bisect_left - H-Index II
r0bertz
1
107
h index ii
275
0.374
Medium
4,967
https://leetcode.com/problems/h-index-ii/discuss/447298/Python-Solution-with-Binary-Search
class Solution: def hIndex(self, citations: List[int]) -> int: lo, hi = 0, len(citations) while lo < hi: mid = (lo + hi) // 2 if citations[~mid] > mid: lo = mid + 1 else: hi = mid return lo
h-index-ii
Python Solution with Binary Search
Yaxe522
1
208
h index ii
275
0.374
Medium
4,968
https://leetcode.com/problems/h-index-ii/discuss/2785536/H-index
class Solution: def hIndex(self, citations: List[int]) -> int: start=0 n=len(citations) end=len(citations)-1 while start<=end: mid=start+(end-start)//2 if citations[mid]==(n-mid): return citations[mid] elif citations[mid]>(n-mid): ...
h-index-ii
H-index
shivansh2001sri
0
4
h index ii
275
0.374
Medium
4,969
https://leetcode.com/problems/h-index-ii/discuss/2715727/Python-or-O(n)-or-94.36-Fast-85.77-Mem-or-no-div-centering
class Solution: def hIndex(self, citations: List[int]) -> int: max_h = 0 idx = -1 while idx >= -len(citations): if citations[idx] >= abs(idx): max_h = abs(idx) idx -= 1 else: break return max_h
h-index-ii
Python | O(n) | 94.36% Fast, 85.77% Mem | no div centering
lan32
0
7
h index ii
275
0.374
Medium
4,970
https://leetcode.com/problems/h-index-ii/discuss/2647931/Simple-Binary-Search
class Solution: def hIndex(self, citations: List[int]) -> int: n = len(citations) l, r = 0 , n-1 while( l <= r): mid = l + (r -l ) //2 if citations[mid]==(n-mid): return n - mid elif citations[mid] > n-mid: r = mid - 1 ...
h-index-ii
Simple Binary Search
akshitub
0
8
h index ii
275
0.374
Medium
4,971
https://leetcode.com/problems/h-index-ii/discuss/2397393/Binary-search-solution-python3-solution
class Solution: # O(logn) time, # O(1) space, # Approach: binary search, def hIndex(self, citations: List[int]) -> int: n = len(citations) def findLowerBoundIndexToNum(lo, hi, num): while True: mid = (lo+hi)//2 curr = citations[mid] ...
h-index-ii
Binary search solution python3 solution
destifo
0
15
h index ii
275
0.374
Medium
4,972
https://leetcode.com/problems/h-index-ii/discuss/1098396/Very-Short-O(n)-Python3-Solution
class Solution: def hIndex(self, citations: List[int]) -> int: for i in range(len(citations)): if citations[i]>=len(citations)-i: return len(citations)-i return 0
h-index-ii
Very Short O(n) Python3 Solution
yash2709
0
141
h index ii
275
0.374
Medium
4,973
https://leetcode.com/problems/h-index-ii/discuss/737820/Python3-solution-simple-binary-search-llessr-not-use-less
class Solution: def hIndex(self, citations: List[int]) -> int l,r=0,len(citations) while l<r: mid=(l+r-1)//2 if citations[mid]<len(citations)-mid: l=mid+1 else: r=mid return len(citations)-l
h-index-ii
Python3 solution, simple binary search l<r not use <=
ladykkk
0
71
h index ii
275
0.374
Medium
4,974
https://leetcode.com/problems/h-index-ii/discuss/694822/Both-O(n)-and-O(log-n)-solution-in-Python3
class Solution: def hIndex(self, citations: List[int]) -> int: if not citations: return 0 n = len(citations) for i in range(n): if citations[i] >= n-i: return n-i return 0
h-index-ii
Both O(n) and O(log n) solution in Python3
nobi1007
0
49
h index ii
275
0.374
Medium
4,975
https://leetcode.com/problems/h-index-ii/discuss/694822/Both-O(n)-and-O(log-n)-solution-in-Python3
class Solution: def hIndex(self, citations: List[int]) -> int: if not citations: return 0 maxi = 0 n = len(citations) left = 0 right = n-1 while left <= right: mid = ( left + right ) // 2 if c...
h-index-ii
Both O(n) and O(log n) solution in Python3
nobi1007
0
49
h index ii
275
0.374
Medium
4,976
https://leetcode.com/problems/h-index-ii/discuss/694163/Simple-Intuitive-Idiomatic-Python3-Solution-O(logn)
class Solution: def hIndex(self, citations: List[int]) -> int: low, high = 0, len(citations)-1 while low <= high: mid = (low+high) // 2 if citations[mid] >= len(citations)-mid and \ (mid == 0 or citations[mid-1] <= len(citations)-mid): return len(c...
h-index-ii
Simple, Intuitive, Idiomatic Python3 Solution - O(logn)
schedutron
0
55
h index ii
275
0.374
Medium
4,977
https://leetcode.com/problems/h-index-ii/discuss/507348/Python3-super-simple-3-lines-code
class Solution: def hIndex(self, citations: List[int]) -> int: while citations and citations[0]<len(citations): citations.pop(0) return len(citations)
h-index-ii
Python3 super simple 3-lines code
jb07
0
59
h index ii
275
0.374
Medium
4,978
https://leetcode.com/problems/first-bad-version/discuss/2700688/Simple-Python-Solution-Using-Binary-Search
class Solution: def firstBadVersion(self, n: int) -> int: left = 1 right = n result = 1 while left<=right: mid = (left+right)//2 if isBadVersion(mid) == False: left = mid+1 else: right = mid-1 ...
first-bad-version
✔️ Simple Python Solution Using Binary Search 🔥
pniraj657
25
3,400
first bad version
278
0.43
Easy
4,979
https://leetcode.com/problems/first-bad-version/discuss/1743709/Python-Simple-Python-Solution-Using-Binary-Search
class Solution: def firstBadVersion(self, n: int) -> int: result = 1 low = 1 high = n while low <= high: mid = (low + high) //2 if isBadVersion(mid) == False: low = mid + 1 else: high = mid - 1 result = mid return result
first-bad-version
[ Python ] ✅✅ Simple Python Solution Using Binary Search 🔥🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
15
1,200
first bad version
278
0.43
Easy
4,980
https://leetcode.com/problems/first-bad-version/discuss/2180671/Python3-clean-code-faster-than-96
class Solution: def firstBadVersion(self, n: int) -> int: low,high = 1, n while low<=high: mid=(low+high)//2 isBad = isBadVersion(mid) if(isBad): high = mid-1 else: low = mid+1 return low
first-bad-version
📌 Python3 clean code faster than 96%
Dark_wolf_jss
8
288
first bad version
278
0.43
Easy
4,981
https://leetcode.com/problems/first-bad-version/discuss/1633603/28-ms-faster-than-80.64
class Solution: def firstBadVersion(self, n): """ :type n: int :rtype: int """ L, R = 0, n if isBadVersion(1): return 1 while L<R: mid = (L+R)//2 if isBadVersion(mid): R = mid else: ...
first-bad-version
28 ms, faster than 80.64%
OAOrzz
3
475
first bad version
278
0.43
Easy
4,982
https://leetcode.com/problems/first-bad-version/discuss/1425012/Binary-search-99.9-speed
class Solution: def firstBadVersion(self, n): left, right = 1, n if isBadVersion(left): return left while left < right: middle = (left + right) // 2 if isBadVersion(middle): right = middle else: left = middle + 1...
first-bad-version
Binary search, 99.9% speed
EvgenySH
3
587
first bad version
278
0.43
Easy
4,983
https://leetcode.com/problems/first-bad-version/discuss/379775/Solution-in-Python-3-(beats-~98)-(six-lines)
class Solution: def firstBadVersion(self, n): if isBadVersion(1): return 1 b = [1,n] while 1: m, i, j = (b[0]+b[1])//2, b[0], b[1] b = [m,j] if isBadVersion(i) == isBadVersion(m) else [i,m] if j - i == 1: return j - Junaid Mansuri (LeetCode ID)@hot...
first-bad-version
Solution in Python 3 (beats ~98%) (six lines)
junaidmansuri
3
1,200
first bad version
278
0.43
Easy
4,984
https://leetcode.com/problems/first-bad-version/discuss/606725/Python3-binary-search
class Solution: def firstBadVersion(self, n): lo, hi = 1, n while lo < hi: mid = lo + hi >> 1 if isBadVersion(mid): hi = mid else: lo = mid + 1 return lo
first-bad-version
[Python3] binary search
ye15
2
127
first bad version
278
0.43
Easy
4,985
https://leetcode.com/problems/first-bad-version/discuss/2507949/Python-Solution
class Solution: def firstBadVersion(self, n): """ :type n: int :rtype: int """ high = n low = 1 while(high>=low): mid = (low+high)//2 if isBadVersion(mid)== True: high = mid-1 else: low = mid+...
first-bad-version
Python Solution
yashkumarjha
1
102
first bad version
278
0.43
Easy
4,986
https://leetcode.com/problems/first-bad-version/discuss/1952734/java-python-iterative-binary-search-(Time-Ologn-space-O1)
class Solution: def firstBadVersion(self, n: int) -> int: l = 1 while l <= n : m = (l + n)>>1 if isBadVersion(m) : n = m - 1 else : l = m + 1 return l
first-bad-version
java, python - iterative binary search (Time Ologn, space O1)
ZX007java
1
143
first bad version
278
0.43
Easy
4,987
https://leetcode.com/problems/first-bad-version/discuss/1621987/Python3-Another-solution-with-condition.-Beats-94.53
class Solution: def firstBadVersion(self, n): """ :type n: int :rtype: int """ left, right = 0, n if isBadVersion(1): return 1 while left < right: middle = (left + right) // 2 if isBadVersion(middle): right =...
first-bad-version
[Python3] Another solution with condition. Beats 94.53%
Fyzzys
1
263
first bad version
278
0.43
Easy
4,988
https://leetcode.com/problems/first-bad-version/discuss/1378350/Easy-Python-O(log-n)-Solution-(Faster-than-99)
class Solution: def firstBadVersion(self, n): f = n l, h = 0, n+1 while l < h: m = l + (h-l)//2 if isBadVersion(m): if m < f: f = m h = m else: l = m+1 return f
first-bad-version
Easy Python O(log n) Solution (Faster than 99%)
the_sky_high
1
243
first bad version
278
0.43
Easy
4,989
https://leetcode.com/problems/first-bad-version/discuss/2842919/python-oror-simple-solution-oror-binary-search
class Solution: def firstBadVersion(self, n: int) -> int: # binary search l = 1 # left r = n # right while l <= r: m = (l + r) // 2 # mid if isBadVersion(m): r = m - 1 else: l = m + 1 return l
first-bad-version
python || simple solution || binary search
wduf
0
2
first bad version
278
0.43
Easy
4,990
https://leetcode.com/problems/first-bad-version/discuss/2830040/Python-Binary-Search-Beats-93-in-time-97-in-space
class Solution: def firstBadVersion(self, n: int) -> int: low = 1 high = n while True: mid = (low + high) // 2 if isBadVersion(mid): if not isBadVersion(mid - 1): return mid high = mid - 1 else: ...
first-bad-version
Python Binary Search [Beats 93% in time, 97% in space]
satyam_mishra13
0
4
first bad version
278
0.43
Easy
4,991
https://leetcode.com/problems/first-bad-version/discuss/2765677/binary-searchpython-3
class Solution: def firstBadVersion(self, n: int) -> int: left = 1 while n >= left: mid = (n + left) // 2 if isBadVersion(mid) and not isBadVersion(mid-1): return mid elif not isBadVersion(mid) : left = mid + 1 else: ...
first-bad-version
[binary search][python 3]
Nyx24
0
3
first bad version
278
0.43
Easy
4,992
https://leetcode.com/problems/perfect-squares/discuss/376795/100-O(log-n)-Python3-Solution-Lagrange's-four-square-theorem
class Solution: def isSquare(self, n: int) -> bool: sq = int(math.sqrt(n)) return sq*sq == n def numSquares(self, n: int) -> int: # Lagrange's four-square theorem if self.isSquare(n): return 1 while (n &amp; 3) == 0: n >>= 2 if (n ...
perfect-squares
100% O(log n) Python3 Solution - Lagrange’s four-square theorem
TCarmic
12
1,200
perfect squares
279
0.526
Medium
4,993
https://leetcode.com/problems/perfect-squares/discuss/1420219/Simple-Python-DP-or-BFS
class Solution: def numSquares(self, n: int) -> int: dp = [float("inf")]*(n+1) for i in range(len(dp)): if int(sqrt(i)) == sqrt(i): dp[i] = 1 else: for j in range(int(sqrt(i))+1): dp[i] = min(dp[i], dp[i-j*j]+1) retu...
perfect-squares
Simple Python DP or BFS
Charlesl0129
5
501
perfect squares
279
0.526
Medium
4,994
https://leetcode.com/problems/perfect-squares/discuss/622567/Python-sol-by-math.-90%2B-w-Comment
class Solution: def numSquares(self, n: int) -> int: while( n % 4 == 0 ): # Reduction by factor of 4 n //= 4 if n % 8 == 7: # Quick response for n = 8k + 7 return 4 # Check whether n = a^2 + b^2 for a in r...
perfect-squares
Python sol by math. 90%+ [w/ Comment]
brianchiang_tw
5
793
perfect squares
279
0.526
Medium
4,995
https://leetcode.com/problems/perfect-squares/discuss/2491871/Python-Elegant-and-Short-or-Three-lines-or-91.28-faster-or-Top-down-DP-or-LRU-cache
class Solution: """ Time: O(n*√n) Memory: O(log(n)) """ def numSquares(self, n: int) -> int: return self._decompose(n) @classmethod @lru_cache(None) def _decompose(cls, n: int) -> int: if n < 2: return n return 1 + min(cls._decompose(n - i * i) for i in range(1, isqrt(n) + 1))
perfect-squares
Python Elegant & Short | Three lines | 91.28% faster | Top-down DP | LRU-cache
Kyrylo-Ktl
3
367
perfect squares
279
0.526
Medium
4,996
https://leetcode.com/problems/perfect-squares/discuss/1816739/Python-ll-iterative-BFS
class Solution: def numSquares(self, n): squares = [i * i for i in range(1, int(n**0.5)+1)] Q = set() Q.add((0,0)) while Q: newQ = set() for rank, vertex in Q: for sq in squares: if vertex+sq == n: return rank+1 newQ.add( (rank+1, vertex+sq) ) Q = newQ
perfect-squares
Python ll iterative BFS
morpheusdurden
2
220
perfect squares
279
0.526
Medium
4,997
https://leetcode.com/problems/perfect-squares/discuss/1031504/Solution-without-DP-and-with-comments-faster-than-99.74-Time-Complexity%3AO(n0.5)
class Solution: def numSquares(self, n: int) -> int: if int(sqrt(n))**2==n: #If number is perfect square----> return 1 return 1 while n%4==0: #From theorem, if number is of the form 4^a(8^b+1) , where a,b=non-negative integers, it is sun of four squares, hence return 4 n/=...
perfect-squares
Solution without DP and with comments- faster than 99.74%, Time Complexity:O(n^0.5)
thisisakshat
2
329
perfect squares
279
0.526
Medium
4,998
https://leetcode.com/problems/perfect-squares/discuss/741367/Python-Recursive-DFS-and-BFS-Solutions-with-Comments!
class Solution: def numSquares(self, n: int) -> int: # Generate the squares, reverse to be in decreasing order for efficiency. sqrs = [i**2 for i in range(1, int(math.sqrt(n))+1)][::-1] mins = float('inf') def helper(t, nums, idx): nonlocal mins l = len(...
perfect-squares
Python Recursive DFS and BFS Solutions with Comments!
Pythagoras_the_3rd
2
550
perfect squares
279
0.526
Medium
4,999