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/ugly-number/discuss/2827081/Python-easy-to-understand-mathematical-solution
class Solution: def isUgly(self, n: int) -> bool: factors = [2, 3, 5] for i in factors: while n > 1 and n % i == 0: n /= i return n == 1
ugly-number
Python easy to understand mathematical solution
KevinJM17
1
36
ugly number
263
0.426
Easy
4,800
https://leetcode.com/problems/ugly-number/discuss/2825982/2-APPROACHororONE-LINER-oror-PYTHON3oror-40-MS-oror-EASY-TO-UNDERSTAND-SOLUTION
class Solution: def isUgly(self, m: int) -> bool: return False if m<=0 else (2*3*5)**32%m==0 APPROACH 2 class Solution: def isUgly(self, n: int) -> bool: if n<=0: return 0 for i in [2,3,5]: while n%i==0: n/=i return n==1
ugly-number
2 APPROACH||ONE LINER || PYTHON3|| 40 MS || EASY TO UNDERSTAND SOLUTION
thezealott
1
68
ugly number
263
0.426
Easy
4,801
https://leetcode.com/problems/ugly-number/discuss/2825974/Python-Detailed-Explanation-or-Fastest-or-99-Faster
class Solution: def isUgly(self, n: int) -> bool: prime_factors = [2, 3, 5] if n <= 0: return False for pf in prime_factors: while n%pf == 0: n = n//pf return n == 1
ugly-number
โœ”๏ธ Python Detailed Explanation | Fastest | 99% Faster ๐Ÿ”ฅ
pniraj657
1
79
ugly number
263
0.426
Easy
4,802
https://leetcode.com/problems/ugly-number/discuss/1842116/Python3-Solution
class Solution: def isUgly(self, n: int) -> bool: if n<=0: return False while n%2==0 or n%3==0 or n%5==0: if n%2==0: n=n//2 if n%3==0: n=n//3 if n%5==0: n=n//5 return False if n>1 else True
ugly-number
Python3 Solution
eaux2002
1
201
ugly number
263
0.426
Easy
4,803
https://leetcode.com/problems/ugly-number/discuss/1445253/Python3-Faster-Than-99.61-Memory-Less-Than-90.52
class Solution: def isUgly(self, n: int) -> bool: if n == 0: return 0 if n == 1: return 1 while n % 2 == 0: n >>= 1 while n % 3 == 0: n /= 3 while n % 5 == 0: n /= 5 return n == 1
ugly-number
Python3 Faster Than 99.61%, Memory Less Than 90.52%
Hejita
1
172
ugly number
263
0.426
Easy
4,804
https://leetcode.com/problems/ugly-number/discuss/2836272/Python3-solution-faster-93.3
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False for i in [2,3,5]: while n % i == 0: n /= i if n == 1: return True return False
ugly-number
Python3 solution faster 93.3%
Sarah_Lene
0
2
ugly number
263
0.426
Easy
4,805
https://leetcode.com/problems/ugly-number/discuss/2830037/Python-Solution-Simple-Maths-oror-EASY
class Solution: def isUgly(self, n: int) -> bool: if n<=0: return False while n!=1: if n%5==0: n//=5 elif n%3==0: n//=3 elif n%2==0: n//=2 else: return False ...
ugly-number
Python Solution - Simple Maths || EASYโœ”
T1n1_B0x1
0
3
ugly number
263
0.426
Easy
4,806
https://leetcode.com/problems/ugly-number/discuss/2829350/Python-Solution
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False i = 2 while (i * i) <= n: if n % i == 0: if i not in [2, 3, 5]: return False n = n / i else: ...
ugly-number
Python Solution
mansoorafzal
0
2
ugly number
263
0.426
Easy
4,807
https://leetcode.com/problems/ugly-number/discuss/2829135/Easiest-Solution-or-Best-Approch-or-Accepted-(Python-C%2B%2BJava)
class Solution: def isUgly(self, n: int) -> bool: while(n>1): if n%2==0: n=n//2 elif n%3==0: n=n//3 elif n%5==0: n=n//5 else: break if n==1: return True else: ...
ugly-number
Easiest Solution | Best Approch | Accepted (Python ,C++,Java) ๐Ÿ’ฏโœ…โœ…
adarshg04
0
7
ugly number
263
0.426
Easy
4,808
https://leetcode.com/problems/ugly-number/discuss/2829085/Python-simple-4-Line-solution
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False for i in [5,3,2]: while(n % i == 0): n /= i return n == 1
ugly-number
๐Ÿ˜Ž Python simple 4 Line solution
Pragadeeshwaran_Pasupathi
0
2
ugly number
263
0.426
Easy
4,809
https://leetcode.com/problems/ugly-number/discuss/2828208/Python3-1-line-Solution
class Solution: def isUgly(self, n: int) -> bool: return False if n<=0 else (2*3*5)**32 % n == 0
ugly-number
Python3 1 line Solution
avs-abhishek123
0
3
ugly number
263
0.426
Easy
4,810
https://leetcode.com/problems/ugly-number/discuss/2828143/Python-3-using-for-and-walrus-operator.
class Solution: def isUgly(self, n: int) -> bool: for i in 2,3,5: while n % i == 0 and (n := n/i): pass return n == 1
ugly-number
Python 3 using for and walrus operator.
TheSeer507
0
4
ugly number
263
0.426
Easy
4,811
https://leetcode.com/problems/ugly-number/discuss/2828092/simple-python-solution
class Solution: def isUgly(self, n: int) -> bool: if n==0: return False while n%5==0: n/=5 while n%3==0: n/=3 while n%2==0: n/=2 return n==1
ugly-number
simple python solution
sundram_somnath
0
4
ugly number
263
0.426
Easy
4,812
https://leetcode.com/problems/ugly-number/discuss/2828007/Python-Simple-Python-Solution-Using-Math
class Solution: def isUgly(self, n: int) -> bool: number = n if number <= 0: return False while number % 2 == 0 or number % 3 == 0 or number % 5 == 0: if number % 2 == 0: number = number // 2 elif number % 3 == 0: number = number // 3 elif number % 5 == 0: number = number // 5 if...
ugly-number
[ Python ] โœ…โœ… Simple Python Solution Using Math๐ŸฅณโœŒ๐Ÿ‘
ASHOK_KUMAR_MEGHVANSHI
0
9
ugly number
263
0.426
Easy
4,813
https://leetcode.com/problems/ugly-number/discuss/2827973/Python-or-Ugly-but-Decently-Efficient
class Solution: def isUgly(self, n): # Base Cases: # For a number to be "Ugly" the first # prerequisite is that it is positive if n <= 0: return False if n == 1: return True # Initializing desired set for O(1) lookups later # Not a huge improvement, but it's...
ugly-number
Python | Ugly but Decently Efficient
jessewalker2010
0
3
ugly number
263
0.426
Easy
4,814
https://leetcode.com/problems/ugly-number/discuss/2827944/Beats-92.57-or-Easiest-Solution
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False allowed_factors = [2, 3, 5] for factor in allowed_factors: while n%factor == 0: n = n / factor if n == 1: return True else: return False
ugly-number
Beats 92.57% | Easiest Solution
sachinsingh31
0
8
ugly number
263
0.426
Easy
4,815
https://leetcode.com/problems/ugly-number/discuss/2827842/Ugly-Number-or-Python-O(1)-Solution
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False else: return (2*3*5)**32 % n == 0
ugly-number
Ugly Number | Python O(1) Solution
jashii96
0
7
ugly number
263
0.426
Easy
4,816
https://leetcode.com/problems/ugly-number/discuss/2827740/Python-Easy-Solution-Time-Complexity-O(n)-Space-O(1)
class Solution: def isUgly(self, n: int) -> bool: if n==1: return True elif n<=0 and n>-2**31: return False i = 2 while n!=1: if n%i==0 : n//=i else: i+=1 if i>5: return Fal...
ugly-number
Python Easy Solution Time Complexity O(n) Space O(1)
nidhi_nishad26
0
7
ugly number
263
0.426
Easy
4,817
https://leetcode.com/problems/ugly-number/discuss/2827646/C%2B%2BJavaPython-Solution
class Solution: def isUgly(self, n: int) -> bool: while n > 1: if n % 2 == 0: n /= 2 elif n % 3 == 0: n /= 3 elif n % 5 == 0: n /= 5 else: break return n == 1
ugly-number
C++/Java/Python Solution
MrFit
0
3
ugly number
263
0.426
Easy
4,818
https://leetcode.com/problems/ugly-number/discuss/2827605/Python3-Solution-with-using-math
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False for x in [2,3,5]: while n % x == 0: n //= x return n == 1
ugly-number
[Python3] Solution with using math
maosipov11
0
2
ugly number
263
0.426
Easy
4,819
https://leetcode.com/problems/ugly-number/discuss/2827403/Constant-time-Functional-Solution-The-Best
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False MAXINT = 2**31 - 1 max_product = reduce(operator.mul, map(partial(self.getMaxPower, limit=MAXINT), (2, 3, 5))) return max_product % n == 0 def getMaxPower(self, base: int, limit: int): ...
ugly-number
Constant-time Functional Solution, The Best
Triquetra
0
3
ugly number
263
0.426
Easy
4,820
https://leetcode.com/problems/ugly-number/discuss/2827403/Constant-time-Functional-Solution-The-Best
class Solution: def isUgly(self, n: int) -> bool: return n > 0 == 2**30 * 3**19 * 5**13 % n
ugly-number
Constant-time Functional Solution, The Best
Triquetra
0
3
ugly number
263
0.426
Easy
4,821
https://leetcode.com/problems/ugly-number/discuss/2827351/Python3-O(logN)-Solution-oror-Explained-in-Detail-oror-Faster
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False while True: if n == 1: return True if n % 2 == 0: n //= 2 elif n % 3 == 0: n //= 3 elif n % 5 == 0: n //= 5 else: return False
ugly-number
โœ…๐Ÿ”ฐPython3 O(logN) Solution || Explained in Detail || Faster
StarBoyAbhi
0
5
ugly number
263
0.426
Easy
4,822
https://leetcode.com/problems/ugly-number/discuss/2827267/PYTHON-Python-easy-solution
class Solution: def isUgly(self, n: int) -> bool: if not n: return False while not n % 2: n = n // 2 while not n % 3: n = n // 3 while not n % 5: n = n // 5 return n == 1
ugly-number
[PYTHON] Python easy solution
valera_grishko
0
6
ugly number
263
0.426
Easy
4,823
https://leetcode.com/problems/ugly-number/discuss/2826997/Python-Simple-Python-Solution
class Solution: def isUgly(self, n: int) -> bool: while n>1: if n%2==0: n=n//2 elif n%3==0: n=n//3 elif n%5==0: n=n//5 else: break if n==1: return True else: ...
ugly-number
[ Python ] ๐Ÿ๐Ÿ Simple Python Solution โœ…โœ…
sourav638
0
7
ugly number
263
0.426
Easy
4,824
https://leetcode.com/problems/ugly-number/discuss/2826987/Ugly-Number
class Solution: def isUgly(self, n: int) -> bool: if n<=0: return 0 while n>1: if n%2==0: n=n/2 elif n%3==0: n=n/3 elif n%5==0: n=n/5 else: return 0 else: ...
ugly-number
Ugly Number
Harsh_Gautam45
0
2
ugly number
263
0.426
Easy
4,825
https://leetcode.com/problems/ugly-number/discuss/2826829/Python-solution-what-are-the-time-and-space-complexities
class Solution: def isUgly(self, n: int) -> bool: while n>1: if n%2==0: n = n // 2 elif n%3==0: n = n // 3 elif n%5==0: n = n // 5 else: break return n==1
ugly-number
Python solution - what are the time and space complexities?
vishalkapoorbkn
0
3
ugly number
263
0.426
Easy
4,826
https://leetcode.com/problems/ugly-number/discuss/2826822/Python-Soln
class Solution: def isUgly(self, n: int) -> bool: if n == 0: return False for num in [2,3,5]: while n % num == 0: n = n // num return True if n is 1 else False
ugly-number
Python Soln
saivamshi0103
0
1
ugly number
263
0.426
Easy
4,827
https://leetcode.com/problems/ugly-number/discuss/2826819/By-recursive-division-by-2-3-and-5
class Solution: def isUgly(self, n: int) -> bool: if n <= 0 : return False while n % 2 == 0 : n = n//2 while n % 3 == 0 : n = n//3 while n % 5 == 0 : n = n//5 if n == 1: return True else: return False
ugly-number
By recursive division by 2, 3 and 5
nikhilbelure
0
1
ugly number
263
0.426
Easy
4,828
https://leetcode.com/problems/ugly-number/discuss/2826698/Recursive-Approach-or-Beats-98.92
class Solution: def isUgly(self, n: int) -> bool: if n == 0: return False if n == 1: return True if n%2 == 0: return self.isUgly(n//2) elif n%3 == 0: return self.isUgly(n//3) elif n%5 == 0: return self.isUgly(n//5) ...
ugly-number
Recursive Approach | Beats 98.92%
user3108Vs
0
8
ugly number
263
0.426
Easy
4,829
https://leetcode.com/problems/ugly-number/discuss/2826580/Python-or-Sqrt-to-get-divisors-or-Fast
class Solution: def isUgly(self, n: int) -> bool: if n <= 0: return False for i in range(2, int(sqrt(n)) + 5): while n % i == 0: n //= i if i > 5: return False if n == 1: break return True
ugly-number
Python | Sqrt to get divisors | Fast
LordVader1
0
7
ugly number
263
0.426
Easy
4,830
https://leetcode.com/problems/ugly-number/discuss/2826554/Python-solution
class Solution: def isUgly(self, n: int) -> bool: if n < 1: return False while n != 1: if n % 2 == 0: n = n // 2 elif n % 3 == 0: n = n // 3 elif n % 5 == 0: n = n // 5 else: return False return True
ugly-number
Python solution
anandanshul001
0
2
ugly number
263
0.426
Easy
4,831
https://leetcode.com/problems/ugly-number/discuss/2826225/Easy-Python-Solution
class Solution: def isUgly(self, n: int) -> bool: if n == 1: return True elif n == 0: return False while(n): if(n%3 == 0 or n%5 == 0 or n%2 == 0): if n%3 == 0: return self.isUgly(n//3) elif n%5 == 0: ...
ugly-number
Easy Python Solution
urmil_kalaria
0
3
ugly number
263
0.426
Easy
4,832
https://leetcode.com/problems/ugly-number/discuss/2826215/Python-solution
class Solution: def isUgly(self, n: int) -> bool: if n<=0:return False else: l=[2,3,5] for i in l: while n%i==0: n=n//i return n==1
ugly-number
Python solution
SheetalMehta
0
1
ugly number
263
0.426
Easy
4,833
https://leetcode.com/problems/ugly-number/discuss/2826207/Python-Easy
class Solution: def isUgly(self, n: int) -> bool: if(n==0): return False if(n == 1): return True if(not(n%2)): return self.isUgly(n/2) if(not(n%3)): return self.isUgly(n/3) if(not(n%5)): return self.isUgly(n/5) ...
ugly-number
Python Easy
Pradyumankannan
0
2
ugly number
263
0.426
Easy
4,834
https://leetcode.com/problems/ugly-number/discuss/2826169/Python-solution-beats-99.33
class Solution: def isUgly(self, n: int) -> bool: if n==0: return False # if the n == 0 return 0 since its not an ugly number for i in [2,3,5]: while n%i==0: # if it is dividable without reminder n/=i #divide each iteration by the list values if n == 1: ...
ugly-number
Python solution beats 99.33 %
akashkr113131
0
2
ugly number
263
0.426
Easy
4,835
https://leetcode.com/problems/ugly-number/discuss/2826108/Simple-Solution
class Solution: def isUgly(self, n: int) -> bool: while (n%2==0 and n!=0): n = n//2 while (n%3==0 and n!=0): n = n//3 while (n%5==0 and n!=0): n = n//5 return n==1
ugly-number
Simple Solution
pruthvi_hingu
0
1
ugly number
263
0.426
Easy
4,836
https://leetcode.com/problems/ugly-number/discuss/2825993/Python3-easy-Solution-.-O(N)O(1)
class Solution: def isUgly(self, n: int) -> bool: if n<=0: return False for p in [2,3,5]: while n%p==0: n=n//p return n==1
ugly-number
Python3 easy Solution . O(N),O(1)
Motaharozzaman1996
0
3
ugly number
263
0.426
Easy
4,837
https://leetcode.com/problems/ugly-number/discuss/2825878/Python3-Easy-idea!-Taking-out-all-%22banned%22-factors
class Solution: def isUgly(self, n: int) -> bool: if n == 0: # CRITICAL edge case! return False a = n for d in [2, 3, 5]: # means -- taking out all "banned" factors from the input number while a % d == 0: a //= d return a == 1
ugly-number
[Python3] Easy idea! Taking out all "banned" factors
JoeH
0
6
ugly number
263
0.426
Easy
4,838
https://leetcode.com/problems/ugly-number-ii/discuss/556314/Python-Simple-DP-9-Lines
class Solution: def nthUglyNumber(self, n: int) -> int: k = [0] * n t1 = t2 = t3 = 0 k[0] = 1 for i in range(1,n): k[i] = min(k[t1]*2,k[t2]*3,k[t3]*5) if(k[i] == k[t1]*2): t1 += 1 if(k[i] == k[t2]*3): t2 += 1 if(k[i] == k[t3]*5): t3 += ...
ugly-number-ii
[Python] Simple DP 9 Lines
mazz272727
23
1,100
ugly number ii
264
0.462
Medium
4,839
https://leetcode.com/problems/ugly-number-ii/discuss/369242/Three-Short-Solutions-in-Python-3-(Bisect-Heap-DP)
class Solution: def nthUglyNumber(self, n: int) -> int: N, I = [1], {2:0, 3:0, 5:0} for _ in range(n-1): I = {i:bisect.bisect(N, N[-1]//i, I[i]) for i in [2,3,5]} N.append(min(N[I[2]]*2,N[I[3]]*3,N[I[5]]*5)) return N[-1]
ugly-number-ii
Three Short Solutions in Python 3 (Bisect, Heap, DP)
junaidmansuri
6
1,200
ugly number ii
264
0.462
Medium
4,840
https://leetcode.com/problems/ugly-number-ii/discuss/369242/Three-Short-Solutions-in-Python-3-(Bisect-Heap-DP)
class Solution: def nthUglyNumber(self, n: int) -> int: N, m, S = [1], 1, set() for _ in range(n): while m in S: m = heapq.heappop(N) S.add(m) for i in [2,3,5]: heapq.heappush(N,i*m) return m
ugly-number-ii
Three Short Solutions in Python 3 (Bisect, Heap, DP)
junaidmansuri
6
1,200
ugly number ii
264
0.462
Medium
4,841
https://leetcode.com/problems/ugly-number-ii/discuss/369242/Three-Short-Solutions-in-Python-3-(Bisect-Heap-DP)
class Solution: def nthUglyNumber(self, n: int) -> int: N, p, I = [1], [2,3,5], [0]*3 for _ in range(n-1): N.append(min([N[I[i]]*p[i] for i in range(3)])) for i in range(3): I[i] += N[I[i]]*p[i] == N[-1] return N[-1] - Junaid Mansuri (LeetCode ID)@hotmail.com
ugly-number-ii
Three Short Solutions in Python 3 (Bisect, Heap, DP)
junaidmansuri
6
1,200
ugly number ii
264
0.462
Medium
4,842
https://leetcode.com/problems/ugly-number-ii/discuss/1624868/Python-solution-using-dynamic-programming
class Solution: def nthUglyNumber(self, n: int) -> int: dp=[1] p2,p3,p5=0,0,0 for i in range(n+1): t2=dp[p2]*2 t3=dp[p3]*3 t5=dp[p5]*5 temp=min(t2,t3,t5) dp.append(temp) ...
ugly-number-ii
Python solution using dynamic programming
diksha_choudhary
4
298
ugly number ii
264
0.462
Medium
4,843
https://leetcode.com/problems/ugly-number-ii/discuss/1630460/Python3-Simple-DP-solution-with-Comments
class Solution: def nthUglyNumber(self, n: int) -> int: l = [1] p2, p3, p5 = 0, 0, 0 for i in range(1, n): # get the next number next_num = min(2*l[p2], min(3*l[p3], 5*l[p5])) l.append(next_num) # increase pointer for which the number matches [see above exp...
ugly-number-ii
Python3 Simple DP solution with Comments
Shwetabh1
3
350
ugly number ii
264
0.462
Medium
4,844
https://leetcode.com/problems/ugly-number-ii/discuss/2828717/Python-oror-93.80-Faster-oror-Iterative-Approach-oror-O(n)-Solution
class Solution: def nthUglyNumber(self, n: int) -> int: ans=[1] prod_2=prod_3=prod_5=0 for i in range(1,n): a=ans[prod_2]*2 b=ans[prod_3]*3 c=ans[prod_5]*5 m=min(a,b,c) ans.append(m) if m==a: prod_2+=1 ...
ugly-number-ii
Python || 93.80% Faster || Iterative Approach || O(n) Solution
DareDevil_007
2
182
ugly number ii
264
0.462
Medium
4,845
https://leetcode.com/problems/ugly-number-ii/discuss/1236467/Python3-simple-solution-using-dynamic-programming
class Solution: def nthUglyNumber(self, n: int) -> int: ugly = [1] a,b,c = 0,0,0 while len(ugly) != n: x = min(ugly[a]*2,ugly[b]*3,ugly[c]*5) if x == ugly[a]*2: ugly.append(ugly[a]*2) a += 1 elif x == ugly[b]*3: ...
ugly-number-ii
Python3 simple solution using dynamic programming
EklavyaJoshi
2
277
ugly number ii
264
0.462
Medium
4,846
https://leetcode.com/problems/ugly-number-ii/discuss/720034/Python3-7-line-dp
class Solution: def nthUglyNumber(self, n: int) -> int: @lru_cache(None) def fn(k): """Return the smallest ugly number larger than k (not necessarily ugly).""" if k == 0: return 1 return min(f*fn(k//f) for f in (2, 3, 5)) ans = 1 for ...
ugly-number-ii
[Python3] 7-line dp
ye15
2
203
ugly number ii
264
0.462
Medium
4,847
https://leetcode.com/problems/ugly-number-ii/discuss/720034/Python3-7-line-dp
class Solution: def nthUglyNumber(self, n: int) -> int: ans = [1] p2 = p3 = p5 = 0 for _ in range(n-1): x = min(2*ans[p2], 3*ans[p3], 5*ans[p5]) ans.append(x) if 2*ans[p2] == x: p2 += 1 if 3*ans[p3] == x: p3 += 1 if 5*ans[p5] == x:...
ugly-number-ii
[Python3] 7-line dp
ye15
2
203
ugly number ii
264
0.462
Medium
4,848
https://leetcode.com/problems/ugly-number-ii/discuss/2847277/python-%2B-comments
class Solution: def nthUglyNumber(self, n: int) -> int: #initialize 3 pointers a,b,c dp,a,b,c = [1]*n,0,0,0 for i in range(1,n): #the main process can be seen as race, #the slow one can run firstly and ensure that the one behind moves first in each round. ...
ugly-number-ii
python + comments
xiaolaotou
0
1
ugly number ii
264
0.462
Medium
4,849
https://leetcode.com/problems/ugly-number-ii/discuss/2827339/O(1)-Solution-)
class Solution: def nthUglyNumber(self, n: int) -> int: lst=[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36, 40, 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 96, 100, 108, 120, 125, 128, 135, 144, 150, 160, 162, 180, 192, 200, 216, 225, 240, 243, 250, 256, 270, 288, 300, 320, 324, 360...
ugly-number-ii
O(1) Solution ;)
parteekraj8
0
6
ugly number ii
264
0.462
Medium
4,850
https://leetcode.com/problems/ugly-number-ii/discuss/2706078/91-faster-easy
class Solution: def nthUglyNumber(self, n: int) -> int: res=[1] two_p=0 three_p=0 five_p=0 while(len(res)<n): by2=res[two_p]*2 by3=res[three_p]*3 by5=res[five_p]*5 mn=min(by2,by3,by5) res.append(mn) if(by...
ugly-number-ii
91% faster easy
Raghunath_Reddy
0
29
ugly number ii
264
0.462
Medium
4,851
https://leetcode.com/problems/ugly-number-ii/discuss/2699887/Python-or-Bisect-solution-or-O(nlog(n))
class Solution: def nthUglyNumber(self, n: int) -> int: dp = [1] j = 0 used = set() while j <= n: for elem in [dp[j]*2, dp[j]*3, dp[j]*5]: if elem not in used: bisect.insort(dp, elem) used.add(elem) j += ...
ugly-number-ii
Python | Bisect solution | O(nlog(n))
LordVader1
0
23
ugly number ii
264
0.462
Medium
4,852
https://leetcode.com/problems/ugly-number-ii/discuss/2650070/Python-Array-Solution-O(n)-TC-and-SC
class Solution: def nthUglyNumber(self, n: int) -> int: if n == 0: return 1 res = [1] + [0]*(n-1) i2 = i3 = i5 = 0 for i in range(1, n): nxt = min(res[i2]*2, res[i3]*3, res[i5]*5) if res[i2] * 2 == nxt: i2 += 1 if res[i...
ugly-number-ii
Python Array Solution - O(n) TC and SC
sharma_shubham
0
5
ugly number ii
264
0.462
Medium
4,853
https://leetcode.com/problems/ugly-number-ii/discuss/2326373/Clean-and-elegant-python3-code-Heap-or-Priority-Queue-or-89.-faster
class Solution: def nthUglyNumber(self, n: int) -> int: heaped=[1] heapify(heaped) set_val=set([1]) result=1 while n: top=heappop(heaped) result=top if top*2 not in set_val: heappush(heaped,top*2) set_val.add...
ugly-number-ii
Clean and elegant python3 code, Heap | Priority Queue | 89. % faster
Sefinehtesfa34
0
43
ugly number ii
264
0.462
Medium
4,854
https://leetcode.com/problems/ugly-number-ii/discuss/2066694/Python-or-DP
class Solution: def nthUglyNumber(self, n: int) -> int: d = [1] i2,i3,i5 = 0,0,0 c = 1 if n==1: return 1 while c < n: d.append(min(d[i2]*2,d[i3]*3,d[i5]*5)) c += 1 if d[-1] == d[i2]*2: i2 += 1 if d[-1...
ugly-number-ii
Python | DP
Shivamk09
0
125
ugly number ii
264
0.462
Medium
4,855
https://leetcode.com/problems/ugly-number-ii/discuss/1868407/Python3-HEAP
class Solution: def nthUglyNumber(self, n: int) -> int: if n < 7: return n factors = (2, 3, 5) ugly, heap, i = [1], [], 1 while True: for f in factors: heappush(heap, ugly[-1]*f) num = ugly[-1] while num ==...
ugly-number-ii
[Python3] HEAP
artod
0
147
ugly number ii
264
0.462
Medium
4,856
https://leetcode.com/problems/ugly-number-ii/discuss/719300/Python3-solution
class Solution: def nthUglyNumber(self, n: int) -> int: if n==1: return 1 q2, q3, q5 = deque([2]), deque([3]), deque([5]) for _ in range(n-1): nxt = min(q2[0],q3[0],q5[0]) if nxt==q2[0]: q2.popleft() if nxt==q3[0]: ...
ugly-number-ii
Python3 solution
dalechoi
0
152
ugly number ii
264
0.462
Medium
4,857
https://leetcode.com/problems/ugly-number-ii/discuss/420906/Accepted-Python-Answer-with-Tuple-Caching
class Solution: def nthUglyNumber(self, n: int) -> int: ugly_list = (1,) count_2 = count_3 = count_5 = 0 while len(ugly_list) < n: while ugly_list[count_2] * 2 <= ugly_list[-1]: count_2 += 1 while ugly_list[count_3] * 3 <= ugly_list[-1]: ...
ugly-number-ii
Accepted Python Answer with Tuple Caching
i-i
0
228
ugly number ii
264
0.462
Medium
4,858
https://leetcode.com/problems/ugly-number-ii/discuss/396035/Am-I-wrong
class Solution: def find_upper(self, d, n_list, beg): re = 1 for i in range(beg, len(n_list)): re = n_list[i] if (d < n_list[i]): break return(re) def nthUglyNumber(self, n: int) -> int: re = 1 ll = [1] for i in...
ugly-number-ii
Am I wrong?
yeqing117
0
101
ugly number ii
264
0.462
Medium
4,859
https://leetcode.com/problems/missing-number/discuss/2081185/Python-Easy-One-liners-with-Explanation
class Solution: def missingNumber(self, nums: List[int]) -> int: return (len(nums) * (len(nums) + 1))//2 - sum(nums)
missing-number
โœ… Python Easy One liners with Explanation
constantine786
23
2,000
missing number
268
0.617
Easy
4,860
https://leetcode.com/problems/missing-number/discuss/2081185/Python-Easy-One-liners-with-Explanation
class Solution: def missingNumber(self, nums: List[int]) -> int: return reduce(lambda x,y: x ^ y, list(range(len(nums)+1)) + nums)
missing-number
โœ… Python Easy One liners with Explanation
constantine786
23
2,000
missing number
268
0.617
Easy
4,861
https://leetcode.com/problems/missing-number/discuss/1092210/PythonPython3-2-One-liner-Solutions
class Solution: def missingNumber(self, nums: List[int]) -> int: return sum(range(len(nums)+1)) - sum(nums)
missing-number
[Python/Python3] 2 One-liner Solutions
newborncoder
16
1,400
missing number
268
0.617
Easy
4,862
https://leetcode.com/problems/missing-number/discuss/1092210/PythonPython3-2-One-liner-Solutions
class Solution: def missingNumber(self, nums: List[int]) -> int: return list(set(range(0,len(nums)+1)).difference(set(nums)))[0]
missing-number
[Python/Python3] 2 One-liner Solutions
newborncoder
16
1,400
missing number
268
0.617
Easy
4,863
https://leetcode.com/problems/missing-number/discuss/2375385/Very-Easy-100(Fully-Explained)(Java-C%2B%2B-Python-JS-C-Python3)
class Solution(object): def missingNumber(self, nums): lenth = len(nums) # Calculate the sum of the first N natural numbers as (1 + lenth) * lenth/2... sum = (1 + lenth) * lenth/2 # Traverse the array from start to end... for i in nums: # Find the sum of all the a...
missing-number
Very Easy 100%(Fully Explained)(Java, C++, Python, JS, C, Python3)
PratikSen07
11
653
missing number
268
0.617
Easy
4,864
https://leetcode.com/problems/missing-number/discuss/2375385/Very-Easy-100(Fully-Explained)(Java-C%2B%2B-Python-JS-C-Python3)
class Solution: def missingNumber(self, nums: List[int]) -> int: lenth = len(nums) # Calculate the sum of the first N natural numbers as (1 + lenth) * lenth/2... sum = (1 + lenth) * lenth/2 # Traverse the array from start to end... for i in nums: # Find the sum of...
missing-number
Very Easy 100%(Fully Explained)(Java, C++, Python, JS, C, Python3)
PratikSen07
11
653
missing number
268
0.617
Easy
4,865
https://leetcode.com/problems/missing-number/discuss/2235492/Python-Cyclic-Sort-Time-O(N)-or-Space-O(1)-Explained
class Solution: def missingNumber(self, nums: List[int]) -> int: i = 0 # This will keep track of the current index as we iterate through the array while i < len(nums): j = nums[i] # This will hold the value at our current index and that value will be the index we swap values with ...
missing-number
[Python] Cyclic Sort - Time O(N) | Space O(1) Explained
Symbolistic
5
130
missing number
268
0.617
Easy
4,866
https://leetcode.com/problems/missing-number/discuss/1752235/Python-or-Three-Approaches-or-Math-Hash-Table-Bit-Operations
class Solution: def missingNumber(self, nums: List[int]) -> int: #Method 3 - Hash Table return list(set(range(len(nums)+1)) - set(nums))[0] #Method 2 - Bit Manipulation - https://leetcode.com/problems/missing-number/discuss/1445140/Python-XOR-Explanation ...
missing-number
Python | Three Approaches | Math, Hash Table, Bit Operations
mehrotrasan16
5
388
missing number
268
0.617
Easy
4,867
https://leetcode.com/problems/missing-number/discuss/1529886/Simple-efficient-two-line-python-solution
class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) return n * (n + 1) // 2 - sum(nums)
missing-number
Simple, efficient, two line python solution
dereky4
5
357
missing number
268
0.617
Easy
4,868
https://leetcode.com/problems/missing-number/discuss/1091175/Python.-Math.-faster-than-96.69-O(1)-space.-O(n)-time
class Solution: def missingNumber(self, nums: List[int]) -> int: l = len(nums) return l * (1 + l) // 2 - sum(nums)
missing-number
Python. Math. faster than 96.69% O(1) space. O(n) time
m-d-f
5
569
missing number
268
0.617
Easy
4,869
https://leetcode.com/problems/missing-number/discuss/2081596/Python-one-liner-solution-oror-Simple-oror-Easy-Understanding
class Solution: def missingNumber(self, nums: List[int]) -> int: return [item for item in [i for i in range(len(nums)+1)] if item not in nums][0]
missing-number
Python one liner solution || Simple || Easy Understanding
Shivam_Raj_Sharma
4
167
missing number
268
0.617
Easy
4,870
https://leetcode.com/problems/missing-number/discuss/2711890/One-line-Python-solution-using-sets
class Solution: def missingNumber(self, nums: List[int]) -> int: return next(iter(set(range(len(nums) + 1)) - set(nums)))
missing-number
๐Ÿ“Œ One-line Python solution using sets
croatoan
3
829
missing number
268
0.617
Easy
4,871
https://leetcode.com/problems/missing-number/discuss/2674105/Python-One-Liner-Easy-Solution-EXPLAINED
class Solution: def missingNumber(self, nums: List[int]) -> int: return (len(nums)*len(nums) + len(nums)) // 2 - sum(nums)
missing-number
[Python] One-Liner Easy Solution EXPLAINED
keioon
3
176
missing number
268
0.617
Easy
4,872
https://leetcode.com/problems/missing-number/discuss/2060767/PYTHON-Simple-Solution
class Solution: def missingNumber(self, nums: List[int]) -> int: nums.sort() l=[x for x in range(max(nums)+1)] if (l==nums): return max(nums)+1 for i in l: if i not in nums: return i
missing-number
PYTHON {Simple} Solution
tusharkhanna575
3
133
missing number
268
0.617
Easy
4,873
https://leetcode.com/problems/missing-number/discuss/1809535/Python3-or-one-line
class Solution: def missingNumber(self, nums: List[int]) -> int: return (list(Counter(range(0,len(nums)+1))-Counter(nums)))[0]
missing-number
Python3 | one line
Anilchouhan181
3
199
missing number
268
0.617
Easy
4,874
https://leetcode.com/problems/missing-number/discuss/1654636/1-line-python-solution
class Solution: def missingNumber(self, nums: List[int]) -> int: return list(set(list(range(0,len(nums)+1)))-set(nums))[0]
missing-number
1 line python solution
amannarayansingh10
3
205
missing number
268
0.617
Easy
4,875
https://leetcode.com/problems/missing-number/discuss/1432995/3-way-of-solving-in-Python-O(n)-and-O(1)-extra-space
class Solution: def missingNumber(self, nums: List[int]) -> int: xor = 0 for i in range(len(nums)+1): xor^=i for v in nums: xor^=v return xor
missing-number
3 way of solving in Python - O(n) and O(1) extra space
abrarjahin
3
391
missing number
268
0.617
Easy
4,876
https://leetcode.com/problems/missing-number/discuss/1432995/3-way-of-solving-in-Python-O(n)-and-O(1)-extra-space
class Solution: def missingNumber(self, nums: List[int]) -> int: return (len(nums)*(len(nums)+1))//2 - sum(nums)
missing-number
3 way of solving in Python - O(n) and O(1) extra space
abrarjahin
3
391
missing number
268
0.617
Easy
4,877
https://leetcode.com/problems/missing-number/discuss/1253405/Python3-dollarolution(99.5-faster-not-memory-eff-tho)
class Solution: def missingNumber(self, nums: List[int]) -> int: i = 0 while i in nums: i += 1 return i
missing-number
Python3 $olution(99.5% faster, not memory eff tho)
AakRay
3
354
missing number
268
0.617
Easy
4,878
https://leetcode.com/problems/missing-number/discuss/2081802/Simple-python-solution
class Solution: def missingNumber(self, nums: List[int]) -> int: nums.sort() l=[x for x in range(nums[-1]+1)] if (l==nums): return nums[-1]+1 for i in l: if i not in nums: return i
missing-number
Simple python solution
tusharkhanna575
2
58
missing number
268
0.617
Easy
4,879
https://leetcode.com/problems/missing-number/discuss/2081613/O(1)-oror-PYTHON-oror-EXPLAINED-FOR-ALL-LANGUAGES
class Solution: def missingNumber(self, n: List[int]) -> int: return ( ( len(nums) * (len(nums)+1) ) // 2) - sum(nums)
missing-number
O(1) || PYTHON || EXPLAINED FOR ALL LANGUAGES
karan_8082
2
82
missing number
268
0.617
Easy
4,880
https://leetcode.com/problems/missing-number/discuss/1917307/Python-Solution-or-100-Faster-or-Simple-Logic
class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) + 1 return ((n - 1) * n) // 2 - sum(nums)
missing-number
Python Solution | 100% Faster | Simple Logic
Gautam_ProMax
2
116
missing number
268
0.617
Easy
4,881
https://leetcode.com/problems/missing-number/discuss/1754741/Using-Binary-Search-and-O(1)-space
class Solution: def missingNumber(self, nums: List[int]) -> int: nums.sort() def in_wrong_position(mid): return nums[mid] != mid left, right = 0, len(nums) while left < right: mid = left + (right - left)//2 if in_wrong_position(mid): ...
missing-number
Using Binary Search and O(1) space
snagsbybalin
2
87
missing number
268
0.617
Easy
4,882
https://leetcode.com/problems/missing-number/discuss/1643648/Easy-and-Simple-Python-Beginners-Solution
class Solution: def missingNumber(self, nums: List[int]) -> int: for i in range(len(nums)+1): if i in nums: pass else: return i
missing-number
Easy and Simple Python Beginners Solution
yashitanamdeo
2
148
missing number
268
0.617
Easy
4,883
https://leetcode.com/problems/missing-number/discuss/1233975/Easy-Python
class Solution: def missingNumber(self, nums: List[int]) -> int: if(len(nums)==0): return 0 nums.sort() for i in range(len(nums)): if(i!=nums[i]): return i return len(nums)
missing-number
Easy Python
Sneh17029
2
1,300
missing number
268
0.617
Easy
4,884
https://leetcode.com/problems/missing-number/discuss/1233975/Easy-Python
class Solution: def missingNumber(self, nums: List[int]) -> int: if(len(nums)==0): return 0 s=0 s1=0 for i in range(len(nums)+1): s+=i for i in nums: s1+=i return s-s1
missing-number
Easy Python
Sneh17029
2
1,300
missing number
268
0.617
Easy
4,885
https://leetcode.com/problems/missing-number/discuss/1062464/Python-very-simple-one-liner
class Solution: def missingNumber(self, nums: List[int]) -> int: return sum(range(len(nums)+1)) - sum(nums)
missing-number
Python - very simple one liner
angelique_
2
102
missing number
268
0.617
Easy
4,886
https://leetcode.com/problems/missing-number/discuss/491658/Python-132ms-13.9MB-94-100-Solution
class Solution: def missingNumber(self, nums: List[int]) -> int: l, s = len(nums), sum(nums) return math.floor(((1 + l) * l - 2 * s ) / 2 )
missing-number
Python 132ms / 13.9MB 94% / 100% Solution
X_D
2
327
missing number
268
0.617
Easy
4,887
https://leetcode.com/problems/missing-number/discuss/342421/Solution-in-Python-3-(two-solutions)
class Solution: def missingNumber(self, n: List[int]) -> int: return len(n)*(len(n)+1)//2 - sum(n)
missing-number
Solution in Python 3 (two solutions)
junaidmansuri
2
1,000
missing number
268
0.617
Easy
4,888
https://leetcode.com/problems/missing-number/discuss/342421/Solution-in-Python-3-(two-solutions)
class Solution: def missingNumber(self, n: List[int]) -> int: n.append(-1) i, L = 0, len(n) while i != L: if n[i] not in [i,-1]: n[n[i]], n[i] = n[i], n[n[i]] else: if n[i] == -1: a = i i += 1 return a - Python 3 (LeetCode ID)@hotm...
missing-number
Solution in Python 3 (two solutions)
junaidmansuri
2
1,000
missing number
268
0.617
Easy
4,889
https://leetcode.com/problems/missing-number/discuss/2707061/Python-1-liner-(with-explanation)
class Solution: def missingNumber(self, nums: List[int]) -> int: return int(len(nums)*(len(nums)+1)/2 - sum(nums))
missing-number
Python 1 liner (with explanation)
code_snow
1
5
missing number
268
0.617
Easy
4,890
https://leetcode.com/problems/missing-number/discuss/2662586/Python
class Solution: def missingNumber(self, nums: List[int]) -> int: n=len(nums)+1 for i in range(n): if i not in nums: return i
missing-number
Python
Sneh713
1
64
missing number
268
0.617
Easy
4,891
https://leetcode.com/problems/missing-number/discuss/2601824/Python3-Faster-than-over-95-in-just-two-lines!
class Solution: def missingNumber(self, nums: List[int]) -> int: complete = list(range(len(nums) + 1)) # generate a list of all numbers return sum(complete) - sum(nums) # when we calculate the sum and subtract the faulty sum, we get the result
missing-number
[Python3] Faster than over 95% in just two lines!
zakmatt
1
151
missing number
268
0.617
Easy
4,892
https://leetcode.com/problems/missing-number/discuss/2563887/Eight-Different-Approaches-in-Python3
class Solution: def brute_force(self, nums: List[int]) -> int: """ Time Complexity: O(N*N) Space Complexity: O(1) """ for i in range(len(nums)): if i not in nums: return i return len(nums) def sorting(self, nums: List[int]) -> int:...
missing-number
Eight Different Approaches in Python3
fai555
1
119
missing number
268
0.617
Easy
4,893
https://leetcode.com/problems/missing-number/discuss/2384004/Fastest-and-simplest-or-Python
class Solution: def missingNumber(self, nums: List[int]) -> int: m=len(nums) s=(m*(m+1))//2 return(s-sum(nums))
missing-number
โœ” Fastest & simplest | Python
ayushigupta2409
1
95
missing number
268
0.617
Easy
4,894
https://leetcode.com/problems/missing-number/discuss/2252335/Python-Answer-or-No-XOR-or-Very-Simple
class Solution: def missingNumber(self, nums: List[int]) -> int: for index in range(len(nums)+1): if index not in nums: return index
missing-number
Python Answer | No XOR | Very Simple
jma2003
1
85
missing number
268
0.617
Easy
4,895
https://leetcode.com/problems/missing-number/discuss/2188755/Simple-Easy-to-understand-solution-using-simple-maths
class Solution: def missingNumber(self, nums: List[int]) -> int: n=len(nums) return (n*(n+1)//2)-sum(nums)
missing-number
Simple Easy to understand solution using simple maths
MahekSavani
1
82
missing number
268
0.617
Easy
4,896
https://leetcode.com/problems/missing-number/discuss/2179772/python3-one-line-solution-with-O(1)-complexity.
class Solution: def missingNumber(self, nums: List[int]) -> int: return (len(nums)*(len(nums)+1)//2) - sum(nums)
missing-number
python3 one line solution with O(1) complexity.
thetimeloops
1
109
missing number
268
0.617
Easy
4,897
https://leetcode.com/problems/missing-number/discuss/2137533/python-xor-explaination
class Solution: def missingNumber(self, nums: List[int]) -> int: s = 0 for i in nums: s ^= i for i in range(len(nums)+1): s ^= i return s
missing-number
python xor explaination
writemeom
1
102
missing number
268
0.617
Easy
4,898
https://leetcode.com/problems/missing-number/discuss/2137533/python-xor-explaination
class Solution: def missingNumber(self, nums: List[int]) -> int: return (set(range(len(nums)+1)) - set(nums)).pop()
missing-number
python xor explaination
writemeom
1
102
missing number
268
0.617
Easy
4,899