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/power-of-four/discuss/2667207/Python-solution-based-on-recursion
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 0: return False if n == 1 or n == 4: return True return self.isPowerOfFour(n/4)
power-of-four
Python solution based on recursion
MPoinelli
0
4
power of four
342
0.458
Easy
5,900
https://leetcode.com/problems/power-of-four/discuss/2664980/Python-Easy-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if n < 1: return False if n == 1: return True return self.isPowerOfFour(n / 4)
power-of-four
Python Easy Solution
kruzhilkin
0
2
power of four
342
0.458
Easy
5,901
https://leetcode.com/problems/power-of-four/discuss/2662054/Python-solution-without-loopsrecursion
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 1: return True bit = "{0:b}".format(n) if bit[-1] == "1": return False if bit[0] == "1" and "1" not in bit[1:] and len(bit) % 2 == 1: return True
power-of-four
Python solution without loops/recursion
maomao1010
0
7
power of four
342
0.458
Easy
5,902
https://leetcode.com/problems/power-of-four/discuss/2614089/Python-Solution-Interview-Approach
class Solution: def isPowerOfFour(self, n: int) -> bool: if n<=0: return False while n>1: if n%4!=0: return False n//=4 return True
power-of-four
[Python Solution] Interview Approach
utsa_gupta
0
9
power of four
342
0.458
Easy
5,903
https://leetcode.com/problems/power-of-four/discuss/2559569/Python-(Simple-Solution-and-Beginner-Friendly)
class Solution: def isPowerOfFour(self, n: int) -> bool: while n>1: n = n/4 return n == 1
power-of-four
Python (Simple Solution and Beginner-Friendly)
vishvavariya
0
39
power of four
342
0.458
Easy
5,904
https://leetcode.com/problems/power-of-four/discuss/2470113/Python3-bitwise-operator
class Solution(object): def isPowerOfFour(self, num): """ :type n: int :rtype: bool """ result = 0 if num <= 0: return False if num &amp; (num - 1) != 0: return False for i in range(0, 31): if i % 2 == 0: result += 1 << i return (num &amp; result) == num sol = Solution() print(sol.isPowerOfFour(15))
power-of-four
Python3 - bitwise operator
user2354hl
0
4
power of four
342
0.458
Easy
5,905
https://leetcode.com/problems/power-of-four/discuss/2466684/Unique-Python-One-Liners
class Solution: def isPowerOfFour(self, n: int) -> bool: return n > 0 \ and f'{n:032b}'.count('1') == 1 \ and f'{n:032b}'.find('1') &amp; 1
power-of-four
Unique Python One-Liners
timberg
0
16
power of four
342
0.458
Easy
5,906
https://leetcode.com/problems/power-of-four/discuss/2466530/Python-Simple-Python-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: power = 0 number = 4 while True: num = number**power if num == n: return True if num > n: return False else: power = power + 1 return False
power-of-four
[ Python ] βœ…βœ… Simple Python Solution πŸ₯³βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
0
22
power of four
342
0.458
Easy
5,907
https://leetcode.com/problems/power-of-four/discuss/2464578/Python-Short-Faster-Solution-log-base-4-oror-Documented
class Solution: def isPowerOfFour(self, n: int) -> bool: if n > 0: power = int(math.log(n,4)) # find power using log base 4 return 4 ** power == n # if 4^power is same as n, return True, else False return False
power-of-four
[Python] Short, Faster Solution - log base 4 || Documented
Buntynara
0
5
power of four
342
0.458
Easy
5,908
https://leetcode.com/problems/power-of-four/discuss/2463692/Python-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: result = 1 while n > result: result *= 4 return result == n
power-of-four
Python Solution
hgalytoby
0
14
power of four
342
0.458
Easy
5,909
https://leetcode.com/problems/power-of-four/discuss/2462341/Python-Fast-and-trivial-solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if n <= 0: return False while n > 1: n, m = divmod(n, 4) if m > 0: return False return True
power-of-four
[Python] Fast and trivial solution
RegInt
0
15
power of four
342
0.458
Easy
5,910
https://leetcode.com/problems/power-of-four/discuss/2462292/Three-Solution-using-recursion-iteration-and-Bit-Manipulation
class Solution: def isPowerOfFour(self, n: int) -> bool: # Solution 1 using recursion while n % 4 == 0 and n > 0: return self.isPowerOfFour(n/4) return n == 1 # Solution 2 iteration if n == 1: return True if n % 4: return False while n > 1: if n % 4: return False n //= 4 return n == 1 # Solution 3 using bit manipulation ''' Once we write numbers in it's binary representation, from there we can observe:=> i. 000001 , power of 2 and 4 ii. 000010, power of only 2 iii. 000100 , power of 2 and 4 iv. 001000, power of only 2 v. 010000 , power of 2 and 4 vi. 100000, power of only 2 We can see if the set bit is at an odd position and is a power of 2, it's also power of 4. ''' return n.bit_length() &amp; 1 and not(n &amp; (n-1))
power-of-four
Three Solution using recursion, iteration and Bit-Manipulation
__Asrar
0
25
power of four
342
0.458
Easy
5,911
https://leetcode.com/problems/power-of-four/discuss/2462065/30ms-or-fast-and-easy-approach
class Solution: def isPowerOfFour(self, n: int) -> bool: if n>0: x=math.log(n,4) return math.floor(x)==math.ceil(x) return False
power-of-four
30ms | fast and easy approach
ayushigupta2409
0
16
power of four
342
0.458
Easy
5,912
https://leetcode.com/problems/power-of-four/discuss/2461942/python3-with-binary-bit-manipulation
class Solution: def isPowerOfFour(self, n: int) -> bool: if n<=0: return False n_bin = bin(n)[2:] return True if (len(n_bin) % 2 == 1) and (sum(map(int,n_bin)) == 1) else False
power-of-four
python3, with binary bit manipulation
pjy953
0
3
power of four
342
0.458
Easy
5,913
https://leetcode.com/problems/power-of-four/discuss/2461898/Python3-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: i = 0 curr = 0 while curr <= n: curr = 4 ** i if curr == n: return True i += 1 return False
power-of-four
Python3 Solution
creativerahuly
0
19
power of four
342
0.458
Easy
5,914
https://leetcode.com/problems/power-of-four/discuss/2461708/Python-Recursion-O(logn)-time-O(logn)-space.
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 1: return True if n < 1: return False return self.isPowerOfFour(n/4)
power-of-four
Python Recursion, O(logn) time, O(logn) space.
OsamaRakanAlMraikhat
0
17
power of four
342
0.458
Easy
5,915
https://leetcode.com/problems/power-of-four/discuss/2461452/Simple-Python-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: while n>1: n = n/4 if n == 1: return True return False
power-of-four
Simple Python Solution
saiavunoori4187
0
15
power of four
342
0.458
Easy
5,916
https://leetcode.com/problems/power-of-four/discuss/2461394/Python-Simple-and-Easy-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 1: return True while n > 1: if n%4 == 0: n = n // 4 else: return False if n == 1: return True return False
power-of-four
Python - Simple and Easy Solution
dayaniravi123
0
7
power of four
342
0.458
Easy
5,917
https://leetcode.com/problems/power-of-four/discuss/2461372/Easy-Python-Solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if n<1: return False elif n==1: return True else: while n!=1: if n%4!=0: return False else: n=n//4 return True
power-of-four
Easy Python Solution
a_dityamishra
0
13
power of four
342
0.458
Easy
5,918
https://leetcode.com/problems/power-of-four/discuss/2461309/Python-O(1)-oneliner-easy-to-understand-(no-loops-or-bitwise-math)
class Solution: def isPowerOfFour(self, num): return num == 4**(num.bit_length()//2)
power-of-four
[Python] O(1) oneliner - easy to understand (no loops or bitwise math)
user5285r
0
7
power of four
342
0.458
Easy
5,919
https://leetcode.com/problems/power-of-four/discuss/2461196/Python-bit-check
class Solution: def isPowerOfFour(self, n: int) -> bool: return n > 0 and n &amp; (n - 1) == 0 and n &amp; 0x55555555
power-of-four
Python, bit check
blue_sky5
0
20
power of four
342
0.458
Easy
5,920
https://leetcode.com/problems/power-of-four/discuss/2446455/python3-using-a-while-loop-faster-than-86
class Solution: def isPowerOfFour(self, n: int) -> bool: while n%4==0<n: n/=4 return n==1
power-of-four
[python3] using a while loop, faster than 86%
hhlinwork
0
8
power of four
342
0.458
Easy
5,921
https://leetcode.com/problems/power-of-four/discuss/2366691/Simple-fast-Python-solution-or-Easy-to-Understand-or-73.70-faster
class Solution: def isPowerOfFour(self, n: int) -> bool: x = 0 y = pow(4, x) while y<n: x+=1 y = pow(4, x) if (n - y) == 0: return True else: return False
power-of-four
Simple fast Python solution | Easy to Understand | 73.70% faster
harishmanjunatheswaran
0
40
power of four
342
0.458
Easy
5,922
https://leetcode.com/problems/power-of-four/discuss/2120598/Python3-easy-understanding-solution
class Solution: def isPowerOfFour(self, n: int) -> bool: if n == 0: return False pow_value = 1 while pow_value < n: pow_value *=4 return pow_value == n
power-of-four
Python3 - easy understanding solution
thanhinterpol
0
23
power of four
342
0.458
Easy
5,923
https://leetcode.com/problems/power-of-four/discuss/1936644/Python-One-Liner-or-Log-or-Math
class Solution: def isPowerOfFour(self, n): return n>0 and log(n,4).is_integer()
power-of-four
Python - One-Liner | Log | Math
domthedeveloper
0
44
power of four
342
0.458
Easy
5,924
https://leetcode.com/problems/power-of-four/discuss/1815199/Easy-to-understand-trick
class Solution: def isPowerOfFour(self, n: int) -> bool: arr=[] for i in range(100): arr.append(4**i) if n in arr: return True else: return False
power-of-four
Easy to understand trick
prashantdahiya711
0
76
power of four
342
0.458
Easy
5,925
https://leetcode.com/problems/integer-break/discuss/2830343/O(1)-oror-TC1-10-line-code
class Solution: def integerBreak(self, n: int) -> int: if(n<=3): return n-1 n3=n//3 r3=n%3 if(r3==0): return 3**n3 if(r3==1): r3=4 n3-=1 return r3*(3**n3)
integer-break
O(1) || TC=1 10 line code
droj
7
71
integer break
343
0.554
Medium
5,926
https://leetcode.com/problems/integer-break/discuss/2180665/Python3-DP-top-down-approach-faster-than-96
class Solution: result = {1:1,2:1,3:2,4:4,5:6,6:9,7:12,8:18,9:27,10:36} def integerBreak(self, n: int) -> int: try: return self.result[n] except: x = float("-inf") for i in range(1,n): j = n-1 while j>0: if i+j==n: k = self.integerBreak(i)*self.integerBreak(j) x = max(x,k) j-=1 self.result[n] = x return self.result[n]
integer-break
πŸ“Œ Python3 DP top down approach faster than 96%
Dark_wolf_jss
4
76
integer break
343
0.554
Medium
5,927
https://leetcode.com/problems/integer-break/discuss/1699371/Python-solution-O(1)-method-Easy-to-understand-with-explanation
class Solution: def integerBreak(self, n: int) -> int: if n == 2 or n == 3: return n - 1 # must break into 1 &amp; (n-1) num_3 = n // 3 # break the number into (3+3+3+...) + remainder if n%3 == 0: return 3**num_3 # (3+3+3+...) if n%3 == 2: return (3**num_3) * 2 # (3+3+3+...) + 2 if n%3 == 1: return 3**(num_3-1) * 4 # (3+3+3+...) + 1 --> (3+3+...) + 3 + 1 -- > (3+3+...) + 4
integer-break
Python solution -- O(1) method Easy to understand with explanation
byroncharly3
4
130
integer break
343
0.554
Medium
5,928
https://leetcode.com/problems/integer-break/discuss/479923/Python-O(n)-Dynamic-Programming-with-Memoization
class Solution: def integerBreak(self, n: int) -> int: memo = {} def intBreak(n): if n in memo: return memo[n] if n<2: memo[n] = 0 return 0 if n==2: memo[2] = 2 return 1 maxval = 0 for i in range(1,n//2+1): maxval = max(maxval,i*intBreak(n-i),i*(n-i)) memo[n] = maxval return maxval ans = intBreak(n) return ans
integer-break
Python O(n) Dynamic Programming with Memoization
user8307e
3
692
integer break
343
0.554
Medium
5,929
https://leetcode.com/problems/integer-break/discuss/2407006/Python3-or-Solved-using-Bottom-Up-DP-%2B-Tabulation
class Solution: #Time-Complexity: O(n^2) #Space-Complexity: O(n) def integerBreak(self, n: int) -> int: #we know we can reduce n as # n # / \ # 1 n-1 # / \ # 1 n-2 # ... #Basically, we can keep reducing n like this in this tree structure above! #This is the pattern I recognized! I recognized for given n, there are #potential sums of (1, n-1), (2, n-2), (3, n-3), ..., (n//2, n//2)! #For each pair, I can compare the direct number with the max product decomposition #and take the max of two! #Reason for comparison: for each of the sum factor of given n, either leave it #undecomposed or decompose it into further sum factors if the product of sum #factors produce ultimately a number that exceeds the orignal sum factor! This way #I am maximing product contribution for each and every sum factor! #For example, for 5, we decompose it into 2 and 3, since 2*3 > 5, so it will #maximize our product further! #However, for 3, we don't decompose since we can maximally decompose to #1 and 2 but 1*2 < 3! #Do that for both numbers of each pair and take the product! #Whatever is largest across the pairs will be answer for given input n! dp = [-1] * (n+1) #add dp-base! dp[1] = 1 #this problem has only one state parameter: the given number to start decomposing #from! #iterate through each subproblem or state! #Bottom-Up for i in range(2, n+1, 1): upper_bound = (i // 2) + 1 #iterate through all possible pairs! for j in range(1, upper_bound, 1): #current pair (j, i-j), which we probably already solved its subproblems! first = max(j, dp[j]) second = max(i-j, dp[i-j]) #get product for current pair! sub_ans = first * second #compare current pair's product against built up answer maximum! dp[i] = max(dp[i], sub_ans) #then, once we are done, we can return dp[n]! return dp[n]
integer-break
Python3 | Solved using Bottom-Up DP + Tabulation
JOON1234
2
29
integer break
343
0.554
Medium
5,930
https://leetcode.com/problems/integer-break/discuss/1136369/O(n)-DP-Python3-bites-97
class Solution: def integerBreak(self, n: int) -> int: dp = [None, None, 1, 2, 4, 6, 9] for i in range(7, n+1): dp.append(dp[i-3] * 3) return dp[n]
integer-break
O(n) - DP - Python3 bites 97%
ShSaktagan
2
167
integer break
343
0.554
Medium
5,931
https://leetcode.com/problems/integer-break/discuss/1778956/Python-3-Solution-Using-power-of-3
class Solution: def integerBreak(self, n: int) -> int: if n==2: return 1 if n==3: return 2 if n==4: return 4 x=n%3 a=n//3 if x==0: return 3**a if x==1: return 3**(a-1)*4 return 3**a*2
integer-break
[Python 3 Solution Using power of 3]
jiteshbhansali
1
81
integer break
343
0.554
Medium
5,932
https://leetcode.com/problems/integer-break/discuss/1148577/Python-math-solutiion-O(1)
class Solution: def integerBreak(self, n: int) -> int: q, r = divmod(n, 3) if r == 0: return 2 if n == 3 else 3**q elif r == 1: return 1 if n == 1 else 4*3**(q-1) else: # r == 2 return 1 if n == 2 else 2*3**q
integer-break
Python, math solutiion O(1)
blue_sky5
1
136
integer break
343
0.554
Medium
5,933
https://leetcode.com/problems/integer-break/discuss/1007487/Python3Golang-Solution-with-using-dp
class Solution: def integerBreak(self, n: int) -> int: dp = [0] * (n + 1) dp[1] = 1 dp[2] = 1 for i in range(3, n + 1): for j in range(1, i // 2 + 1): dp[i] = max(dp[i], max((i - j), dp[i - j]) * j) return dp[-1]
integer-break
[Python3/Golang] Solution with using dp
maosipov11
1
55
integer break
343
0.554
Medium
5,934
https://leetcode.com/problems/integer-break/discuss/2842149/Python-Solution-with-O(1)-time-and-space-complexity
class Solution: def integerBreak(self, n: int) -> int: if n == 2: return 1 if n == 3: return 2 remainder = n%3 numberOfThree = n//3 if remainder == 0: return 3 ** numberOfThree if remainder == 1: return (3 ** (numberOfThree-1)) * 4 else: return (3 ** numberOfThree) * 2
integer-break
Python Solution with O(1) time and space complexity
jxlau
0
3
integer break
343
0.554
Medium
5,935
https://leetcode.com/problems/integer-break/discuss/2817001/intuitive-sub-optimal-solution-from-someone-who-is-terrible-at-math
class Solution: def integerBreak(self, n: int) -> int: # to max product, try to break into k similar-sized pieces def build_pieces( k ): if k < 2: return None # k >= 2 div, mod = divmod(n,k) pieces = [div] * k while mod >0: pieces[mod] += 1 # spread the residule evenly mod -= 1 return pieces def product( nums ): ans = 1 for num in nums: ans *= num return ans ans = 0 for k in range(2, n+1): pieces = build_pieces( k ) # for debug purpose ans = max(ans, product( pieces ) ) return ans
integer-break
intuitive, sub-optimal solution from someone who is terrible at math
SkinheadBob
0
2
integer break
343
0.554
Medium
5,936
https://leetcode.com/problems/integer-break/discuss/2776999/Simple-Python-Solution-or-Math
class Solution: def integerBreak(self, n: int) -> int: three = 0 if n<4: return n-1 while n>4: n-=3 three+=1 return 3**three*n
integer-break
Simple Python Solution | Math
RajatGanguly
0
6
integer break
343
0.554
Medium
5,937
https://leetcode.com/problems/integer-break/discuss/2740476/Python-solution
class Solution: def integerBreak(self, n: int) -> int: if n == 2: return 1 if n == 3: return 2 if n % 3 == 0: return pow(3, n//3) else: r3 = 0 while n > 4: r3 += 1 n -= 3 return pow(3, r3) * n
integer-break
Python solution
maomao1010
0
5
integer break
343
0.554
Medium
5,938
https://leetcode.com/problems/integer-break/discuss/2733624/Very-short-dynamic-programming-solution-beat-99
class Solution: def integerBreak(self, n: int) -> int: dp = [0] + [1] * n for i in range(3,n+1): dp[i] = max(max(i-2,dp[i-2]) * 2, max(i-3,dp[i-3]) * 3) return dp[n]
integer-break
Very short dynamic programming solution, beat 99%
zhaoqiang
0
7
integer break
343
0.554
Medium
5,939
https://leetcode.com/problems/integer-break/discuss/2691100/python-working-solution-or-dp
class Solution: def integerBreak(self, n: int) -> int: dp = {} def dfs(n): if n == 1: return 1 if n in dp: return dp[n] ans = float('-inf') for l in range(1,n): r = n-l left,right = dfs(l),dfs(r) ans = max(ans ,left * r , l * right ,r*l) dp[n] = ans return ans return dfs(n)
integer-break
python working solution | dp
Sayyad-Abdul-Latif
0
10
integer break
343
0.554
Medium
5,940
https://leetcode.com/problems/integer-break/discuss/2682681/Easy-to-Understand.-Not-the-best-solution-I-feel.-Faster-than-50
class Solution: def integerBreak(self, n: int) -> int: def dfs(a): if a == 1: return 1 max_res = 1 for right in range(1, a): left = a - right max_res = max(max_res, left * dfs(right), left * right) return max_res return dfs(n)
integer-break
Easy to Understand. Not the best solution I feel. Faster than 50%
devanshsolani30
0
18
integer break
343
0.554
Medium
5,941
https://leetcode.com/problems/integer-break/discuss/2662453/python-easy-dp-solution
class Solution: def integerBreak(self, n: int) -> int: dp = [0]*(n+1) dp[1] = 1 for i in range(2,n+1): for j in range(1,i): dp[i] =max(dp[i],j*(i-j),j*dp[i-j]) return dp[n]
integer-break
python easy dp solution
Akash_chavan
0
13
integer break
343
0.554
Medium
5,942
https://leetcode.com/problems/integer-break/discuss/2657315/Simple-1D-Bottom-Up-DP
class Solution: def integerBreak(self, n: int) -> int: dp = [0] * (n+1) for i in range(1, n+1): for j in range(1, i): dp[i] = max(dp[i], j * (i-j), j * dp[i-j]) return dp[-1]
integer-break
Simple 1D Bottom-Up DP
jonathanbrophy47
0
6
integer break
343
0.554
Medium
5,943
https://leetcode.com/problems/integer-break/discuss/2296772/Python3-or-Memoization-or-Recursion
class Solution: def integerBreak(self, n: int) -> int: @lru_cache(None) def recursive(n): if n==1: return 1 max_prod=0 for index in range(1,n): max_prod=max(max_prod,index*recursive(n-index)) max_prod=max(max_prod,index*(n-index)) return max_prod return recursive(n)
integer-break
Python3 | Memoization | Recursion
Sefinehtesfa34
0
19
integer break
343
0.554
Medium
5,944
https://leetcode.com/problems/integer-break/discuss/1642913/Liner-solution-90-speed
class Solution: def integerBreak(self, n: int) -> int: max_prod = 1 for k in range(2, n // 2 + 2): num, rem = divmod(n, k) max_prod = max(max_prod, pow(num, k - rem) * pow(num + 1, rem)) return max_prod
integer-break
Liner solution, 90% speed
EvgenySH
0
92
integer break
343
0.554
Medium
5,945
https://leetcode.com/problems/integer-break/discuss/1593305/Python-Straightforward-Solution-or-Pure-Easy-Mathematics
class Solution: def integerBreak(self, n: int) -> int: if n <= 3: return n-1 three = n//3 rem = n % 3 if rem == 0: return 3**three elif rem == 1: return (3**(three-1))*4 else: return (3**three)*2 # 6 = 3x3 = 9 # 7 = 3x4 = 12 # 8 = 3x3x2 = 18 # 9 = 3x3x3 = 27 # 10 = 3x3x4 = 36 # 54 = 3 ^ 18 = 387420489 # 55 = (3 ^ 17)*4 = 516560652 # 56 = (3 ^ 18)*2 = 774840978
integer-break
Python Straightforward Solution | Pure Easy Mathematics
leet_satyam
0
94
integer break
343
0.554
Medium
5,946
https://leetcode.com/problems/integer-break/discuss/1498409/we-only-need-2-and-3.
class Solution: def integerBreak(self, n: int) -> int: if n == 2: return 1 if n == 3: return 2 num3 = n//3 maxv = 1 for i in range(num3+1): res = n - 3*i if res %2 == 0: num2 = res//2 maxv = max(maxv, 2**num2*3**i) return maxv
integer-break
we only need 2 and 3.
byuns9334
0
103
integer break
343
0.554
Medium
5,947
https://leetcode.com/problems/integer-break/discuss/1492277/Python-solution-using-DP
class Solution: def integerBreak(self, n: int) -> int: if n == 2: return 1 if n == 3: return 2 # 1 dp = [sys.maxsize] * (n+1) # 2 dp[0] = 0 dp[1] = 1 dp[2] = 2 # 3 for i in range(3,n+1): # 4 # Generate all combos that add up to i # Figure out what is bigger: i, or max((a x dp[b]) for (a,b) in combos) max_combo = max([(j * dp[i-j]) for j in range(1,i)]) # If i is bigger than the max_combo at i, just use i (this only applies to 3 and 4) dp[i] = max(i, max_combo) return dp[n]
integer-break
Python solution using DP
mikewillard87
0
46
integer break
343
0.554
Medium
5,948
https://leetcode.com/problems/integer-break/discuss/792197/Python3-dp-and-math
class Solution: def integerBreak(self, n: int) -> int: @lru_cache(None) def fn(n): """Return the max product by splitting n.""" if n == 1: return 1 return max(max(i, fn(i))*max(n-i, fn(n-i)) for i in range(1, n//2+1)) return fn(n)
integer-break
[Python3] dp & math
ye15
0
91
integer break
343
0.554
Medium
5,949
https://leetcode.com/problems/integer-break/discuss/792197/Python3-dp-and-math
class Solution: def integerBreak(self, n: int) -> int: ans = [None, 1] + [0]*(n-1) for k in range(2, n+1): for i in range(1, k//2+1): ans[k] = max(ans[k], max(i, ans[i])*max(k-i, ans[k-i])) return ans[-1]
integer-break
[Python3] dp & math
ye15
0
91
integer break
343
0.554
Medium
5,950
https://leetcode.com/problems/integer-break/discuss/792197/Python3-dp-and-math
class Solution: def integerBreak(self, n: int) -> int: if n == 2: return 1 if n == 3: return 2 ans = 1 while n > 4: ans *= 3 n -= 3 return ans*n
integer-break
[Python3] dp & math
ye15
0
91
integer break
343
0.554
Medium
5,951
https://leetcode.com/problems/integer-break/discuss/457138/python-DP-solution-with-explanation-80-faster
class Solution: def integerBreak(self, n: int) -> int: dp = [0] * (n+3) # extra buffer dp[0] = 0 dp[1] = 1 dp[2] = 1 if n <= 2: return dp[n] for i in range(3, n+1): for j in range(1, i // 2 + 1): dp[i] = max( dp[i], j * dp[i-j], j * (i-j) ) return dp[n]
integer-break
python DP solution with explanation 80% faster
sokketsu
0
141
integer break
343
0.554
Medium
5,952
https://leetcode.com/problems/integer-break/discuss/362181/Solution-in-Python-3-(beats-100)-(one-line)-(direct-solution-using-Calculus)
class Solution: def integerBreak(self, n: int) -> int: return 3**(n//3-((n-3*(n//3))<=1))*((n-3*(n//3))+3*((n-3*(n//3))<=1)) if n>3 else n-1
integer-break
Solution in Python 3 (beats 100%) (one line) (direct solution using Calculus)
junaidmansuri
0
241
integer break
343
0.554
Medium
5,953
https://leetcode.com/problems/reverse-string/discuss/670137/Python-3-~actually~-easiest-solution
class Solution: def reverseString(self, s: List[str]) -> None: s[:] = s[::-1]
reverse-string
Python 3 ~actually~ easiest solution
drblessing
77
6,800
reverse string
344
0.762
Easy
5,954
https://leetcode.com/problems/reverse-string/discuss/1480872/Python-and-Java-One-Line-Iterative-Recursive-Solutions
class Solution: def reverseString(self, s: List[str]) -> None: s.reverse()
reverse-string
Python and Java - One Line, Iterative, Recursive Solutions
Pootato
8
554
reverse string
344
0.762
Easy
5,955
https://leetcode.com/problems/reverse-string/discuss/1480872/Python-and-Java-One-Line-Iterative-Recursive-Solutions
class Solution: def reverseString(self, s: List[str]) -> None: N = len(s) for i in range(N // 2): s[i], s[-i-1] == s[-i-1], s[i]
reverse-string
Python and Java - One Line, Iterative, Recursive Solutions
Pootato
8
554
reverse string
344
0.762
Easy
5,956
https://leetcode.com/problems/reverse-string/discuss/1480872/Python-and-Java-One-Line-Iterative-Recursive-Solutions
class Solution: def reverse(self, l, r, s): if l >= r: return s[l], s[r] = s[r], s[l] self.reverse(l+1, r-1, s) def reverseString(self, s: List[str]) -> None: self.reverse(0, len(s)-1, s)
reverse-string
Python and Java - One Line, Iterative, Recursive Solutions
Pootato
8
554
reverse string
344
0.762
Easy
5,957
https://leetcode.com/problems/reverse-string/discuss/946287/Python-Easy-Solution
class Solution: def reverseString(self, s: List[str]) -> None: for i in range(len(s)//2): s[i],s[-i-1]=s[-i-1],s[i] return s
reverse-string
Python Easy Solution
lokeshsenthilkumar
8
1,400
reverse string
344
0.762
Easy
5,958
https://leetcode.com/problems/reverse-string/discuss/1902443/Python-Easy-Solutions-or-One-Liner
class Solution: def reverseString(self, s: List[str]) -> None: lo, hi = 0, len(s) - 1 while lo < hi: s[lo], s[hi] = s[hi], s[lo] lo += 1 hi -= 1
reverse-string
βœ… Python Easy Solutions | One Liner
dhananjay79
6
952
reverse string
344
0.762
Easy
5,959
https://leetcode.com/problems/reverse-string/discuss/1902443/Python-Easy-Solutions-or-One-Liner
class Solution: def reverseString(self, s: List[str]) -> None: def reverse(lo,hi): if lo > hi: return s[lo], s[hi] = s[hi], s[lo] reverse(lo+1, hi-1) reverse(0, len(s)-1)
reverse-string
βœ… Python Easy Solutions | One Liner
dhananjay79
6
952
reverse string
344
0.762
Easy
5,960
https://leetcode.com/problems/reverse-string/discuss/1902443/Python-Easy-Solutions-or-One-Liner
class Solution: def reverseString(self, s: List[str]) -> None: for i in range(1,len(s) // 2 + 1): s[i-1], s[-i] = s[-i], s[i-1]
reverse-string
βœ… Python Easy Solutions | One Liner
dhananjay79
6
952
reverse string
344
0.762
Easy
5,961
https://leetcode.com/problems/reverse-string/discuss/2403620/Pythonoror4-methodoror-beginner-friendlyororeasyto-understandororvery-simple
class Solution: def reverseString(self, s: List[str]) -> None: i=0 j=len(s)-1 def rev(s,i,j): if i>=j: return s[i],s[j]=s[j],s[i] rev(s,i+1,j-1) rev(s,i,j)
reverse-string
Python||4 method|| beginner-friendly||easyto understand||very simple
shikhar_srivastava391
4
184
reverse string
344
0.762
Easy
5,962
https://leetcode.com/problems/reverse-string/discuss/2403620/Pythonoror4-methodoror-beginner-friendlyororeasyto-understandororvery-simple
class Solution: def reverseString(self, s: List[str]) -> None: size = len(s) for i in range(size//2): s[i], s[-i-1] = s[-i-1], s[i]
reverse-string
Python||4 method|| beginner-friendly||easyto understand||very simple
shikhar_srivastava391
4
184
reverse string
344
0.762
Easy
5,963
https://leetcode.com/problems/reverse-string/discuss/1764369/Python-3-(200ms)-or-Two-Pointers-Approach-or-2-Solutions
class Solution: def reverseString(self, s: List[str]) -> None: st,e=0,len(s)-1 while st<=e: s[st],s[e]=s[e],s[st] st+=1 e-=1
reverse-string
Python 3 (200ms) | Two Pointers Approach | 2 Solutions
MrShobhit
4
338
reverse string
344
0.762
Easy
5,964
https://leetcode.com/problems/reverse-string/discuss/1764369/Python-3-(200ms)-or-Two-Pointers-Approach-or-2-Solutions
class Solution: def reverseString(self, s: List[str]) -> None: for i in range(len(s)//2): s[i], s[-i-1] = s[-i-1], s[i]
reverse-string
Python 3 (200ms) | Two Pointers Approach | 2 Solutions
MrShobhit
4
338
reverse string
344
0.762
Easy
5,965
https://leetcode.com/problems/reverse-string/discuss/1264496/Easy-Python-Solution
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ for i in range(int(len(s)/2)): s[i],s[len(s)-i-1]=s[len(s)-i-1],s[i]
reverse-string
Easy Python Solution
Sneh17029
3
1,000
reverse string
344
0.762
Easy
5,966
https://leetcode.com/problems/reverse-string/discuss/2095725/Simple-python-solutions-(3-different-methods)
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s.reverse()
reverse-string
Simple python solutions (3 different methods)
tylerpruitt
2
143
reverse string
344
0.762
Easy
5,967
https://leetcode.com/problems/reverse-string/discuss/2095725/Simple-python-solutions-(3-different-methods)
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s[:] = s[-1::-1]
reverse-string
Simple python solutions (3 different methods)
tylerpruitt
2
143
reverse string
344
0.762
Easy
5,968
https://leetcode.com/problems/reverse-string/discuss/2095725/Simple-python-solutions-(3-different-methods)
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ # First pointer (start of array) i = 0 # Second pointer (end of array) j = len(s) - 1 while i < j: s[i], s[j] = s[j], s[i] i += 1 j -= 1
reverse-string
Simple python solutions (3 different methods)
tylerpruitt
2
143
reverse string
344
0.762
Easy
5,969
https://leetcode.com/problems/reverse-string/discuss/2346952/3-SOLUTIONS-oror-SINGLE-LINE-PYTHON-oror-oror-Beginner-friendly
class Solution: def reverseString(self, s: List[str]) -> None: s[:] = s[::-1]
reverse-string
3 SOLUTIONS || SINGLE LINE PYTHON || βœ”βœ”|| Beginner friendly
adithya_s_k
1
100
reverse string
344
0.762
Easy
5,970
https://leetcode.com/problems/reverse-string/discuss/2346952/3-SOLUTIONS-oror-SINGLE-LINE-PYTHON-oror-oror-Beginner-friendly
class Solution(object): def reverseString(self, s): l = len(s) if l < 2: return s return self.reverseString(s[l/2:]) + self.reverseString(s[:l/2]) **other two general solutions** class SolutionClassic(object): def reverseString(self, s): r = list(s) i, j = 0, len(r) - 1 while i < j: r[i], r[j] = r[j], r[i] i += 1 j -= 1 return "".join(r)
reverse-string
3 SOLUTIONS || SINGLE LINE PYTHON || βœ”βœ”|| Beginner friendly
adithya_s_k
1
100
reverse string
344
0.762
Easy
5,971
https://leetcode.com/problems/reverse-string/discuss/2308497/Python-2-Easy-Solutions
class Solution: def reverseString(self, s: List[str]) -> None: left = 0 right = len(s) - 1 while left <= right: s[left], s[right] = s[right], s[left] left += 1 right -= 1 return s
reverse-string
βœ…Python 2 Easy Solutions
Skiper228
1
121
reverse string
344
0.762
Easy
5,972
https://leetcode.com/problems/reverse-string/discuss/2308497/Python-2-Easy-Solutions
class Solution: def reverseString(self, s: List[str]) -> None: s[:] = s[::-1]
reverse-string
βœ…Python 2 Easy Solutions
Skiper228
1
121
reverse string
344
0.762
Easy
5,973
https://leetcode.com/problems/reverse-string/discuss/2253500/O(n)-Runtime%3A-185-ms-faster-than-99.86-of-Python3-online-submissions-for-Reverse-String.
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ start=0 end = len(s)-1 while start<end: s[start],s[end] = s[end], s[start] start = start+1 end = end-1
reverse-string
O(n) Runtime: 185 ms, faster than 99.86% of Python3 online submissions for Reverse String.
grishptl
1
105
reverse string
344
0.762
Easy
5,974
https://leetcode.com/problems/reverse-string/discuss/2134039/2-Approaches.-Easy-to-Understand-in-Python.
class Solution: def reverseString(self, s: List[str]) -> None: s[:]=s[::-1]
reverse-string
2 Approaches. Easy to Understand in Python.
AY_
1
103
reverse string
344
0.762
Easy
5,975
https://leetcode.com/problems/reverse-string/discuss/2134039/2-Approaches.-Easy-to-Understand-in-Python.
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ sLen = len(s) j = sLen-1 for i in range(sLen//2): s[i],s[j] = s[j],s[i] j -= 1
reverse-string
2 Approaches. Easy to Understand in Python.
AY_
1
103
reverse string
344
0.762
Easy
5,976
https://leetcode.com/problems/reverse-string/discuss/2131067/Python3-Two-pointer-Solution-faster-than-90.31-easily-explained.
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ # so we use two pointers; left at 0 index and right at last index (len(numbers)-1) # then in a while loop we swap our left index value to the right index value # in one liner. That's it. left, right = 0, len(s)-1 while left < right: s[left], s[right] = s[right], s[left] left += 1 right -= 1 # return s
reverse-string
Python3 Two-pointer Solution; faster than 90.31%, easily explained.
shubhamdraj
1
56
reverse string
344
0.762
Easy
5,977
https://leetcode.com/problems/reverse-string/discuss/2098031/Python3-1-optimal-solution-1-brute-force-2-builtIn
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ return self.reverseStringOptimal(s) # O(n) || O(1) # runtime: 342 19.09% def reverseStringOptimal(self, string): if not string: return string left, right = 0, len(string) - 1 while left < right: string[left], string[right] = string[right], string[left] left += 1 right -= 1 return string # O(n) || O(n) # brute force def reverseStringWithNewList(self, string): if not string: return string newList = [0] * len(string) j = 0 for i in reversed(range(len(string))): newList[i] = string[j] j += 1 return newList # below are just 'some' python built in def reverseStringWithListCompression(self, string): if not string: return string return [string[i] for i in reversed(range(len(string)))] def reversedStringWithReverse(self, string): string.reverse() return string or string[::-1]
reverse-string
Python3 1 optimal solution, 1 brute force, 2 builtIn
arshergon
1
69
reverse string
344
0.762
Easy
5,978
https://leetcode.com/problems/reverse-string/discuss/1787196/Dumb-python-solution-oror-faster-than-93-with-space-less-than-99
class Solution: def reverseString(self, s: List[str]) -> None: for i, v in enumerate(s[::-1]): s[i] = v
reverse-string
Dumb python solution || faster than 93% with space less than 99%
shafzgamer
1
239
reverse string
344
0.762
Easy
5,979
https://leetcode.com/problems/reverse-string/discuss/1751782/Python-Simple-and-Clean-Python-Solution-By-Swapping-First-and-Last-Number
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ length = len(s) for i in range(length//2): s[i],s[-(i+1)]=s[-(i+1)],s[i]
reverse-string
[ Python ] βœ”βœ” Simple and Clean Python Solution By Swapping First and Last Number
ASHOK_KUMAR_MEGHVANSHI
1
103
reverse string
344
0.762
Easy
5,980
https://leetcode.com/problems/reverse-string/discuss/1736515/Python-93.24-faster-ororSimplest-solution-with-explanationoror-Beg-to-Advoror-Two-Pointer
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s.reverse() return s
reverse-string
Python 93.24% faster ||Simplest solution with explanation|| Beg to Adv|| Two Pointer
rlakshay14
1
152
reverse string
344
0.762
Easy
5,981
https://leetcode.com/problems/reverse-string/discuss/1736515/Python-93.24-faster-ororSimplest-solution-with-explanationoror-Beg-to-Advoror-Two-Pointer
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ left, right = 0, len(s) - 1 while left < right: #swaping the positions of the elements. s[left], s[right] = s[right], s[left] # incrementing both the pointers. right -= 1 left += 1
reverse-string
Python 93.24% faster ||Simplest solution with explanation|| Beg to Adv|| Two Pointer
rlakshay14
1
152
reverse string
344
0.762
Easy
5,982
https://leetcode.com/problems/reverse-string/discuss/1621867/faster-than-82.25-of-Python3-online-submissions-for-Reverse-String.
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ i, j = 0, len(s) - 1 while i != j and i < j: s[i], s[j] = s[j], s[i] i += 1 j -= 1
reverse-string
faster than 82.25% of Python3 online submissions for Reverse String.
sagarhasan273
1
46
reverse string
344
0.762
Easy
5,983
https://leetcode.com/problems/reverse-string/discuss/1559714/Python-two-pointers-O(n)-time-O(1)-space-beats-85-explained
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ # two pointers, one at the beginning, the other at the end # swap, unti you meet at center # O(N) time, O(1) space left_pointer, right_pointer = 0, len(s) - 1 while left_pointer < right_pointer: s[left_pointer], s[right_pointer] = s[right_pointer], s[left_pointer] left_pointer += 1 right_pointer -= 1 class Solution2: def reverseString(self, s:List[str]) -> None: # for loop + bitwise inversion might be faser, but while loop makes it more readable for l in range(len(s) // 2): s[l], s[~l] = s[~l], s[l]
reverse-string
[Python] two-pointers O(n) time, O(1) space, beats 85%, explained
mateoruiz5171
1
135
reverse string
344
0.762
Easy
5,984
https://leetcode.com/problems/reverse-string/discuss/1388002/Python3-faster-than-95.34
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ l = len(s) for i in range(l//2): s[i], s[l-i-1] = s[l-i-1],s[i] return
reverse-string
Python3 faster than 95.34%
saumyasuvarna
1
258
reverse string
344
0.762
Easy
5,985
https://leetcode.com/problems/reverse-string/discuss/1259231/Python3-dollarolution
class Solution: def reverseString(self, s: List[str]) -> None: i, j = 0, len(s)-1 while i < j: temp = s[i] s[i] = s[j] s[j] = temp i += 1 j -= 1
reverse-string
Python3 $olution
AakRay
1
296
reverse string
344
0.762
Easy
5,986
https://leetcode.com/problems/reverse-string/discuss/670009/Python-one-liner-solution
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s=s.reverse()
reverse-string
Python one liner solution
rajesh_26
1
80
reverse string
344
0.762
Easy
5,987
https://leetcode.com/problems/reverse-string/discuss/669559/simplest-solution-for-reverse-string-python3
class Solution: def reverseString(self, s: List[str]) -> None: s[:] = s[::-1]
reverse-string
simplest solution for reverse string [python3]
user4410vu
1
118
reverse string
344
0.762
Easy
5,988
https://leetcode.com/problems/reverse-string/discuss/291391/Python-basic-recursive-and-iterative-formula
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ for i in range(len(s) // 2): s[i], s[len(s) - i - 1] = s[len(s) - i - 1], s[i]
reverse-string
Python -- basic recursive and iterative formula
CorvusEtiam
1
293
reverse string
344
0.762
Easy
5,989
https://leetcode.com/problems/reverse-string/discuss/291391/Python-basic-recursive-and-iterative-formula
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ return self.loop(s, 0, len(s) // 2) def loop(self, s, i, final): if i == final: return else: s[i], s[len(s) - i - 1] = s[len(s) - i - 1], s[i] return self.loop(s, i + 1, final)
reverse-string
Python -- basic recursive and iterative formula
CorvusEtiam
1
293
reverse string
344
0.762
Easy
5,990
https://leetcode.com/problems/reverse-string/discuss/2847979/Quick-Python-solution-to-reverse-string
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ for i in range(0,len(s)): s.insert(i,s.pop()) print(s) sol=Solution() sol.reverseString(["h","e","l","l","o"])
reverse-string
Quick Python solution to reverse string
dassdipanwita
0
1
reverse string
344
0.762
Easy
5,991
https://leetcode.com/problems/reverse-string/discuss/2846086/python3-oror-in-place-o(1)-memory
class Solution: def reverseString(self, s: List[str]) -> None: l, r = 0, len(s) - 1 # left and right pointer while(l < r): # switch chars at l and r s[l], s[r] = s[r], s[l] l += 1 r -= 1
reverse-string
python3 || in-place, o(1) memory
wduf
0
2
reverse string
344
0.762
Easy
5,992
https://leetcode.com/problems/reverse-string/discuss/2842646/Python-solution.
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ l = 0 h = len(s) - 1 for i in range(len(s) // 2): s[i], s[h] = s[h], s[l] l += 1 h -= 1
reverse-string
Python solution.
ahti1405
0
2
reverse string
344
0.762
Easy
5,993
https://leetcode.com/problems/reverse-string/discuss/2840063/Reverse-String-or-Python-3.x-or-Two-pointers
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ head = -1 tail = len(s) - 1 while head != tail: tmp = s.pop(tail) tail -= 1 s.append(tmp)
reverse-string
Reverse String | Python 3.x | Two pointers
SnLn
0
1
reverse string
344
0.762
Easy
5,994
https://leetcode.com/problems/reverse-string/discuss/2830609/Python-Stack-Two-Pointers
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ stack = [] l, r = 0, len(s) - 1 stack.append((l, r)) while stack and l < r: if stack: l, r = stack.pop() s[l], s[r] = s[r], s[l] l += 1 r -= 1 stack.append((l , r))
reverse-string
Python Stack Two Pointers
user6168bh
0
1
reverse string
344
0.762
Easy
5,995
https://leetcode.com/problems/reverse-string/discuss/2819025/Simple-and-Fast-Python-Solution-One-Liner
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s.reverse()
reverse-string
Simple and Fast Python Solution - One-Liner
PranavBhatt
0
5
reverse string
344
0.762
Easy
5,996
https://leetcode.com/problems/reverse-string/discuss/2816647/list-reverse
class Solution: def reverseString(self, s: List[str]) -> None: s2 = s.copy() lens = len(s) for i in range(lens): s[i] = s2.pop()
reverse-string
list-reverse
dexck7770
0
1
reverse string
344
0.762
Easy
5,997
https://leetcode.com/problems/reverse-string/discuss/2815904/Simple-Python-using-2-Pointers
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ first, last = 0, len(s)-1 while first < last: s[first],s[last] = s[last],s[first] first +=1 last -= 1 return s
reverse-string
Simple Python using 2 Pointers
BhavyaBusireddy
0
2
reverse string
344
0.762
Easy
5,998
https://leetcode.com/problems/reverse-string/discuss/2813967/TC-%3A-95.40-Simple-solution
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s[:] = s[::-1]
reverse-string
😎 TC : 95.40 % Simple solution
Pragadeeshwaran_Pasupathi
0
3
reverse string
344
0.762
Easy
5,999