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 return True
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: i += 1 return False if (n > 1 and n not in [2, 3, 5]) else True
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: return False
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 number == 1: return True else: return False
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 non-zero improvement desired = {2,3,5} primes = (i for i in [2,3,5,7]) current_prime = next(primes) while True: # As soon as our current prime exceeds 2,3,5 we know our n # has prime factors not included in the "Ugly" grouping if current_prime not in desired: return False # We want to divide n by said prime as many times as we can # If the prime doesn't divide evenly into n then we go to the # next prime. We would only do this shift 3 times, and once # we have, we've gone into the realm of non-Ugly numbers while n % current_prime == 0: div= n/current_prime if div == 1: return True n = div current_prime = next(primes)
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 False return True
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): max_exponent = math.floor(math.log(limit, base)) return base ** max_exponent
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: return False
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: return 1
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) else: return False
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: return self.isUgly(n//5) else: return self.isUgly(n//2) else: return False return True
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) return False
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: #when it goes to 1 return 1 else False return True
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 += 1 return k[n-1]
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) if temp==dp[p2]*2: p2+=1 if temp==dp[p3]*3: p3+=1 if temp==dp[p5]*5: p5+=1 # print(dp) return dp[n-1]
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 explanation] if next_num == 2 * l[p2]: p2 += 1 if next_num == 3 * l[p3]: p3 += 1 if next_num == 5 * l[p5]: p5 += 1 return l[n-1]
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 if m==b: prod_3+=1 if m==c: prod_5+=1 return ans[-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: if x in ugly: b += 1 else: ugly.append(ugly[b]*3) b += 1 elif x == ugly[c]*5: if x in ugly: c += 1 else: ugly.append(ugly[c]*5) c += 1 return ugly[-1]
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 _ in range(n-1): ans = fn(ans) return ans
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: p5 += 1 return ans[-1]
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. n2,n3,n5 = dp[a]*2,dp[b]*3,dp[c]*5 dp[i]=min(n2,n3,n5) #if the small one is n2/n3/n4, then the pointer of it will move one step to right. each round only the pointer of small one can be moved, others will stay at the same location. if dp[i] == n2: a+=1 if dp[i] == n3: b+=1 if dp[i] == n5: c+=1 return dp[-1]
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, 375, 384, 400, 405, 432, 450, 480, 486, 500, 512, 540, 576, 600, 625, 640, 648, 675, 720, 729, 750, 768, 800, 810, 864, 900, 960, 972, 1000, 1024, 1080, 1125, 1152, 1200, 1215, 1250, 1280, 1296, 1350, 1440, 1458, 1500, 1536, 1600, 1620, 1728, 1800, 1875, 1920, 1944, 2000, 2025, 2048, 2160, 2187, 2250, 2304, 2400, 2430, 2500, 2560, 2592, 2700, 2880, 2916, 3000, 3072, 3125, 3200, 3240, 3375, 3456, 3600, 3645, 3750, 3840, 3888, 4000, 4050, 4096, 4320, 4374, 4500, 4608, 4800, 4860, 5000, 5120, 5184, 5400, 5625, 5760, 5832, 6000, 6075, 6144, 6250, 6400, 6480, 6561, 6750, 6912, 7200, 7290, 7500, 7680, 7776, 8000, 8100, 8192, 8640, 8748, 9000, 9216, 9375, 9600, 9720, 10000, 10125, 10240, 10368, 10800, 10935, 11250, 11520, 11664, 12000, 12150, 12288, 12500, 12800, 12960, 13122, 13500, 13824, 14400, 14580, 15000, 15360, 15552, 15625, 16000, 16200, 16384, 16875, 17280, 17496, 18000, 18225, 18432, 18750, 19200, 19440, 19683, 20000, 20250, 20480, 20736, 21600, 21870, 22500, 23040, 23328, 24000, 24300, 24576, 25000, 25600, 25920, 26244, 27000, 27648, 28125, 28800, 29160, 30000, 30375, 30720, 31104, 31250, 32000, 32400, 32768, 32805, 33750, 34560, 34992, 36000, 36450, 36864, 37500, 38400, 38880, 39366, 40000, 40500, 40960, 41472, 43200, 43740, 45000, 46080, 46656, 46875, 48000, 48600, 49152, 50000, 50625, 51200, 51840, 52488, 54000, 54675, 55296, 56250, 57600, 58320, 59049, 60000, 60750, 61440, 62208, 62500, 64000, 64800, 65536, 65610, 67500, 69120, 69984, 72000, 72900, 73728, 75000, 76800, 77760, 78125, 78732, 80000, 81000, 81920, 82944, 84375, 86400, 87480, 90000, 91125, 92160, 93312, 93750, 96000, 97200, 98304, 98415, 100000, 101250, 102400, 103680, 104976, 108000, 109350, 110592, 112500, 115200, 116640, 118098, 120000, 121500, 122880, 124416, 125000, 128000, 129600, 131072, 131220, 135000, 138240, 139968, 140625, 144000, 145800, 147456, 150000, 151875, 153600, 155520, 156250, 157464, 160000, 162000, 163840, 164025, 165888, 168750, 172800, 174960, 177147, 180000, 182250, 184320, 186624, 187500, 192000, 194400, 196608, 196830, 200000, 202500, 204800, 207360, 209952, 216000, 218700, 221184, 225000, 230400, 233280, 234375, 236196, 240000, 243000, 245760, 248832, 250000, 253125, 256000, 259200, 262144, 262440, 270000, 273375, 276480, 279936, 281250, 288000, 291600, 294912, 295245, 300000, 303750, 307200, 311040, 312500, 314928, 320000, 324000, 327680, 328050, 331776, 337500, 345600, 349920, 354294, 360000, 364500, 368640, 373248, 375000, 384000, 388800, 390625, 393216, 393660, 400000, 405000, 409600, 414720, 419904, 421875, 432000, 437400, 442368, 450000, 455625, 460800, 466560, 468750, 472392, 480000, 486000, 491520, 492075, 497664, 500000, 506250, 512000, 518400, 524288, 524880, 531441, 540000, 546750, 552960, 559872, 562500, 576000, 583200, 589824, 590490, 600000, 607500, 614400, 622080, 625000, 629856, 640000, 648000, 655360, 656100, 663552, 675000, 691200, 699840, 703125, 708588, 720000, 729000, 737280, 746496, 750000, 759375, 768000, 777600, 781250, 786432, 787320, 800000, 810000, 819200, 820125, 829440, 839808, 843750, 864000, 874800, 884736, 885735, 900000, 911250, 921600, 933120, 937500, 944784, 960000, 972000, 983040, 984150, 995328, 1000000, 1012500, 1024000, 1036800, 1048576, 1049760, 1062882, 1080000, 1093500, 1105920, 1119744, 1125000, 1152000, 1166400, 1171875, 1179648, 1180980, 1200000, 1215000, 1228800, 1244160, 1250000, 1259712, 1265625, 1280000, 1296000, 1310720, 1312200, 1327104, 1350000, 1366875, 1382400, 1399680, 1406250, 1417176, 1440000, 1458000, 1474560, 1476225, 1492992, 1500000, 1518750, 1536000, 1555200, 1562500, 1572864, 1574640, 1594323, 1600000, 1620000, 1638400, 1640250, 1658880, 1679616, 1687500, 1728000, 1749600, 1769472, 1771470, 1800000, 1822500, 1843200, 1866240, 1875000, 1889568, 1920000, 1944000, 1953125, 1966080, 1968300, 1990656, 2000000, 2025000, 2048000, 2073600, 2097152, 2099520, 2109375, 2125764, 2160000, 2187000, 2211840, 2239488, 2250000, 2278125, 2304000, 2332800, 2343750, 2359296, 2361960, 2400000, 2430000, 2457600, 2460375, 2488320, 2500000, 2519424, 2531250, 2560000, 2592000, 2621440, 2624400, 2654208, 2657205, 2700000, 2733750, 2764800, 2799360, 2812500, 2834352, 2880000, 2916000, 2949120, 2952450, 2985984, 3000000, 3037500, 3072000, 3110400, 3125000, 3145728, 3149280, 3188646, 3200000, 3240000, 3276800, 3280500, 3317760, 3359232, 3375000, 3456000, 3499200, 3515625, 3538944, 3542940, 3600000, 3645000, 3686400, 3732480, 3750000, 3779136, 3796875, 3840000, 3888000, 3906250, 3932160, 3936600, 3981312, 4000000, 4050000, 4096000, 4100625, 4147200, 4194304, 4199040, 4218750, 4251528, 4320000, 4374000, 4423680, 4428675, 4478976, 4500000, 4556250, 4608000, 4665600, 4687500, 4718592, 4723920, 4782969, 4800000, 4860000, 4915200, 4920750, 4976640, 5000000, 5038848, 5062500, 5120000, 5184000, 5242880, 5248800, 5308416, 5314410, 5400000, 5467500, 5529600, 5598720, 5625000, 5668704, 5760000, 5832000, 5859375, 5898240, 5904900, 5971968, 6000000, 6075000, 6144000, 6220800, 6250000, 6291456, 6298560, 6328125, 6377292, 6400000, 6480000, 6553600, 6561000, 6635520, 6718464, 6750000, 6834375, 6912000, 6998400, 7031250, 7077888, 7085880, 7200000, 7290000, 7372800, 7381125, 7464960, 7500000, 7558272, 7593750, 7680000, 7776000, 7812500, 7864320, 7873200, 7962624, 7971615, 8000000, 8100000, 8192000, 8201250, 8294400, 8388608, 8398080, 8437500, 8503056, 8640000, 8748000, 8847360, 8857350, 8957952, 9000000, 9112500, 9216000, 9331200, 9375000, 9437184, 9447840, 9565938, 9600000, 9720000, 9765625, 9830400, 9841500, 9953280, 10000000, 10077696, 10125000, 10240000, 10368000, 10485760, 10497600, 10546875, 10616832, 10628820, 10800000, 10935000, 11059200, 11197440, 11250000, 11337408, 11390625, 11520000, 11664000, 11718750, 11796480, 11809800, 11943936, 12000000, 12150000, 12288000, 12301875, 12441600, 12500000, 12582912, 12597120, 12656250, 12754584, 12800000, 12960000, 13107200, 13122000, 13271040, 13286025, 13436928, 13500000, 13668750, 13824000, 13996800, 14062500, 14155776, 14171760, 14348907, 14400000, 14580000, 14745600, 14762250, 14929920, 15000000, 15116544, 15187500, 15360000, 15552000, 15625000, 15728640, 15746400, 15925248, 15943230, 16000000, 16200000, 16384000, 16402500, 16588800, 16777216, 16796160, 16875000, 17006112, 17280000, 17496000, 17578125, 17694720, 17714700, 17915904, 18000000, 18225000, 18432000, 18662400, 18750000, 18874368, 18895680, 18984375, 19131876, 19200000, 19440000, 19531250, 19660800, 19683000, 19906560, 20000000, 20155392, 20250000, 20480000, 20503125, 20736000, 20971520, 20995200, 21093750, 21233664, 21257640, 21600000, 21870000, 22118400, 22143375, 22394880, 22500000, 22674816, 22781250, 23040000, 23328000, 23437500, 23592960, 23619600, 23887872, 23914845, 24000000, 24300000, 24576000, 24603750, 24883200, 25000000, 25165824, 25194240, 25312500, 25509168, 25600000, 25920000, 26214400, 26244000, 26542080, 26572050, 26873856, 27000000, 27337500, 27648000, 27993600, 28125000, 28311552, 28343520, 28697814, 28800000, 29160000, 29296875, 29491200, 29524500, 29859840, 30000000, 30233088, 30375000, 30720000, 31104000, 31250000, 31457280, 31492800, 31640625, 31850496, 31886460, 32000000, 32400000, 32768000, 32805000, 33177600, 33554432, 33592320, 33750000, 34012224, 34171875, 34560000, 34992000, 35156250, 35389440, 35429400, 35831808, 36000000, 36450000, 36864000, 36905625, 37324800, 37500000, 37748736, 37791360, 37968750, 38263752, 38400000, 38880000, 39062500, 39321600, 39366000, 39813120, 39858075, 40000000, 40310784, 40500000, 40960000, 41006250, 41472000, 41943040, 41990400, 42187500, 42467328, 42515280, 43046721, 43200000, 43740000, 44236800, 44286750, 44789760, 45000000, 45349632, 45562500, 46080000, 46656000, 46875000, 47185920, 47239200, 47775744, 47829690, 48000000, 48600000, 48828125, 49152000, 49207500, 49766400, 50000000, 50331648, 50388480, 50625000, 51018336, 51200000, 51840000, 52428800, 52488000, 52734375, 53084160, 53144100, 53747712, 54000000, 54675000, 55296000, 55987200, 56250000, 56623104, 56687040, 56953125, 57395628, 57600000, 58320000, 58593750, 58982400, 59049000, 59719680, 60000000, 60466176, 60750000, 61440000, 61509375, 62208000, 62500000, 62914560, 62985600, 63281250, 63700992, 63772920, 64000000, 64800000, 65536000, 65610000, 66355200, 66430125, 67108864, 67184640, 67500000, 68024448, 68343750, 69120000, 69984000, 70312500, 70778880, 70858800, 71663616, 71744535, 72000000, 72900000, 73728000, 73811250, 74649600, 75000000, 75497472, 75582720, 75937500, 76527504, 76800000, 77760000, 78125000, 78643200, 78732000, 79626240, 79716150, 80000000, 80621568, 81000000, 81920000, 82012500, 82944000, 83886080, 83980800, 84375000, 84934656, 85030560, 86093442, 86400000, 87480000, 87890625, 88473600, 88573500, 89579520, 90000000, 90699264, 91125000, 92160000, 93312000, 93750000, 94371840, 94478400, 94921875, 95551488, 95659380, 96000000, 97200000, 97656250, 98304000, 98415000, 99532800, 100000000, 100663296, 100776960, 101250000, 102036672, 102400000, 102515625, 103680000, 104857600, 104976000, 105468750, 106168320, 106288200, 107495424, 108000000, 109350000, 110592000, 110716875, 111974400, 112500000, 113246208, 113374080, 113906250, 114791256, 115200000, 116640000, 117187500, 117964800, 118098000, 119439360, 119574225, 120000000, 120932352, 121500000, 122880000, 123018750, 124416000, 125000000, 125829120, 125971200, 126562500, 127401984, 127545840, 128000000, 129140163, 129600000, 131072000, 131220000, 132710400, 132860250, 134217728, 134369280, 135000000, 136048896, 136687500, 138240000, 139968000, 140625000, 141557760, 141717600, 143327232, 143489070, 144000000, 145800000, 146484375, 147456000, 147622500, 149299200, 150000000, 150994944, 151165440, 151875000, 153055008, 153600000, 155520000, 156250000, 157286400, 157464000, 158203125, 159252480, 159432300, 160000000, 161243136, 162000000, 163840000, 164025000, 165888000, 167772160, 167961600, 168750000, 169869312, 170061120, 170859375, 172186884, 172800000, 174960000, 175781250, 176947200, 177147000, 179159040, 180000000, 181398528, 182250000, 184320000, 184528125, 186624000, 187500000, 188743680, 188956800, 189843750, 191102976, 191318760, 192000000, 194400000, 195312500, 196608000, 196830000, 199065600, 199290375, 200000000, 201326592, 201553920, 202500000, 204073344, 204800000, 205031250, 207360000, 209715200, 209952000, 210937500, 212336640, 212576400, 214990848, 215233605, 216000000, 218700000, 221184000, 221433750, 223948800, 225000000, 226492416, 226748160, 227812500, 229582512, 230400000, 233280000, 234375000, 235929600, 236196000, 238878720, 239148450, 240000000, 241864704, 243000000, 244140625, 245760000, 246037500, 248832000, 250000000, 251658240, 251942400, 253125000, 254803968, 255091680, 256000000, 258280326, 259200000, 262144000, 262440000, 263671875, 265420800, 265720500, 268435456, 268738560, 270000000, 272097792, 273375000, 276480000, 279936000, 281250000, 283115520, 283435200, 284765625, 286654464, 286978140, 288000000, 291600000, 292968750, 294912000, 295245000, 298598400, 300000000, 301989888, 302330880, 303750000, 306110016, 307200000, 307546875, 311040000, 312500000, 314572800, 314928000, 316406250, 318504960, 318864600, 320000000, 322486272, 324000000, 327680000, 328050000, 331776000, 332150625, 335544320, 335923200, 337500000, 339738624, 340122240, 341718750, 344373768, 345600000, 349920000, 351562500, 353894400, 354294000, 358318080, 358722675, 360000000, 362797056, 364500000, 368640000, 369056250, 373248000, 375000000, 377487360, 377913600, 379687500, 382205952, 382637520, 384000000, 387420489, 388800000, 390625000, 393216000, 393660000, 398131200, 398580750, 400000000, 402653184, 403107840, 405000000, 408146688, 409600000, 410062500, 414720000, 419430400, 419904000, 421875000, 424673280, 425152800, 429981696, 430467210, 432000000, 437400000, 439453125, 442368000, 442867500, 447897600, 450000000, 452984832, 453496320, 455625000, 459165024, 460800000, 466560000, 468750000, 471859200, 472392000, 474609375, 477757440, 478296900, 480000000, 483729408, 486000000, 488281250, 491520000, 492075000, 497664000, 500000000, 503316480, 503884800, 506250000, 509607936, 510183360, 512000000, 512578125, 516560652, 518400000, 524288000, 524880000, 527343750, 530841600, 531441000, 536870912, 537477120, 540000000, 544195584, 546750000, 552960000, 553584375, 559872000, 562500000, 566231040, 566870400, 569531250, 573308928, 573956280, 576000000, 583200000, 585937500, 589824000, 590490000, 597196800, 597871125, 600000000, 603979776, 604661760, 607500000, 612220032, 614400000, 615093750, 622080000, 625000000, 629145600, 629856000, 632812500, 637009920, 637729200, 640000000, 644972544, 645700815, 648000000, 655360000, 656100000, 663552000, 664301250, 671088640, 671846400, 675000000, 679477248, 680244480, 683437500, 688747536, 691200000, 699840000, 703125000, 707788800, 708588000, 716636160, 717445350, 720000000, 725594112, 729000000, 732421875, 737280000, 738112500, 746496000, 750000000, 754974720, 755827200, 759375000, 764411904, 765275040, 768000000, 774840978, 777600000, 781250000, 786432000, 787320000, 791015625, 796262400, 797161500, 800000000, 805306368, 806215680, 810000000, 816293376, 819200000, 820125000, 829440000, 838860800, 839808000, 843750000, 849346560, 850305600, 854296875, 859963392, 860934420, 864000000, 874800000, 878906250, 884736000, 885735000, 895795200, 900000000, 905969664, 906992640, 911250000, 918330048, 921600000, 922640625, 933120000, 937500000, 943718400, 944784000, 949218750, 955514880, 956593800, 960000000, 967458816, 972000000, 976562500, 983040000, 984150000, 995328000, 996451875, 1000000000, 1006632960, 1007769600, 1012500000, 1019215872, 1020366720, 1024000000, 1025156250, 1033121304, 1036800000, 1048576000, 1049760000, 1054687500, 1061683200, 1062882000, 1073741824, 1074954240, 1076168025, 1080000000, 1088391168, 1093500000, 1105920000, 1107168750, 1119744000, 1125000000, 1132462080, 1133740800, 1139062500, 1146617856, 1147912560, 1152000000, 1162261467, 1166400000, 1171875000, 1179648000, 1180980000, 1194393600, 1195742250, 1200000000, 1207959552, 1209323520, 1215000000, 1220703125, 1224440064, 1228800000, 1230187500, 1244160000, 1250000000, 1258291200, 1259712000, 1265625000, 1274019840, 1275458400, 1280000000, 1289945088, 1291401630, 1296000000, 1310720000, 1312200000, 1318359375, 1327104000, 1328602500, 1342177280, 1343692800, 1350000000, 1358954496, 1360488960, 1366875000, 1377495072, 1382400000, 1399680000, 1406250000, 1415577600, 1417176000, 1423828125, 1433272320, 1434890700, 1440000000, 1451188224, 1458000000, 1464843750, 1474560000, 1476225000, 1492992000, 1500000000, 1509949440, 1511654400, 1518750000, 1528823808, 1530550080, 1536000000, 1537734375, 1549681956, 1555200000, 1562500000, 1572864000, 1574640000, 1582031250, 1592524800, 1594323000, 1600000000, 1610612736, 1612431360, 1620000000, 1632586752, 1638400000, 1640250000, 1658880000, 1660753125, 1677721600, 1679616000, 1687500000, 1698693120, 1700611200, 1708593750, 1719926784, 1721868840, 1728000000, 1749600000, 1757812500, 1769472000, 1771470000, 1791590400, 1793613375, 1800000000, 1811939328, 1813985280, 1822500000, 1836660096, 1843200000, 1845281250, 1866240000, 1875000000, 1887436800, 1889568000, 1898437500, 1911029760, 1913187600, 1920000000, 1934917632, 1937102445, 1944000000, 1953125000, 1966080000, 1968300000, 1990656000, 1992903750, 2000000000, 2013265920, 2015539200, 2025000000, 2038431744, 2040733440, 2048000000, 2050312500, 2066242608, 2073600000, 2097152000, 2099520000, 2109375000, 2123366400] return lst[n-1]
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(by2==mn): two_p+=1 if(by3==mn): three_p+=1 if(by5==mn): five_p+=1 return res[-1]
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 += 1 return dp[n - 1]
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[i3]*3 == nxt: i3 += 1 if res[i5]*5 == nxt: i5 += 1 res[i] = nxt return res[-1]
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(top*2) if top*3 not in set_val: heappush(heaped,top*3) set_val.add(top*3) if top*5 not in set_val: heappush(heaped,top*5) set_val.add(top*5) n-=1 return result
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] == d[i3]*3: i3 += 1 if d[-1] == d[i5]*5: i5 += 1 return d[n-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[-1]: num = heappop(heap) ugly.append(num) i += 1 if i == n: return ugly[-1]
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]: q3.popleft() if nxt==q5[0]: q5.popleft() q2.append(nxt*2) q3.append(nxt*3) q5.append(nxt*5) return nxt
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]: count_3 += 1 while ugly_list[count_5] * 5 <= ugly_list[-1]: count_5 += 1 next_ugly = min(ugly_list[count_2] * 2, ugly_list[count_3] * 3, ugly_list[count_5] * 5) ugly_list += (next_ugly,) return 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 range(n-1): new_n = ll[len(ll)-1] min_n = new_n * 10 d2 = self.find_upper(new_n//2, ll, len(ll)//2) * 2 if (d2 < min_n): min_n = d2 d3 = self.find_upper(new_n//3, ll, len(ll)//3) * 3 if (d3 < min_n): min_n = d3 d5 = self.find_upper(new_n//5, ll, len(ll)//5) * 5 if (d5 < min_n): min_n = d5 ll.append(min_n) re = min_n return(re)
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 array elements... sum -= i return sum
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 all the array elements... sum -= i return sum
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 if nums[i] < len(nums) and nums[i] != i: nums[i], nums[j] = nums[j], nums[i] # Swap else: i += 1 # Now we just iterate through the array and look for the value that doesn't match its index for idx in range(len(nums)): if nums[idx] != idx: return idx # If all values match their index, then the only missing number is the end the the range # So we return len(nums) return len(nums)
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 numxor = 0 for i,el in enumerate(nums): numxor ^= (i ^ el) return numxor ^ (i+1) #Method 1 - Math O(n) time O(1) space l = len(nums) # print(l) rsum = sum(range(l+1)) # print(rsum) return rsum - sum(nums)
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): right = mid else: left = mid + 1 return left
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)@hotmail.com
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: """ Time Complexity: O(Nlog(N)) Space Complexity: O(N) """ nums.sort() for i, num in enumerate(nums): if i != num: return i return len(nums) def binary_search(self, nums: List[int]) -> int: """ Time Complexity: O(Nlog(N)) if nums not sorted O(log(N)) if nums already sorted Space Complexity: O(N) if nums not sorted O(1) if nums sorted """ nums.sort() left, right = 0, len(nums) mid = (left+right)//2 while left < right: if nums[mid] == mid: left = mid+1 else: right = mid - 1 mid = (left + right)//2 return mid + 1 def hashing(self, nums: List[int]) -> int: """ Time Complexity: O(N) Space Complexity: O(N) """ nums_set = set(nums) N = len(nums) for i in range(N): if i not in nums_set: return i return len(nums) def gauss_formula(self, nums: List[int]) -> int: """ Time Complexity: O(N) Space Complexity: O(1) """ N = len(nums) return N*(N + 1)//2 - sum(nums) def xor(self, nums: List[int]) -> int: """ Time Complexity: O(N) Space Complexity: O(1) """ result = len(nums) for i, v in enumerate(nums): result ^= i^v return result def cyclic_swapping(self, nums: List[int]) -> int: """ Time Complexity: O(N) Space Complexity: O(1) """ current = 0 N = len(nums) count = 0 while current < N: count+= 1 if nums[current] == N: current += 1 continue if nums[current] == nums[nums[current]]: current += 1 else: temp = nums[current] nums[current] = nums[nums[current]] nums[temp] = temp for i, v in enumerate(nums): if i != v: return i return N def value_inversion(self, nums: List[int]) -> int: """ Time Complexity: O(N) Space Complexity: O(1) Advantages: - Original Input array can be restored """ for i, _ in enumerate(nums): nums[i] += 1 for i, v in enumerate(nums): if abs(v) > len(nums): continue nums[abs(v)-1] = -abs(nums[abs(v)-1]) for i, v in enumerate(nums): if v > 0: return i return len(nums) def missingNumber(self, nums: List[int]) -> int: # return self.brute_force(nums) # return self.sorting(nums) # return self.hashing(nums) # return self.gauss_formula(nums) # return self.xor(nums) # return self.cyclic_swapping(nums) # return self.binary_search(nums) return self.value_inversion(nums)
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