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/valid-perfect-square/discuss/2092461/Python-or-O(logn)-solution
class Solution: def isPerfectSquare(self, num: int) -> bool: if num == 1: return True start = 0 end = num while start <= end: mid = (start + end) // 2 if mid*mid == num: return True if mid*mid < num: start = mid + 1 else: end = mid - 1 return False
valid-perfect-square
Python | O(logn) solution
rahulsh31
0
64
valid perfect square
367
0.433
Easy
6,300
https://leetcode.com/problems/valid-perfect-square/discuss/2057210/Python-3-or-Really-Simple-Solution
class Solution: def isPerfectSquare(self, num: int) -> bool: root = math.sqrt(num) if root % 1 == 0: return True else: return False
valid-perfect-square
Python 3 | Really Simple Solution
quiseo
0
55
valid perfect square
367
0.433
Easy
6,301
https://leetcode.com/problems/valid-perfect-square/discuss/2045562/The-simplest-way
class Solution(object): def isPerfectSquare(self, num): """ :type num: int :rtype: bool """ if num**(0.5)%1 == 0: return True else: return False
valid-perfect-square
The simplest way
shubhsach16
0
32
valid perfect square
367
0.433
Easy
6,302
https://leetcode.com/problems/valid-perfect-square/discuss/2032396/Binary-Search-in-python3-without-functions
class Solution: def isPerfectSquare(self, num: int) -> bool: if num == 1 or num == 0: return True low , high = 0, num mid = 0 while low +1 < high: mid = (low + high ) // 2 if (mid*mid) > num: high = mid elif (mid*mid) < num: low = mid else: return True return False
valid-perfect-square
Binary Search in python3 without functions
Yodawgz0
0
36
valid perfect square
367
0.433
Easy
6,303
https://leetcode.com/problems/valid-perfect-square/discuss/1964202/Python-oror-Clean-and-Simple-Binary-Search
class Solution: def isPerfectSquare(self, num: int) -> bool: start, end = 0 , num while start <= end: mid = (start+end)//2 if mid*mid == num: return True elif mid*mid < num: start = mid + 1 else: end = mid-1 return False
valid-perfect-square
Python || Clean and Simple Binary Search
morpheusdurden
0
50
valid perfect square
367
0.433
Easy
6,304
https://leetcode.com/problems/valid-perfect-square/discuss/1945496/java-python3-binary-search-(Time-O1-space-O1)
class Solution: def isPerfectSquare(self, num: int) -> bool: l = 0 r = 2**31 - 1 while l <= r : m = (l + r) // 2 n = m * m if n == num : return True if n > num : r = m - 1 else : l = m + 1 return False
valid-perfect-square
java, python3 - binary search (Time O1, space O1)
ZX007java
0
44
valid perfect square
367
0.433
Easy
6,305
https://leetcode.com/problems/valid-perfect-square/discuss/1920518/simple-python-solution
class Solution: def isPerfectSquare(self, num: int) -> bool: return pow(num,0.5).is_integer()
valid-perfect-square
simple python solution
jaymishpatel
0
47
valid perfect square
367
0.433
Easy
6,306
https://leetcode.com/problems/valid-perfect-square/discuss/1907801/Python-easy-to-understand-code
class Solution: def isPerfectSquare(self, num: int) -> bool: num = num**0.5 if num - int(num) == 0: return True else: return False
valid-perfect-square
Python easy to understand code
aashnachib17
0
53
valid perfect square
367
0.433
Easy
6,307
https://leetcode.com/problems/valid-perfect-square/discuss/1675020/Python-solution-using-Binary-Search
class Solution: def isPerfectSquare(self, num: int) -> bool: s = 1 l = num ans = 0 while s <= l: mid = s + (l - s) // 2 square = mid * mid if (square > num): l = mid - 1 else: ans = mid s = mid + 1 if ans * ans == num: return True else: return False
valid-perfect-square
Python solution using Binary Search
Joe-Felix
0
103
valid perfect square
367
0.433
Easy
6,308
https://leetcode.com/problems/valid-perfect-square/discuss/1588909/Python-quick-solution-without-using-sqrt
class Solution: def isPerfectSquare(self, num): return str(num**(1/2))[-1] == "0"
valid-perfect-square
Python quick solution without using sqrt
Novand2121
0
91
valid perfect square
367
0.433
Easy
6,309
https://leetcode.com/problems/valid-perfect-square/discuss/1585209/Python-Easy-Solution-or-Faster-than-98
class Solution: def isPerfectSquare(self, num: int) -> bool: start = 0 end = num while start <= end: mid = start+(end-start)//2 if mid*mid == num: root = mid break if mid*mid > num: end = mid-1 else: start = mid+1 root = mid if root*root == num: return True return False
valid-perfect-square
Python Easy Solution | Faster than 98%
leet_satyam
0
109
valid perfect square
367
0.433
Easy
6,310
https://leetcode.com/problems/valid-perfect-square/discuss/1568547/max-runtime-993-real-working-code-O-log(n)
class Solution: def isPerfectSquare(self, num: int) -> bool: le = 1 ri = 46341 while le <= ri: mid = (le + ri) // 2 if mid * mid == num: return mid if mid * mid < num: le = mid + 1 else: ri = mid - 1
valid-perfect-square
max runtime 99,3% real working code O = log(n)
petros000
0
55
valid perfect square
367
0.433
Easy
6,311
https://leetcode.com/problems/valid-perfect-square/discuss/1526548/Python-Three-Solutions-Brute-Force-greater-AC
class Solution: def isPerfectSquare(self, num: int) -> bool: num = str(num ** 0.5) trail = num[len(num)-2:] return trail == ".0"
valid-perfect-square
[Python] Three Solutions, Brute Force --> AC
dev-josh
0
50
valid perfect square
367
0.433
Easy
6,312
https://leetcode.com/problems/valid-perfect-square/discuss/1526548/Python-Three-Solutions-Brute-Force-greater-AC
class Solution: def isPerfectSquare(self, num: int) -> bool: for i in range(num+1): if i*i == num: return True return False
valid-perfect-square
[Python] Three Solutions, Brute Force --> AC
dev-josh
0
50
valid perfect square
367
0.433
Easy
6,313
https://leetcode.com/problems/valid-perfect-square/discuss/1526548/Python-Three-Solutions-Brute-Force-greater-AC
class Solution: def isPerfectSquare(self, num: int) -> bool: lo, hi = 1, num+1 while lo < hi: mid = (lo+hi) // 2 guess = mid*mid if guess == num: return True elif guess < num: lo = mid+1 elif guess > num: hi = mid return False
valid-perfect-square
[Python] Three Solutions, Brute Force --> AC
dev-josh
0
50
valid perfect square
367
0.433
Easy
6,314
https://leetcode.com/problems/valid-perfect-square/discuss/1250531/Python3-simple-solution-%22one-liner%22
class Solution: def isPerfectSquare(self, num: int) -> bool: return num**.5%1 == 0
valid-perfect-square
Python3 simple solution "one-liner"
EklavyaJoshi
0
44
valid perfect square
367
0.433
Easy
6,315
https://leetcode.com/problems/largest-divisible-subset/discuss/1127633/Python-Dynamic-Programming-with-comments
class Solution: def largestDivisibleSubset(self, nums: List[int]) -> List[int]: if not nums or len(nums) == 0: return [] # since we are doing a "subset" question # sorting does not make any differences nums.sort() n = len(nums) # initilization # f[i] represents the size of LDS ended with nums[i] f = [1 for _ in range(n)] for i in range(1, n): for j in range(i): # since we have already sorted, # then nums[j] % nums[i] will never equals zero # unless nums[i] == nums[j] if nums[i] % nums[j] == 0: f[i] = max(f[i], f[j] + 1) # extract result from dp array max_size = max(f) max_idx = f.index(max_size) # since we can return one of the largest prev_num, prev_size = nums[max_idx], f[max_idx] res = [prev_num] for curr_idx in range(max_idx, -1, -1): if prev_num % nums[curr_idx] == 0 and f[curr_idx] == prev_size - 1: # update res.append(nums[curr_idx]) prev_num = nums[curr_idx] prev_size = f[curr_idx] return res[::-1]
largest-divisible-subset
Python Dynamic Programming with comments
zna2
3
227
largest divisible subset
368
0.413
Medium
6,316
https://leetcode.com/problems/largest-divisible-subset/discuss/2811050/Python-(Simple-Dynamic-Programming)
class Solution: def largestDivisibleSubset(self, nums): nums.sort() n = len(nums) dp = [[nums[i]] for i in range(n)] for i in range(n): for j in range(i): if nums[i]%nums[j] == 0 and len(dp[j]) + 1 > len(dp[i]): dp[i] = dp[j] + [nums[i]] return max(dp, key = len)
largest-divisible-subset
Python (Simple Dynamic Programming)
rnotappl
0
6
largest divisible subset
368
0.413
Medium
6,317
https://leetcode.com/problems/largest-divisible-subset/discuss/2647795/Python3-solution-with-comments-(similar-to-300.-Longest-Increasing-Subsequence)
class Solution: def lengthOfLIS(self, nums: List[int]) -> int: # tuition: dp[i] represents the length of the longest strictly increasing subsequence ending with nums[i]. recursive formula: dp[i]=max(dp[j]+1, dp[i]) with 0 < j < i. This takes O(n^2) time. n = len(nums) if n <= 1: return n dp = [1 for _ in range(n)] for i in range(n): for j in range(i): if nums[j] < nums[i]: # strictly increasing dp[i] = max(dp[i], dp[j] + 1) return max(dp)
largest-divisible-subset
Python3 solution with comments (similar to #300. Longest Increasing Subsequence)
cutesunny
0
1
largest divisible subset
368
0.413
Medium
6,318
https://leetcode.com/problems/largest-divisible-subset/discuss/2647795/Python3-solution-with-comments-(similar-to-300.-Longest-Increasing-Subsequence)
class Solution: def largestDivisibleSubset(self, nums: List[int]) -> List[int]: # O(n^2) time nums.sort() # so that divisors appear ahead of each number n = len(nums) if n == 0: return [] dp = [[num] for num in nums] # dp[i] represents the valid solution including nums[i] for i in range(n): for j in range(i): if nums[i] % nums[j] == 0 and len(dp[i]) < len(dp[j]) + 1: # find better solution, update dp[i] = dp[j] + [nums[i]] return max(dp, key = len) # customer sort based on length
largest-divisible-subset
Python3 solution with comments (similar to #300. Longest Increasing Subsequence)
cutesunny
0
1
largest divisible subset
368
0.413
Medium
6,319
https://leetcode.com/problems/largest-divisible-subset/discuss/2322104/Python3-Solution-with-using-dp
class Solution: def largestDivisibleSubset(self, nums: List[int]) -> List[int]: if len(nums) == 0: return [] nums.sort() dp = [[num] for num in nums] for i in range(len(nums)): for j in range(i): if nums[i] % nums[j] == 0 and len(dp[j]) >= len(dp[i]): dp[i] = dp[j] + [nums[i]] max_len = 0 res = [] for subset in dp: if len(subset) > max_len: max_len = len(subset) res = subset return res
largest-divisible-subset
[Python3] Solution with using dp
maosipov11
0
40
largest divisible subset
368
0.413
Medium
6,320
https://leetcode.com/problems/largest-divisible-subset/discuss/2146559/Python-3-or-DP-or-Easy-Understand
class Solution: def largestDivisibleSubset(self, nums: List[int]) -> List[int]: nums.sort() dp = [[i] for i in nums] for i in range(1, len(nums)): for j in range(i): if nums[i] % nums[j] == 0: if len(dp[j]) + 1 > len(dp[i]): dp[i] = [nums[i]] + dp[j] else: pass else: continue max_len = 1 for lst in dp: max_len = max(max_len, len(lst)) for lst in dp: if max_len == len(lst): return lst
largest-divisible-subset
Python 3 | DP | Easy Understand
itachieve
0
23
largest divisible subset
368
0.413
Medium
6,321
https://leetcode.com/problems/largest-divisible-subset/discuss/1122066/Python-DP-Solution
class Solution: def largestDivisibleSubset(self, nums: List[int]) -> List[int]: nums.sort() t = [1 for _ in range(len(nums))] for i in range(1,len(t)): for j in range(i): if nums[i]%nums[j]==0: t[i] = max(t[i],1+t[j]) maxi = float('-inf') for i in range(len(t)): if maxi<t[i]: maxi=t[i] ind = i subset = [nums[ind]] max_ = maxi-1 prev = subset[0] for i in range(len(t)-1,-1,-1): if t[i]==max_ and prev%nums[i]==0: subset.append(nums[i]) prev = nums[i] max_-=1 return subset
largest-divisible-subset
Python DP Solution
bharatgg
0
106
largest divisible subset
368
0.413
Medium
6,322
https://leetcode.com/problems/largest-divisible-subset/discuss/685654/Python3-6-line
class Solution: def largestDivisibleSubset(self, nums: List[int]) -> List[int]: nums.sort() dp = [] for i, x in enumerate(nums): dp.append([x]) for ii in range(i): if x % nums[ii] == 0: dp[-1] = max(dp[-1], dp[ii] + [x], key=len) return max(dp, key=len)
largest-divisible-subset
[Python3] 6-line
ye15
0
60
largest divisible subset
368
0.413
Medium
6,323
https://leetcode.com/problems/sum-of-two-integers/discuss/1876632/Python-one-line-solution-using-the-logic-of-logs-and-powers
class Solution: def getSum(self, a: int, b: int) -> int: return int(math.log2(2**a * 2**b))
sum-of-two-integers
Python one line solution using the logic of logs and powers
alishak1999
7
596
sum of two integers
371
0.507
Medium
6,324
https://leetcode.com/problems/sum-of-two-integers/discuss/1413403/Not-weeb-does-python
class Solution: def getSum(self, a: int, b: int) -> int: return int(log2(2**a * 2**b))
sum-of-two-integers
Not weeb does python
blackcoffee
5
336
sum of two integers
371
0.507
Medium
6,325
https://leetcode.com/problems/sum-of-two-integers/discuss/1680696/Python3-or-Bit-manipulation-solution-handling-subtracts-and-adds
class Solution: def getSum(self, a: int, b: int) -> int: self.level = -1 self.sum = 0 self.neg = False self.subtract = False def doSum(m, n): if not m and not n: return self.sum elif not m and n: return n elif not n and m: if m == 1: self.level += 1 self.sum += 1*2**self.level return self.sum else: return m elif self.subtract: carry = (m%2) ^ (n%2) self.level += 1 self.sum += carry*2**self.level if not m%2 and n%2: m = (m-1)>>1 else: m >>= 1 return doSum(m, n>>1) else: return doSum(m^n, (m&amp;n)<<1) if a < 0 and b < 0: a, b = -a, -b self.neg = True if a*b < 0: self.subtract = True if abs(a) == abs(b): return 0 elif abs(a) > abs(b): if a < b: a, b = -a, b self.neg = True else: if a < b: a, b = b, -a else: a, b = -b, a self.neg = True self.sum = doSum(a, b) return -self.sum if self.neg else self.sum
sum-of-two-integers
Python3 | Bit manipulation solution handling subtracts and adds
elainefaith0314
3
868
sum of two integers
371
0.507
Medium
6,326
https://leetcode.com/problems/sum-of-two-integers/discuss/810086/Python3-bit-xorand
class Solution: def getSum(self, a: int, b: int) -> int: mask = 0xffffffff while b &amp; mask: a, b = a^b, (a&amp;b) << 1 return a &amp; mask if b > mask else a
sum-of-two-integers
[Python3] bit xor/and
ye15
3
558
sum of two integers
371
0.507
Medium
6,327
https://leetcode.com/problems/sum-of-two-integers/discuss/2699859/PYTHON-oror-EASY-TO-READ
class Solution: def getSum(self, a: int, b: int) -> int: return eval(f'{a}{chr(43)}{b}')
sum-of-two-integers
✅ 🔥 🎉 PYTHON || EASY TO READ 🎉 🔥✅
neet_n_niftee
2
748
sum of two integers
371
0.507
Medium
6,328
https://leetcode.com/problems/sum-of-two-integers/discuss/2835718/SIMPLE-SOLUTION-USING-LISToror-3-LINESoror-EASY
class Solution: def getSum(self, a: int, b: int) -> int: l=[] l.append(a) l.append(b) return sum(l)
sum-of-two-integers
SIMPLE SOLUTION USING LIST|| 3 LINES|| EASY
thezealott
1
42
sum of two integers
371
0.507
Medium
6,329
https://leetcode.com/problems/sum-of-two-integers/discuss/656176/python3-beats-98
class Solution: def getSum(self, a: int, b: int) -> int: MAX_INT = 0x7FFFFFFF MIN_INT = 0x80000000 MASK = 0x100000000 while b: carry = (a &amp; b) a = (a ^ b) % MASK b = (carry << 1) % MASK if(a <= MAX_INT): return a else: return ~((a % MIN_INT) ^ MAX_INT)
sum-of-two-integers
python3 beats 98%
hunnain_a
1
505
sum of two integers
371
0.507
Medium
6,330
https://leetcode.com/problems/sum-of-two-integers/discuss/2839117/Python3-1-liner-without-%2B-
class Solution: def getSum(self, a: int, b: int) -> int: return sum([a, b])
sum-of-two-integers
Python3 - 1 liner without +/-
mediocre-coder
0
7
sum of two integers
371
0.507
Medium
6,331
https://leetcode.com/problems/sum-of-two-integers/discuss/2834651/Crude-solution
class Solution: def sameNegative(self, a: int, b: int) -> bool: if ( b < 0 ) and (a < 0): return True else: return False def getSum(self, a: int, b: int) -> int: start = 1 if (self.sameNegative(a, b)): max_range=abs(b) max_range+=1 a= abs(a) for i in range(start, max_range): # use a as an anchor a+=1 a=-abs(a) elif(a>0) and (b < 0): max_range=abs(b) max_range+=1 for i in range(start, max_range): # use a as an anchor a-=1 elif(a<0) and (b > 0): max_range=abs(b) max_range+=1 for i in range(start, max_range): # use a as an anchor a+=1 elif(b==0): return a elif(a==0): return b else: max_range=abs(b) max_range+=1 a= abs(a) for i in range(start, max_range): print("In here!") # use a as an anchor a+=1 return a
sum-of-two-integers
Crude solution
malfyore
0
2
sum of two integers
371
0.507
Medium
6,332
https://leetcode.com/problems/sum-of-two-integers/discuss/2833879/For-Begginers
class Solution: def getSum(self, a: int, b: int) -> int: return eval("a"+str(chr(43))+"b")
sum-of-two-integers
For Begginers
19121A04N7
0
3
sum of two integers
371
0.507
Medium
6,333
https://leetcode.com/problems/sum-of-two-integers/discuss/2830158/Python3-One-Liner-using-a-recursive-Bitshifting-Logic
class Solution: def getSum(self, a: int, b: int) -> int: return a if b == 0 else self.getSum(a^b, (a&amp;b)<<1)
sum-of-two-integers
Python3 One Liner, using a recursive Bitshifting Logic
Aleph-Null
0
8
sum of two integers
371
0.507
Medium
6,334
https://leetcode.com/problems/sum-of-two-integers/discuss/2804781/Python-or-O(n)-or-Easy-to-understand-Code
class Solution: def getSum(self, a: int, b: int) -> int: if a >= 0 and b >= 0: for i in range(b): a += 1 return a elif a < 0 and b >= 0: for i in range(b): a += 1 return a elif a >= 0 and b <= 0: for i in range(-b): a -= 1 return a elif a <= 0 and b <= 0: for i in range(-b): a -= 1 return a
sum-of-two-integers
Python | O(n) | Easy to understand Code
bhuvneshwar906
0
5
sum of two integers
371
0.507
Medium
6,335
https://leetcode.com/problems/sum-of-two-integers/discuss/2779414/python-very-easy-solution
class Solution: def getSum(self, a: int, b: int) -> int: return sum([a,b])
sum-of-two-integers
python very easy solution
seifsoliman
0
20
sum of two integers
371
0.507
Medium
6,336
https://leetcode.com/problems/sum-of-two-integers/discuss/2596892/Python-Solution-or-Beats-99-or-Trick-or-Bit-Manipulation
class Solution: def getSum(self, a: int, b: int) -> int: # Bit Manipulation: TLE (eg. -1, 1) # while b: # carry=(a&amp;b)<<1 # to carry it forward # a=a^b # it acts a sum # b=carry # return a # Way around ;) return sum([a,b])
sum-of-two-integers
Python Solution | Beats 99% | Trick | Bit-Manipulation
Siddharth_singh
0
274
sum of two integers
371
0.507
Medium
6,337
https://leetcode.com/problems/sum-of-two-integers/discuss/1665375/Python-recursive-solution-for-positive-numbers-only
class Solution: def getSum(self, a: int, b: int) -> int: s, carry = a ^ b, a &amp; b return s if not carry else self.getSum(s, carry << 1)
sum-of-two-integers
Python, recursive solution for positive numbers only
blue_sky5
0
146
sum of two integers
371
0.507
Medium
6,338
https://leetcode.com/problems/sum-of-two-integers/discuss/1615895/Python-3-Not-Using-Mask
class Solution: def getSum(self, a: int, b: int) -> int: plist = [] nlist = [] def run_a(): if a > 0: for i in range(a): plist.append(0) else: for i in range(abs(a)): nlist.append(0) def run_b(): if b > 0: for i in range(b): plist.append(0) else: for i in range(abs(b)): nlist.append(0) run_a() run_b() if len(nlist) == len(plist): return 0 if len(nlist) == 0 or len(plist)== 0: if len(nlist) == 0: return len(plist) else: nlist.append(a) nlist.append(b) return sum(nlist) if len(nlist) < len(plist): for n in nlist: plist.pop() return len(plist) if len(nlist) > len(plist): for n in plist: nlist.pop() return len(nlist)
sum-of-two-integers
Python 3 - Not Using Mask
hudsonh
0
998
sum of two integers
371
0.507
Medium
6,339
https://leetcode.com/problems/sum-of-two-integers/discuss/627502/Intuitive-approach-by-rule-based
class Solution: def getSum(self, a: int, b: int) -> int: def bin2int(bin_list): result = 0 for i, v in enumerate(bin_list): result += pow(2, i) * v return result if a == 0 or b == 0: return a if b == 0 else b elif (a > 0 and b > 0) or (a < 0 and b < 0): a_bin = list(map(lambda e: int(e), "{:032b}".format(abs(a))[::-1])) b_bin = list(map(lambda e: int(e), "{:032b}".format(abs(b))[::-1])) cb = 0 r_bin = [] for av, bv in zip(a_bin, b_bin): if av == 0 and bv == 0: r_bin.append(cb) cb = 0 elif (av == 0 and bv == 1) or (av == 1 and bv == 0): if cb: r_bin.append(0) else: r_bin.append(1) else: if cb: r_bin.append(1) else: r_bin.append(0) cb = 1 result = bin2int(r_bin) return result if a > 0 else -1 * result else: is_negative = False if a > 0: if a < abs(b): is_negative = True a, b = abs(b), a else: a, b = a, abs(b) else: if b < abs(a): is_negative = True a, b = abs(a), b else: a, b = b, abs(a) a_bin = list(map(lambda e: int(e), "{:032b}".format(a)[::-1])) b_bin = list(map(lambda e: int(e), "{:032b}".format(b)[::-1])) r_bin = [] for av, bv in zip(a_bin, b_bin): if av == bv: r_bin.append(0) elif av == 1 and bv == 0: r_bin.append(1) else: r_bin.append(-1) for i in range(len(r_bin)): if r_bin[i] < 0: for j in range(i+1, len(r_bin)): if r_bin[j] == 0: r_bin[j] == 1 else: r_bin[j] == 0 break r_bin[i] == 1 result = bin2int(r_bin) return result if not is_negative else -1 * result
sum-of-two-integers
Intuitive approach by rule based
puremonkey2001
0
189
sum of two integers
371
0.507
Medium
6,340
https://leetcode.com/problems/sum-of-two-integers/discuss/419864/Accepted-Answer-Python3%3A-One-Liner-return-a.__add__(b)
class Solution: def getSum(self, a: int, b: int) -> int: return a.__add__(b)
sum-of-two-integers
Accepted Answer Python3: One-Liner `return a.__add__(b)`
i-i
-1
736
sum of two integers
371
0.507
Medium
6,341
https://leetcode.com/problems/sum-of-two-integers/discuss/1235588/Simplest-Solution-in-python
class Solution: def getSum(self, a: int, b: int) -> int: return sum([a,b])
sum-of-two-integers
Simplest Solution in python
dhrumilg699
-20
621
sum of two integers
371
0.507
Medium
6,342
https://leetcode.com/problems/super-pow/discuss/400893/Python-3-(With-Explanation)-(Handles-All-Test-Cases)-(one-line)-(beats-~97)
class Solution: def superPow(self, a: int, b: List[int]) -> int: return (a % 1337)**(1140 + int(''.join(map(str, b))) % 1140) % 1337 - Junaid Mansuri
super-pow
Python 3 (With Explanation) (Handles All Test Cases) (one line) (beats ~97%)
junaidmansuri
11
1,900
super pow
372
0.371
Medium
6,343
https://leetcode.com/problems/super-pow/discuss/1420565/Python-3-or-Naive-array-operation-Euler's-theorem-(and-optimization)-or-Explanation
class Solution(object): def superPow(self, a, b): @cache def cus_pow(a, b): # A customized pow(a, b) if b == 0 or a == 1: return 1 if b % 2: return a * cus_pow(a, b - 1) % 1337 return cus_pow((a * a) % 1337, b / 2) % 1337 res = 1 for x in b: # power on array res = cus_pow(res, 10) * cus_pow(a, x) % 1337 return res
super-pow
Python 3 | Naïve array operation, Euler's theorem (and optimization) | Explanation
idontknoooo
2
317
super pow
372
0.371
Medium
6,344
https://leetcode.com/problems/super-pow/discuss/1420565/Python-3-or-Naive-array-operation-Euler's-theorem-(and-optimization)-or-Explanation
class Solution: def superPow(self, a: int, b: List[int]) -> int: if a % 1337 == 0: return 0 else: exponent = int(''.join(map(str, b))) return pow(a, exponent % 1140 + 1140, 1337)
super-pow
Python 3 | Naïve array operation, Euler's theorem (and optimization) | Explanation
idontknoooo
2
317
super pow
372
0.371
Medium
6,345
https://leetcode.com/problems/super-pow/discuss/1420565/Python-3-or-Naive-array-operation-Euler's-theorem-(and-optimization)-or-Explanation
class Solution: def superPow(self, a: int, b: List[int]) -> int: if a % 1337 == 0: return 0 else: exponent = int(''.join(map(str, b))) return pow(a, exponent % 1140, 1337)
super-pow
Python 3 | Naïve array operation, Euler's theorem (and optimization) | Explanation
idontknoooo
2
317
super pow
372
0.371
Medium
6,346
https://leetcode.com/problems/super-pow/discuss/2645771/Python-EXPLAINED-or-recursive-or-O(log(n))-or-comparison-with-Pow(x-n)
class Solution: def superPow(self, a: int, b: List[int]) -> int: b = [str(i) for i in b] n = int("".join(b)) #converting list into integer def pow(x, n): if n == 0: return 1 elif n%2==0: res = pow(x, n>>1)%1337 return res**2 else: res = pow(x, (n-1)>>1)%1337 return x*(res**2) return pow(a, n)%1337
super-pow
Python [EXPLAINED] | recursive | O(log(n)) | comparison with Pow(x, n)
diwakar_4
1
168
super pow
372
0.371
Medium
6,347
https://leetcode.com/problems/super-pow/discuss/2645771/Python-EXPLAINED-or-recursive-or-O(log(n))-or-comparison-with-Pow(x-n)
class Solution: def myPow(self, x: float, n: int) -> float: def pow(x, n): if n == 0: return 1 elif n%2==0: res = pow(x, n/2) return res*res else: res = pow(x, (n-1)/2) return x*(res**2) if n>=0: return pow(x, n) else: return 1/pow(x, abs(n))
super-pow
Python [EXPLAINED] | recursive | O(log(n)) | comparison with Pow(x, n)
diwakar_4
1
168
super pow
372
0.371
Medium
6,348
https://leetcode.com/problems/super-pow/discuss/1920679/Simple-Python-Solution
class Solution: def calc_pow(self,x,n): if n == 0: return 1 mid = self.calc_pow(x,n//2) if n%2==0: return (mid*mid)%1337 else: return (x*mid*mid)%1337 def superPow(self, a: int, b: List[int]) -> int: b_str = "".join([str(i) for i in b]) power = int(b_str) return self.calc_pow(a,power)
super-pow
Simple Python Solution
palakmehta
1
213
super pow
372
0.371
Medium
6,349
https://leetcode.com/problems/super-pow/discuss/1709461/Python-oror-Explanation-oror-Built-in-function-oror-DP-DFS-oror-Easy-to-understand
class Solution: def superPow(self, a: int, b: List[int]) -> int: if a==1: return 1 c=0 for i in b: c=10*c+i return pow(a,c,1337)
super-pow
Python || Explanation || Built-in function || ❌ DP ❌ DFS || Easy to understand
rushi_javiya
1
212
super pow
372
0.371
Medium
6,350
https://leetcode.com/problems/super-pow/discuss/2843437/Pythonor-fast-power
class Solution: def mypaw(self, a, b): a %= 1337 res = 1 for _ in range(b): res *= a res %= 1337 return res def superPow(self, a: int, b: List[int]) -> int: if len(b) == 0: return 1 if a == 1: return 1 last = b.pop() part1 = self.mypaw(a, last) part2 = self.mypaw(self.superPow(a,b), 10) return (part1*part2)%1337
super-pow
Python| fast power
lucy_sea
0
3
super pow
372
0.371
Medium
6,351
https://leetcode.com/problems/super-pow/discuss/2834338/Recursion-and-Mod
class Solution: def __init__(self): self.base = 1337 def superPow(self, a: int, b: List[int]) -> int: if not b: return 1 last = b.pop() part1 = self.my_power(a, last) part2 = self.my_power(self.superPow(a, b), 10) return (part1 * part2) % self.base def my_power(self, a, k): a %= self.base res = 1 for _ in range(k): res *= a res %= self.base return res
super-pow
Recursion & Mod
lillllllllly
0
3
super pow
372
0.371
Medium
6,352
https://leetcode.com/problems/super-pow/discuss/2747913/Python3-Solution
class Solution: def superPow(self, a: int, b: List[int]) -> int: b = int("".join(map(str, b))) return pow(a,b,1337)
super-pow
Python3 Solution
vivekrajyaguru
0
10
super pow
372
0.371
Medium
6,353
https://leetcode.com/problems/super-pow/discuss/2747577/recursion-solution
class Solution: def mypow(self, n, k): if k == 0: return 1 base = 1337 n %= base print(k) if(k % 2 == 0): sub = self.mypow(n, k//2) return (sub * sub) % base else: return (n * self.mypow(n, k-1)) % base def superPow(self, a: int, b: List[int]) -> int: base = 1337 if not b: return 1 last = b[-1] b = b[:-1] prev = self.mypow(a, last) latter = self.mypow(self.superPow(a, b) , 10) return (prev * latter) % base
super-pow
recursion solution
yhu415
0
2
super pow
372
0.371
Medium
6,354
https://leetcode.com/problems/super-pow/discuss/2652806/Easy-loop-finding-with-explanation
class Solution: def superPow(self, a: int, b: List[int]) -> int: loop = [] loopset = set() cur = a % 1337 while cur not in loopset: loopset.add(cur) loop.append(cur) cur *= a cur %= 1337 loop_len = len(loop) print(loop_len, loop) p = 0 cur = 0 while p<len(b): cur *= 10 cur += b[p] cur %= loop_len p+=1 return loop[cur-1]
super-pow
Easy loop finding with explanation
FrankYJY
0
6
super pow
372
0.371
Medium
6,355
https://leetcode.com/problems/super-pow/discuss/2635593/Python-1-Liner-easy-solution
class Solution: def superPow(self, a: int, b: List[int]) -> int: return pow(a, int(''.join([str(i) for i in b])), 1337)
super-pow
Python 1 Liner easy solution
code_snow
0
20
super pow
372
0.371
Medium
6,356
https://leetcode.com/problems/super-pow/discuss/2053437/Python-simple-oneliner
class Solution: def superPow(self, a: int, b: List[int]) -> int: return pow(a,int(''.join(map(str,b))),mod=1337)
super-pow
Python simple oneliner
StikS32
0
242
super pow
372
0.371
Medium
6,357
https://leetcode.com/problems/super-pow/discuss/1893630/Python3-pow-Solution-or-faster-than-77-or-less-than-33
class Solution: def superPow(self, a: int, b: List[int]) -> int: if a == 1: return 1 else: return pow(a, int("".join(map(lambda s:str(s),b))),1337)
super-pow
Python3 pow Solution | faster than 77% | less than 33%
khRay13
0
177
super pow
372
0.371
Medium
6,358
https://leetcode.com/problems/super-pow/discuss/1876654/Python-solution-using-strings-faster-than-66-memory-less-than-94
class Solution: def superPow(self, a: int, b: List[int]) -> int: if a == 1 or a == 0: return 1 b_num = int(''.join([str(x) for x in b])) return pow(a, b_num, 1337)
super-pow
Python solution using strings faster than 66%, memory less than 94%
alishak1999
0
145
super pow
372
0.371
Medium
6,359
https://leetcode.com/problems/super-pow/discuss/1806330/Easy-to-understand-(faster-than-43.71-of-Python3)
class Solution: def modpow(self, a: int, b: int, m: int) -> int: """ Compute a^b mod m with fast exponentiation""" if b == 0: return 1 r = self.modpow(a, b//2, m) if b % 2 == 0: return (r * r) % m else: return (a * r * r) % m def superPow(self, a: int, b: List[int]) -> int: m = 1337 r = 1 # this will contain the final result base = a # this is the starting base for e in b[::-1]: # note that I will traverse the list backward r = (r * self.modpow(base, e, m)) % m base = self.modpow(base, 10, m) # updating base if base == 1: # this trick will save computation: if you find a base==1 no need to compute more return r return r
super-pow
Easy to understand (faster than 43.71% of Python3)
pierluigif
0
175
super pow
372
0.371
Medium
6,360
https://leetcode.com/problems/super-pow/discuss/813810/Python3-leveraging-on-pow()
class Solution: def superPow(self, a: int, b: List[int]) -> int: ans = 1 for bb in b: ans = pow(ans, 10, 1337) * pow(a, bb, 1337) return ans % 1337
super-pow
[Python3] leveraging on pow()
ye15
0
290
super pow
372
0.371
Medium
6,361
https://leetcode.com/problems/super-pow/discuss/1214436/using-modular-expansion
class Solution: def superPow(self, a: int, b: List[int]) -> int: b=int(''.join(map(str,b))) res = 1 a = a % 1337 if (a == 0) : return 0 while (b>0) : if ((b &amp; 1) == 1): res=(res*a)%1337 b=b>>1 a=(a*a)%1337 return res
super-pow
using modular expansion
janhaviborde23
-3
179
super pow
372
0.371
Medium
6,362
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/1701122/Python-Simple-heap-solution-explained
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: hq = [] heapq.heapify(hq) # add all the pairs that we can form with # all the (first k) items in nums1 with the first # item in nums2 for i in range(min(len(nums1), k)): heapq.heappush(hq, (nums1[i]+nums2[0], nums1[i], nums2[0], 0)) # since the smallest pair will # be the first element from both nums1 and nums2. We'll # start with that and then subsequently, we'll pop it out # from the heap and also insert the pair of the current # element from nums1 with the next nums2 element out = [] while k > 0 and hq: _, n1, n2, idx = heapq.heappop(hq) out.append((n1, n2)) if idx + 1 < len(nums2): # the heap will ensure that the smallest element # based on the sum will remain on top and the # next iteration will give us the pair we require heapq.heappush(hq, (n1+nums2[idx+1], n1, nums2[idx+1], idx+1)) k -= 1 return out
find-k-pairs-with-smallest-sums
[Python] Simple heap solution explained
buccatini
11
1,400
find k pairs with smallest sums
373
0.383
Medium
6,363
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/1103290/Python-7-line-Simple-Heap-36-ms
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: if not nums2 or not nums1: return [] heap = [] heapq.heapify(heap) for i, num1 in enumerate(nums1[:k]): for num2 in nums2[:k//(i+1)]: heapq.heappush(heap, [num1+num2, num1, num2]) return [x[1:] for x in heapq.nsmallest(k, heap)]
find-k-pairs-with-smallest-sums
[Python] 7-line Simple Heap, 36 ms
cloverpku
6
1,100
find k pairs with smallest sums
373
0.383
Medium
6,364
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/492422/Python-Min-Heap
class Solution: def kSmallestPairs(self, nums1, nums2, k): if not nums1 or not nums2 or not k: return [] i = j = 0 minHeap = [] for _ in range(k): if i < len(nums1) and j < len(nums2): if nums1[i] <= nums2[j]: for x in nums2[j:]: heapq.heappush(minHeap, (nums1[i], x)) i += 1 else: for x in nums1[i:]: heapq.heappush(minHeap, (x, nums2[j])) j += 1 return heapq.nsmallest(k, minHeap, key = sum)
find-k-pairs-with-smallest-sums
Python - Min Heap
mmbhatk
4
1,700
find k pairs with smallest sums
373
0.383
Medium
6,365
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/1058117/Python-or-Heap-Push-Heap-Pop
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: result, min_heap = [], [] for i in nums1: for j in nums2: heapq.heappush(min_heap, (i+j, i, j)) for _ in range(k): if not min_heap: break result.append(heapq.heappop(min_heap)[1:]) return result
find-k-pairs-with-smallest-sums
Python | Heap Push Heap Pop
dev-josh
2
426
find k pairs with smallest sums
373
0.383
Medium
6,366
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/658086/Python3-heapq.merge-generator-Find-K-Pairs-with-Smallest-Sums
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: def generator(n1): for n2 in nums2: yield [n1+n2, n1, n2] merged = heapq.merge(*map(generator, nums1)) return [p[1:] for p in itertools.islice(merged, k) if p]
find-k-pairs-with-smallest-sums
Python3 heapq.merge generator - Find K Pairs with Smallest Sums
r0bertz
2
478
find k pairs with smallest sums
373
0.383
Medium
6,367
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/811844/Python3-via-heap
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: if not nums1 or not nums2: return [] # edge case hp = [(nums1[0] + nums2[j], 0, j) for j in range(len(nums2))] # min heap of size n heapify(hp) ans = [] while k and hp: k -= 1 _, i, j = heappop(hp) ans.append([nums1[i], nums2[j]]) if i+1 < len(nums1): heappush(hp, (nums1[i+1] + nums2[j], i+1, j)) return ans
find-k-pairs-with-smallest-sums
[Python3] via heap
ye15
1
222
find k pairs with smallest sums
373
0.383
Medium
6,368
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/2816924/Modified-BFS-by-selectively-branching-from-the-lowest-sum-nodes
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: ans = [] candidates = [(0,0)] # first pair is always give the smallest sum seen = set(['0,0']) # do not visit the indexes we have seen while len(candidates) > 0: listOfChosenNumber = self.getLowestN(candidates, nums1, nums2) # we choose the smallest n out of all the possible candidates for num in listOfChosenNumber[0]: if len(ans) == k: ans = [[nums1[x[0]],nums2[x[1]]] for x in ans] return ans else: ans.append(num) # we get the possible choices of the pairs we chose &amp; add it the pairs we didn't choose # for the next iteration listOfChosenNumber[1].extend(self.getChoices(listOfChosenNumber[0], len(nums1), len(nums2), seen)) candidates = listOfChosenNumber[1] ans = [[nums1[x[0]],nums2[x[1]]] for x in ans] # convert the index into values return ans def getChoices(self,listOfChosenNumber, num1Len, num2Len, seen): choices = [] for c in listOfChosenNumber: addNum1 = str(c[0]+1) + ',' + str(c[1]) addNum2 = str(c[0]) + ',' + str(c[1]+1) # we increment nums1 in the pair if c[0]+1 < num1Len and addNum1 not in seen: choices.append((c[0]+1, c[1])) seen.add(addNum1) # we increment nums2 in the pair if c[1]+1 < num2Len and addNum2 not in seen: choices.append((c[0], c[1]+1)) seen.add(addNum2) return choices def getLowestN(self,candidates,nums1,nums2): # getting the n smallest sum of the list of candidates selected = [] notSelected = [] minSum = None for pair in candidates: numSum = nums1[pair[0]]+nums2[pair[1]] if minSum == None: minSum = numSum selected.append(pair) elif numSum < minSum: notSelected.extend(selected) selected = [pair] minSum = numSum elif numSum > minSum: notSelected.append(pair) else: selected.append(pair) return [selected,notSelected]
find-k-pairs-with-smallest-sums
Modified BFS by selectively branching from the lowest sum nodes
yellowduckyugly
0
1
find k pairs with smallest sums
373
0.383
Medium
6,369
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/2800118/Python-or-SImple-or-Max-Heap
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: max_heap, result = [], [] for i in range(min(k, len(nums1))): for j in range(min(k, len(nums2))): sum_val = nums1[i] + nums2[j] if len(max_heap) < k: heappush(max_heap, (-sum_val, i, j)) else: if sum_val > -max_heap[0][0]: break else: heappushpop(max_heap, (-sum_val, i, j)) for _, i, j in max_heap: result.append([nums1[i], nums2[j]]) return result
find-k-pairs-with-smallest-sums
Python | SImple | Max Heap
david-cobbina
0
7
find k pairs with smallest sums
373
0.383
Medium
6,370
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/2661685/Heap-or-Python-or-O(klogn)
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: flag = (n := len(nums1)) > (m := len(nums2)) if flag: n, m, nums1, nums2 = m, n, nums2, nums1 heap = [] for i in range(min(k, n)): heappush(heap, (nums1[i] + nums2[0], i, 0)) ans = [] while heap and len(ans) < k: s, i, j = heappop(heap) ans.append([nums1[i], nums2[j]] if not flag else [nums2[j], nums1[i]]) if j + 1 < min(k, m): heappush(heap, (nums1[i] + nums2[j + 1], i, j + 1)) return ans
find-k-pairs-with-smallest-sums
Heap | Python | O(klogn)
Kiyomi_
0
52
find k pairs with smallest sums
373
0.383
Medium
6,371
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/2643855/Python-Solution-using-Heap
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: ans = [] heapq.heapify(ans) second = (min(k, len(nums2))) first = min(k, len(nums1)) for i in nums1[:first+1]: for j in nums2[:second + 1]: if len(ans) < k: heapq.heappush(ans, [-(i + j), [i,j]]) else: if (i + j) > -ans[0][0]: break heapq.heappush(ans, [-(i + j), [i,j]]) heapq.heappop(ans) return [j for i, j in ans[::-1]]
find-k-pairs-with-smallest-sums
Python Solution using Heap
vijay_2022
0
19
find k pairs with smallest sums
373
0.383
Medium
6,372
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/2348052/simple-python-heap
class Solution: def kSmallestPairs(self, nums1, nums2, k): res = [] if not nums1 or not nums2 or not k: return res heap = [] visited = set() heapq.heappush(heap, (nums1[0] + nums2[0], 0, 0)) visited.add((0, 0)) while len(res) < k and heap: _, i, j = heapq.heappop(heap) res.append([nums1[i], nums2[j]]) if i + 1 < len(nums1) and (i + 1, j) not in visited: heapq.heappush(heap, (nums1[i + 1] + nums2[j], i + 1, j)) visited.add((i + 1, j)) if j + 1 < len(nums2) and (i, j + 1) not in visited: heapq.heappush(heap, (nums1[i] + nums2[j + 1], i, j + 1)) visited.add((i, j + 1)) return res
find-k-pairs-with-smallest-sums
simple python heap
gasohel336
0
151
find k pairs with smallest sums
373
0.383
Medium
6,373
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/1249301/Python-Space-optimsed-as-well-as-time-optimised-Solution(HEAP-DS)
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: min_heap=[] if(len(nums1)*len(nums2)<k): k=len(nums1)*len(nums2) for i in range(0,len(nums1)): for j in range(0,len(nums2)): s=nums1[i]+nums2[j] heappush(min_heap,(-s,nums1[i],nums2[j])) if(len(min_heap)>k): heappop(min_heap) j=0 ans=[] while j<k: res,a,b=heappop(min_heap) ans.append([a,b]) j+=1 return ans
find-k-pairs-with-smallest-sums
Python Space optimsed as well as time optimised Solution(HEAP DS)
Amans82702
-2
136
find k pairs with smallest sums
373
0.383
Medium
6,374
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/1249301/Python-Space-optimsed-as-well-as-time-optimised-Solution(HEAP-DS)
class Solution: def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: min_heap=[] if(len(nums1)*len(nums2)<k): k=len(nums1)*len(nums2) for i in range(0,len(nums1)): for j in range(0,len(nums2)): s=nums1[i]+nums2[j] heappush(min_heap,(s,nums1[i],nums2[j])) j=0 ans=[] while j<k: res,a,b=heappop(min_heap) ans.append([a,b]) j+=1 return ans
find-k-pairs-with-smallest-sums
Python Space optimsed as well as time optimised Solution(HEAP DS)
Amans82702
-2
136
find k pairs with smallest sums
373
0.383
Medium
6,375
https://leetcode.com/problems/find-k-pairs-with-smallest-sums/discuss/1319295/Easy-to-Understand-Python-Solution
class Solution: def kSmallestPairs(self, nums1, nums2, k: int): temp=[] res=[] tot=[] end=0 for i in nums1: for j in nums2: temp.append([i,j]) tot.append(sum([i,j])) for i,v in sorted(zip(temp,tot),key = lambda x: x[1]): if end<k: res.append(i) end+=1 return res
find-k-pairs-with-smallest-sums
Easy to Understand Python Solution
sangam92
-3
365
find k pairs with smallest sums
373
0.383
Medium
6,376
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2717871/DONT-TRY-THIS-CODE-or-ONE-LINE-PYTHON-CODE
class Solution: def guessNumber(self, n: int) -> int: return __pick__
guess-number-higher-or-lower
DONT TRY THIS CODE | ONE LINE PYTHON CODE
raghavdabra
9
441
guess number higher or lower
374
0.514
Easy
6,377
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2164730/Python3-extension-of-Binary-Search
class Solution: def guessNumber(self, n: int) -> int: low = 1 high = n while low<=high: mid = (low+high)//2 gussed = guess(mid) if gussed == 0: return mid if gussed<0: high = mid-1 else: low = mid+1 return low
guess-number-higher-or-lower
📌 Python3 extension of Binary Search
Dark_wolf_jss
5
145
guess number higher or lower
374
0.514
Easy
6,378
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2819214/Easy-Python-Solution
class Solution: def guessNumber(self, n: int) -> int: low = 1 high = n while low <= high: mid = (low + high)//2 res = guess(mid) if res == 0 : return mid elif res == -1: high = mid - 1 else: low = mid + 1
guess-number-higher-or-lower
Easy Python Solution
Vistrit
3
248
guess number higher or lower
374
0.514
Easy
6,379
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/1259722/Python3-dollarolution-(memory-99.87)
class Solution: def guessNumber(self, n: int) -> int: l, h = 1, n+1 while True: m = int((l+h)/2) if guess(m) == 0: return m elif guess(m) == -1: h = m else: l = m
guess-number-higher-or-lower
Python3 $olution (memory - 99.87%)
AakRay
2
334
guess number higher or lower
374
0.514
Easy
6,380
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/1241261/Python3-simple-solution-using-binary-search
class Solution: def guessNumber(self, n: int) -> int: l,h = 1,n mid = (l + h)//2 while True: if guess(mid) < 0: h = mid-1 mid = (l + h)//2 elif guess(mid) > 0: l = mid+1 mid = (l + h)//2 else: return mid
guess-number-higher-or-lower
Python3 simple solution using binary search
EklavyaJoshi
2
83
guess number higher or lower
374
0.514
Easy
6,381
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2820807/C%2B%2B-Python-Java-oror-Videos-of-Binary-Search-by-Experts-oror-Easy-to-Understand-oror-Binary-Search
class Solution: def guessNumber(self, n: int) -> int: low = 0 high = n while low<=high: mid = low+(high-low)//2 num = guess(mid) if num == 0: return mid elif num == -1: high = mid-1 else: low = mid+1
guess-number-higher-or-lower
C++, Python, Java || Videos of Binary-Search by Experts || Easy to Understand || Binary-Search
mataliaprajit
1
27
guess number higher or lower
374
0.514
Easy
6,382
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2819629/Python-Classic-Binary-Search-Problem-with-Explaination-or-99-Faster-or-Fastest-Solution
class Solution: def guessNumber(self, n: int) -> int: left, right = 0, n while left<=right: mid = (left+right)//2 num = guess(mid) if num == 0: return mid elif num == -1: right = mid-1 else: left = mid+1
guess-number-higher-or-lower
✔️ Python Classic Binary Search Problem with Explaination | 99% Faster | Fastest Solution 🔥
pniraj657
1
39
guess number higher or lower
374
0.514
Easy
6,383
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2819624/99.04-Faster-Python-binary-search-easiest-approach-O(log-N)
class Solution: def guessNumber(self, n: int) -> int: l = 1 r = n while l<=r: mid = l + (r-l)//2 result = guess(mid) if result == 0: return mid elif result == -1: r = mid -1 elif result == 1: l = mid + 1 return mid
guess-number-higher-or-lower
99.04% Faster Python binary search easiest approach O(log N)
mohitOnlyCodeLover
1
47
guess number higher or lower
374
0.514
Easy
6,384
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/1930437/Python-Binary-Search-or-Simple-and-Clean
class Solution: def guessNumber(self, n): l, r = 1, n while True: m = (l+r)//2 match guess(m): case 0: return m case 1: l = m+1 case -1: r = m-1
guess-number-higher-or-lower
Python - Binary Search | Simple and Clean
domthedeveloper
1
116
guess number higher or lower
374
0.514
Easy
6,385
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/1517044/Python3-binary-search
class Solution: def guessNumber(self, n: int) -> int: lo, hi = 1, n while lo <= hi: mid = lo + hi >> 1 val = guess(mid) if val == -1: hi = mid - 1 elif val == 0: return mid else: lo = mid + 1
guess-number-higher-or-lower
[Python3] binary search
ye15
1
58
guess number higher or lower
374
0.514
Easy
6,386
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/1378302/Easy-Fast-Memory-Efficient-Python-O(log-n)-Solution
class Solution: def guessNumber(self, n: int) -> int: l, h = 0, n+1 ret = guess(n) if ret == 0: return n while l < h: m = (h+l)//2 ret = guess(m) if ret == 0: return m elif ret == -1: h = m+1 elif ret == 1: l = m-1
guess-number-higher-or-lower
Easy, Fast, Memory Efficient Python O(log n) Solution
the_sky_high
1
179
guess number higher or lower
374
0.514
Easy
6,387
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2830091/Python-Solution-Binary-Search-Approach
class Solution: def guessNumber(self, n: int) -> int: #BASED ON BINARY SEARCH l=1 h=n while l<=h: mid=(l+h)//2 pick=guess(mid) if pick==0: return mid if pick==-1: h=mid elif pick==1: l=mid+1
guess-number-higher-or-lower
Python Solution - Binary Search Approach ✔
T1n1_B0x1
0
4
guess number higher or lower
374
0.514
Easy
6,388
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2827778/Python3%3A-Runtime%3A-98.96-Memory%3A-97.83
class Solution: def guessNumber(self, n: int) -> int: left = 1 right = n while left <= right: mid = (left + right) // 2 (res := guess(mid)) if res == 1: left = mid + 1 elif res == -1: right = mid - 1 else: return mid return -1
guess-number-higher-or-lower
Python3: Runtime: 98.96%, Memory: 97.83%
Leyonad
0
2
guess number higher or lower
374
0.514
Easy
6,389
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2821787/oror-C%2B%2B-and-PYTHON-oror-EASY-SOLUTION-oror-BEATS-100-oror-BINARY-SEARCH-oror
class Solution: def guessNumber(self, n: int) -> int: l = 1 r = n p = l + (r - l) / 2 g = guess(p) while(g != 0): r = p-1 if g == -1 else r l = p+1 if g == 1 else l p = l + (r - l) / 2 g = guess(p) return int(p)
guess-number-higher-or-lower
|| C++ & PYTHON || EASY SOLUTION || BEATS 100% || BINARY SEARCH ||
michalwesolowski
0
4
guess number higher or lower
374
0.514
Easy
6,390
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2821478/C%2B%2B.-oror-Binary-Search-Based-Solution
class Solution: def guessNumber(self, n: int) -> int: i, j = 1, n while i <= j: m = (i+j)//2 g = guess(m) if g > 0: i = m+1 elif g < 0: j = m-1 else: return m return -1
guess-number-higher-or-lower
C++. || Binary Search Based Solution
Anup_1999
0
5
guess number higher or lower
374
0.514
Easy
6,391
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2821389/Binary-search-solution
class Solution: def guessNumber(self, n: int) -> int: low = 1 high = n mid = n//2 g = guess(mid) while g!=0: if g==-1: high = mid - 1 elif g==1: low = mid + 1 mid = (low+high)//2 g = guess(mid) return mid
guess-number-higher-or-lower
Binary search solution
pratiklilhare
0
2
guess number higher or lower
374
0.514
Easy
6,392
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2821149/Solution-The-Best
class Solution: def guessNumber(self, j: int, i=1) -> int: while True: m = (i + j) >> 1 g = guess(m) if g == 0: return m g = (g + 1) >> 1 i += g * (m + 1 - i) j += (1 - g) * (m - 1 - j)
guess-number-higher-or-lower
Solution, The Best
Triquetra
0
4
guess number higher or lower
374
0.514
Easy
6,393
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2821131/Changing-guess-one-digit-at-a-time
class Solution: def guessNumber(self, n): number = 2147483648 exponent = 9 while True: if guess(number) == -1: number -= 10**exponent if guess(number) == 1: exponent -= 1 elif guess(number) == 1: number += 10**exponent if guess(number) == -1: exponent -= 1 else: return number
guess-number-higher-or-lower
Changing guess one digit at a time
OSTERIE
0
1
guess number higher or lower
374
0.514
Easy
6,394
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2821007/Python-or-Binary-Search-O(log-n)-Time-O(1)-Space
class Solution: def guessNumber(self, n: int, lo = 1) -> int: low, high = 1, n+1 while True: num = int((low+high)/2) attempt = guess(num) if attempt == -1: high = num elif attempt == 1: low = num else: return num
guess-number-higher-or-lower
Python | Binary Search O(log n) Time, O(1) Space
jessewalker2010
0
3
guess number higher or lower
374
0.514
Easy
6,395
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2820991/Python-solution-with-explanation
class Solution: def guessNumber(self, n: int) -> int: left = 1 right = n while(left<=right): mid = left + (right-left)//2 val = guess(mid) if(val==0): return mid elif(val==-1): right = mid-1 else: left = mid+1 return -1
guess-number-higher-or-lower
Python solution with explanation
yashkumarjha
0
2
guess number higher or lower
374
0.514
Easy
6,396
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2820865/Simple-Solution%3A-Recursive-Binary-Search
class Solution: def guessNumber(self, n: int, m=1) -> int: i = int((n+m)/2) g = guess(i) if not g: return i if g == 1: return self.guessNumber(n, i+1) return self.guessNumber(i-1, m)
guess-number-higher-or-lower
Simple Solution: Recursive Binary Search
Mencibi
0
2
guess number higher or lower
374
0.514
Easy
6,397
https://leetcode.com/problems/guess-number-higher-or-lower/discuss/2820143/Python-(Faster-than-97)-or-Binary-search
class Solution: def guessNumber(self, n: int) -> int: l, r = 1, n while l <= r: mid = (l + r) // 2 val = guess(mid) if val == -1: r = mid - 1 elif val == 1: l = mid + 1 elif val == 0: return mid
guess-number-higher-or-lower
Python (Faster than 97%) | Binary search
KevinJM17
0
2
guess number higher or lower
374
0.514
Easy
6,398
https://leetcode.com/problems/guess-number-higher-or-lower-ii/discuss/1510747/Python-DP-beat-97.52-in-time-99-in-memory-(with-explanation)
class Solution: def getMoneyAmount(self, n: int) -> int: if n == 1: return 1 starting_index = 1 if n % 2 == 0 else 2 selected_nums = [i for i in range(starting_index, n, 2)] selected_nums_length = len(selected_nums) dp = [[0] * selected_nums_length for _ in range(selected_nums_length)] for i in range(selected_nums_length): dp[i][i] = selected_nums[i] for length in range(2, selected_nums_length + 1): for i in range(selected_nums_length - length + 1): j = i + length - 1 dp[i][j] = float("inf") for k in range(i, j + 1): dp_left = dp[i][k - 1] if k != 0 else 0 dp_right = dp[k + 1][j] if k != j else 0 dp[i][j] = min(dp[i][j], selected_nums[k] + max(dp_left, dp_right)) return dp[0][-1]
guess-number-higher-or-lower-ii
[Python] DP, beat 97.52% in time, 99% in memory (with explanation)
wingskh
31
1,100
guess number higher or lower ii
375
0.465
Medium
6,399