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/best-time-to-buy-and-sell-stock-ii/discuss/2557423/Python3-or-C%2B%2B-or-Easy | class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
mx = 0
for i in range(len(prices) - 1):
profit = prices[i+1] - prices[i]
if profit > 0:
mx += profit
return mx | best-time-to-buy-and-sell-stock-ii | Python3 | C++ | Easy | joshua_mur | 0 | 6 | best time to buy and sell stock ii | 122 | 0.634 | Medium | 1,300 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/discuss/2474168/python-simple-intuit-solution | class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
pre = prices[0]
for i in range(len(prices)):
profit += max((prices[i] - pre) , 0)
pre = prices[i]
return profit | best-time-to-buy-and-sell-stock-ii | python simple intuit solution | rajitkumarchauhan99 | 0 | 32 | best time to buy and sell stock ii | 122 | 0.634 | Medium | 1,301 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/discuss/2405500/Pythonor-Easy-to-understand-or-Beginner-or-Faster-than-90 | class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit=0
l=0
r=0
while r<len(prices):
if prices[r]>prices[l]:
profit+=prices[r]-prices[l]
l=r
elif prices[r]<prices[l]:
l=r
r+=1
... | best-time-to-buy-and-sell-stock-ii | Python| Easy to understand | Beginner | Faster than 90% | user2559cj | 0 | 47 | best time to buy and sell stock ii | 122 | 0.634 | Medium | 1,302 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/discuss/2370372/Python3-O(n)-time-easy-and-simple-solution | class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
for i in range(1,len(prices)):
if prices[i]>prices[i-1]:
profit += prices[i]-prices[i-1]
pass
return profit | best-time-to-buy-and-sell-stock-ii | Python3/ O(n) time / easy and simple solution | abhinav_kumar07 | 0 | 34 | best time to buy and sell stock ii | 122 | 0.634 | Medium | 1,303 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/discuss/2338819/DP-by-Python-easy-to-understand! | class Solution:
def maxProfit(self, prices: List[int]) -> int:
"""sell is the maximum benifit we can get,
buy is if you buy this stock.
So, sell=buy+"this stock" if it>sell, that means we buy last stock and sell now.
buy=sell-"this stock"
Time comlexity: O(n), space complexit... | best-time-to-buy-and-sell-stock-ii | DP by Python, easy to understand! | XRFXRF | 0 | 31 | best time to buy and sell stock ii | 122 | 0.634 | Medium | 1,304 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/discuss/2326954/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-JULY-2022 | class Solution:
def maxProfit(self, prices: List[int]) -> int:
@cache
def trade(day_d):
if day_d == 0:
# Hold on day_#0 = buy stock at the price of day_#0
# Not-hold on day_#0 = doing nothing on day_#0
... | best-time-to-buy-and-sell-stock-ii | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms JULY 2022 | cucerdariancatalin | 0 | 105 | best time to buy and sell stock ii | 122 | 0.634 | Medium | 1,305 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2040316/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-MAY-2022 | class Solution:
def maxProfit(self, prices: List[int]) -> int:
buy, sell = [inf]*2, [0]*2
for x in prices:
for i in range(2):
if i: buy[i] = min(buy[i], x - sell[i-1])
else: buy[i] = min(buy[i], x)
sell[i] = max(sell[i], x - buy[i])
... | best-time-to-buy-and-sell-stock-iii | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022 | cucerdariancatalin | 16 | 821 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,306 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/795456/Python-Simple-Solution-Fully-Explained-(video-%2B-code-%2B-image) | class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0
A = -prices[0]
B = float('-inf')
C = float('-inf')
D = float('-inf')
for price in prices:
A = max(A, -price)
B = max(B, A + pri... | best-time-to-buy-and-sell-stock-iii | Python Simple Solution Fully Explained (video + code + image) | spec_he123 | 10 | 1,000 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,307 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1477485/Well-Explained-oror-98-faster-oror-Clean-and-Concise | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
profit = [0]*n
global_min = prices[0]
for i in range(1,n):
global_min = min(global_min,prices[i])
profit[i] = max(profit[i-1],prices[i]-global_min)
res = max(profit[-1],0)
global_max = 0... | best-time-to-buy-and-sell-stock-iii | 🐍 Well-Explained || 98% faster || Clean & Concise 📌📌 | abhi9Rai | 8 | 527 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,308 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2480095/Python-O(N*2*3)-All-Easy-soln-Easy-approch | class Solution:
def f(self,ind,buy,cap,n,price,dp):
if ind==n:
return 0
if cap==0:
return 0
if dp[ind][buy][cap]!=-1:
return dp[ind][buy][cap]
if buy:
profit=max(-price[ind]+self.f(ind+1,0,cap,n,price,dp),0+self.f(ind+1,1,cap,n,price,dp... | best-time-to-buy-and-sell-stock-iii | Python O(N*2*3) All Easy soln Easy approch✔ | adarshg04 | 7 | 254 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,309 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1243694/Python-O(kn)-and-O(n)-time-complexity-DP-solutions | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
dp = [[[0 for i in range(2)] for i in range(n)] for i in range(3)]
for k in range(1,3):
for i in range(n):
if i == 0 and k == 1:
dp[k][i][1] = -prices[i]
... | best-time-to-buy-and-sell-stock-iii | Python O(kn) & O(n) time complexity DP solutions | m0biu5 | 4 | 460 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,310 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1243694/Python-O(kn)-and-O(n)-time-complexity-DP-solutions | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
dp = [[0 for i in range(n)] for i in range(3)]
for k in range(1,3):
buy = -999999
for i in range(n):
if i == 0:
buy = -prices[i]
... | best-time-to-buy-and-sell-stock-iii | Python O(kn) & O(n) time complexity DP solutions | m0biu5 | 4 | 460 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,311 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1243694/Python-O(kn)-and-O(n)-time-complexity-DP-solutions | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n, buy1, buy2, sell1, sell2, temp = len(prices), 999999, 999999, 0, 0, 0
for i in range(n):
buy1 = min(buy1, prices[i])
sell1 = max(prices[i] - buy1, sell1)
buy2 = min(buy2, prices[i] - sell1)... | best-time-to-buy-and-sell-stock-iii | Python O(kn) & O(n) time complexity DP solutions | m0biu5 | 4 | 460 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,312 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/702602/Python3-dp | class Solution:
def maxProfit(self, prices: List[int]) -> int:
buy, sell = [inf]*2, [0]*2
for x in prices:
for i in range(2):
if i: buy[i] = min(buy[i], x - sell[i-1])
else: buy[i] = min(buy[i], x)
sell[i] = max(sell[i], x - buy[i])
... | best-time-to-buy-and-sell-stock-iii | [Python3] dp | ye15 | 4 | 269 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,313 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/702602/Python3-dp | class Solution:
def maxProfit(self, prices: List[int]) -> int:
pnl = [0]*len(prices)
for _ in range(2):
most = 0
for i in range(1, len(prices)):
most = max(pnl[i], most + prices[i] - prices[i-1])
pnl[i] = max(pnl[i-1], most)
return pnl... | best-time-to-buy-and-sell-stock-iii | [Python3] dp | ye15 | 4 | 269 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,314 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2320972/O(N)-Solution-With-Explanation-Using-A-Running-Example | class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
"""
The solution to this problem is based on several observations
- We can make a maximum of 2 transactions.
- Since we can make a maximu... | best-time-to-buy-and-sell-stock-iii | O(N) Solution With Explanation Using A Running Example | suhail03 | 2 | 112 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,315 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2120983/Python-or-DP-or | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
dp = [[[0 for i in range(3)] for i in range(2)] for i in range(n+1)]
ind = n-1
while(ind >= 0):
for buy in range(2):
for cap in range(1,3):
... | best-time-to-buy-and-sell-stock-iii | Python | DP | | LittleMonster23 | 2 | 156 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,316 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2120983/Python-or-DP-or | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
after = [[0 for i in range(3)] for i in range(2)]
curr = [[0 for i in range(3)] for i in range(2)]
ind = n-1
while(ind >= 0):
for buy in range(2):
... | best-time-to-buy-and-sell-stock-iii | Python | DP | | LittleMonster23 | 2 | 156 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,317 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2326951/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-JULY-2022 | class Solution:
def maxProfit(self, prices: List[int]) -> int:
buy, sell = [inf]*2, [0]*2
for x in prices:
for i in range(2):
if i: buy[i] = min(buy[i], x - sell[i-1])
else: buy[i] = min(buy[i], x)
sell[i] = max(sell[i], x - buy[i])
... | best-time-to-buy-and-sell-stock-iii | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms JULY 2022 | cucerdariancatalin | 1 | 195 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,318 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1608947/O(n)-and-O(1)-beats-90-98-easy-to-understand-python-code | class Solution:
def maxProfit(self, prices: List[int]) -> int:
if (len(prices) == 1): return 0
profit_11 = profit_12 = -prices[0]
profit_01 = profit_02 = profit_03 = 0
for price in prices[1:]:
profit_11 = max(profit_11, profit_01 - price)
pro... | best-time-to-buy-and-sell-stock-iii | O(n) & O(1), beats 90% 98%, easy to understand python code | jhps73 | 1 | 227 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,319 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1476696/Python3-or-Recusrion%2BMemoization | class Solution:
def maxProfit(self, prices: List[int]) -> int:
self.dp={}
return self.dfs(0,0,prices,2)
def dfs(self,day,own,prices,k):
if k==0 or day==len(prices):
return 0
if (day,k,own) in self.dp:
return self.dp[(day,k,own)]
if own:
... | best-time-to-buy-and-sell-stock-iii | [Python3] | Recusrion+Memoization | swapnilsingh421 | 1 | 158 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,320 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1458121/Simple-Python-O(n)-two-pass-solution | class Solution:
def maxProfit(self, prices: List[int]) -> int:
# forward pass to find the maximum profit
# between index 0 and index i
# (same as Best Time to Buy and Sell Stock I)
cur_min, max_profit = float("inf"), 0
max_profit_first = []
for i in range(len(prices)):
cur... | best-time-to-buy-and-sell-stock-iii | Simple Python O(n) two pass solution | Charlesl0129 | 1 | 254 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,321 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2788230/Python-solution-generalizing-to-K-transactions | class Solution:
def maxProfit(self, prices: List[int]) -> int:
#You can replace K with any number
K = 2
T = [[0 for _ in range(len(prices))] for _ in range(2*K+2)]
for i in range(3, len(T), 2):
T[i][0] = -prices[0]
for i in range(1,3):
for j in range(1... | best-time-to-buy-and-sell-stock-iii | Python solution generalizing to K transactions | dominhnhut01 | 0 | 6 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,322 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2741142/Intuitive-Python-Recursion-%2B-Memoization | class Solution:
def maxProfit(self, prices: List[int]) -> int:
@cache
def max_profit(idx, phase):
'''
Starting from idx and phase, what is the max profit?
Phase = 1 --> starting point
= 2 --> holding
= 3 --> cash, bought once
... | best-time-to-buy-and-sell-stock-iii | Intuitive Python Recursion + Memoization | npa02012 | 0 | 9 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,323 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2641628/Linear-traversal-no-DP-beat-94-Runtime-and-96-Memory | class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
if n<2: return 0
left_diff = [0 for i in range(n)] #define this to be the max diff for the sub array ending in index i
cur_min = prices[0]
cur_diff = 0
for i in range(1,len(prices)):
... | best-time-to-buy-and-sell-stock-iii | Linear traversal, no DP, beat 94% Runtime and 96% Memory | bjht3 | 0 | 7 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,324 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2618166/92-faster-Python-solution-oror-Dynamic-programming-oror-Prefix-Sum-oror-Greedy | class Solution:
def maxProfit(self, prices: List[int]) -> int:
leftPrefix = self.leftPrefix(prices, 0, 1)
left = len(prices) - 2
right = len(prices) - 1
profit = maxProfit = 0
while left > 0:
profit = max(profit, prices[right] - prices[left])
maxProfit... | best-time-to-buy-and-sell-stock-iii | 92% faster Python solution || Dynamic programming || Prefix Sum || Greedy | Sefinehtesfa34 | 0 | 63 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,325 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2558613/Easy-Solution-Dynamic-Programming | class Solution:
def maxProfit(self, prices: List[int]) -> int:
k=2
min_price = [float("inf")] * (k + 1)
max_profit = [0] * (k + 1)
for price in prices:
for i in range(1, k + 1):
min_price[i] = min(min_price[i], price - max_profit... | best-time-to-buy-and-sell-stock-iii | Easy Solution Dynamic Programming | mehtadeepparesh | 0 | 19 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,326 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2434728/Fast-and-Simple-Python3-or-O(n) | class Solution:
def maxProfit(self, prices: List[int]) -> int:
cur_max, max_after = float('-inf'), []
for price in reversed(prices):
cur_max = max(cur_max, price)
max_after.append(cur_max)
max_after.reverse()
cur_min, max_prof_left, max_so_far = float... | best-time-to-buy-and-sell-stock-iii | Fast & Simple Python3 | O(n) | ryangrayson | 0 | 54 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,327 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2431055/Python-DP-Solution-(Memoization) | class Solution:
def maxProfit(self, prices: List[int]) -> int:
d = {}
def bs(index, buyOrNotBuy, timesRemaining):
if index >= len(prices):
return 0
if timesRemaining == 0:
return 0
# if we are able to buy the stock, if we already so... | best-time-to-buy-and-sell-stock-iii | Python DP Solution (Memoization) | DietCoke777 | 0 | 52 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,328 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2425324/Best-time-to-buy-and-sell-3-oror-Python3 | class Solution:
def maxProfit(self, prices: List[int]) -> int:
mat = [0] * len(prices)
min_buy = math.inf
max_sell_profit = -math.inf
# 1st traversal from start to end maintaining the optimal sell at or before that point
for i in range(0, len(prices)):
mi... | best-time-to-buy-and-sell-stock-iii | Best time to buy and sell 3 || Python3 | vanshika_2507 | 0 | 43 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,329 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/2060382/Python-Solution | class Solution:
def maxProfit(self, l: List[int]) -> int:
n = len(l)
l1 = [0 for i in range(n)]
l2 = [0 for i in range(n)]
mi = l[0]
ma = l[-1]
for i in range(n):
if i!=0:
l1[i] = max(l1[i-1],l[i]-mi)
mi = min(mi,l[i])
... | best-time-to-buy-and-sell-stock-iii | Python Solution | Shivamk09 | 0 | 103 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,330 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1789138/Python-easy-to-read-and-understand-or-Memoization | class Solution:
def solve(self, prices, index, option, trans):
if index == len(prices) or trans == 0:
return 0
res = 0
if option == 0:
buy = self.solve(prices, index + 1, 1, trans) - prices[index]
cool = self.solve(prices, index + 1, 0, trans)
... | best-time-to-buy-and-sell-stock-iii | Python easy to read and understand | Memoization | sanial2001 | 0 | 268 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,331 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1789138/Python-easy-to-read-and-understand-or-Memoization | class Solution_memo:
def solve(self, prices, index, option, trans):
if index == len(prices) or trans == 0:
return 0
if (index, option, trans) in self.dp:
return self.dp[(index, option, trans)]
if option == 0:
buy = self.solve(prices, index + 1, 1, trans) -... | best-time-to-buy-and-sell-stock-iii | Python easy to read and understand | Memoization | sanial2001 | 0 | 268 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,332 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1523578/Python-State-machine-easy-to-understand | class Solution:
def maxProfit(self, prices: List[int]) -> int:
oneBuy = -math.inf
oneBuyOneSell = 0
twoBuy = -math.inf
twoBuyTwoSell = 0
for price in prices:
oneBuy = max(oneBuy, -price)
oneBuyOneSell = max(oneBuyOneSell, oneBuy+price)
... | best-time-to-buy-and-sell-stock-iii | Python - State machine - easy to understand | zoro_55 | 0 | 80 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,333 |
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/1523195/Python3-Easy-DFS-%2B-Memoization | class Solution:
def maxProfit(self, prices: List[int]) -> int:
@cache
def dfs(i, hasBought, transactions):
if (i >= len(prices) or transactions >= 2):
return 0
maxProfit = 0
if (hasBought):
maxProfit = prices[i] + dfs(i + 1, False, ... | best-time-to-buy-and-sell-stock-iii | Python3 - Easy DFS + Memoization ✅ | Bruception | 0 | 185 | best time to buy and sell stock iii | 123 | 0.449 | Hard | 1,334 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2040330/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-MAY-2022 | class Solution:
def __init__(self):
self.maxSum = float('-inf')
def maxPathSum(self, root: TreeNode) -> int:
def traverse(root):
if root:
left = traverse(root.left)
right = traverse(root.right)
self.maxSum = max(self.maxSum,root.val, ro... | binary-tree-maximum-path-sum | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022 | cucerdariancatalin | 17 | 1,100 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,335 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/577860/Python-beats-89.49-basic-recursion-structure-w-algorithm-explanation | class Solution:
def __init__(self):
self.maxSum = float('-inf')
def maxPathSum(self, root: TreeNode) -> int:
def traverse(root):
if root:
left = traverse(root.left)
right = traverse(root.right)
self.maxSum = max(self.maxSum,root.val, ro... | binary-tree-maximum-path-sum | Python beats 89.49%, basic recursion structure w/ algorithm explanation | alexlee1995 | 6 | 175 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,336 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/982680/Simple-Python-Recursive-Solution-easy-to-understand | class Solution:
def maxPathSum(self, root: TreeNode) -> int:
def helper(node):
if not node:
return (float('-inf'),float('-inf'))
l_TBC,l_Complete = helper(node.left)
r_TBC,r_Complete = helper(node.right)
rt_TBC = max(node.val,node.val+l_TBC,nod... | binary-tree-maximum-path-sum | Simple Python Recursive Solution, easy to understand | KevinZzz666 | 3 | 331 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,337 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2098751/Python3-Recursive-Solution-with-Comments-Easy-To-Understand | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
if not root: return
self.res = root.val
def solve(root):
# Base case
if not root: return 0
# l and r store maximum path sum going through left and right child o... | binary-tree-maximum-path-sum | [Python3] Recursive Solution with Comments Easy To Understand | samirpaul1 | 2 | 129 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,338 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1383476/Concise-recursion-97-speed | class Solution:
def maxPathSum(self, root: TreeNode) -> int:
max_path = -inf
def traverse(node: TreeNode):
nonlocal max_path
left = traverse(node.left) if node.left else 0
right = traverse(node.right) if node.right else 0
max_path = max(max_path, node... | binary-tree-maximum-path-sum | Concise recursion, 97% speed | EvgenySH | 2 | 241 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,339 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2748344/O(N)-python-diagramatically-explained-solution | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.maxsum = -inf
#get the continuous path sum with "node" as the root/middle of the path
def pathsum(node):
if not node:
return 0
#no point in adding negetive path
l... | binary-tree-maximum-path-sum | O(N) python diagramatically explained solution | user9611y | 1 | 81 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,340 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1757401/Python-or-DFS | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
maxy=-inf
def dfs(node):
nonlocal maxy
if not node:
return 0
l=dfs(node.left)
r=dfs(node.right)
maxy=max(l+node.val,r+node.val,l+r+node.val,node.val,maxy... | binary-tree-maximum-path-sum | Python | DFS | heckt27 | 1 | 154 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,341 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1636863/Python-Recursion-beats-90-runtime-92-memory | class Solution:
# time: O(n)
# space: O(H) - O(logn) if balanced tree else O(n)
def maxPathSum(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
self.max_sum = -float('inf')
self.search_path(root)
return self.max_sum
def... | binary-tree-maximum-path-sum | [Python] Recursion beats 90% runtime 92% memory | sh3956 | 1 | 174 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,342 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1583626/python-simple-dfs-with-explanation-oror-include-and-exclude-parent-oror-beats-99 | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
def dfs(node):
if not node.left and not node.right:
return node.val, float('-inf')
left_i = left_e = right_i = right_e = float('-inf')
if node.left:
left_i, left_e = dfs... | binary-tree-maximum-path-sum | [python] simple dfs with explanation || include and exclude parent || beats 99% | kevintancs | 1 | 225 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,343 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1409592/Easy-to-Understand-Python-Solution-w-Comments | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
res = float('-inf')
def recur(root):
nonlocal res
if root is None:
return 0
#the maximum path-sum from the left and right subtree
left_max, right_max = recur(root.left), recur(root.right)
#we don't want previous recursive... | binary-tree-maximum-path-sum | Easy to Understand Python Solution w/ Comments | mguthrie45 | 1 | 169 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,344 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/533568/Self-explanatory-python-code | class Solution:
def maxPathSum(self, root: TreeNode) -> int:
totalsum = -math.inf
def dfs(node):
if not node:
return 0
lsum = dfs(node.left)
rsum = dfs(node.right)
diam_sum = max(lsum + rsum + node.val, node.va... | binary-tree-maximum-path-sum | Self explanatory python code | purushottam3 | 1 | 207 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,345 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2808450/Simple-Python3-DFS-or-Beats-98 | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
max_sum = float('-inf')
def dfs(root):
if not root:
return float('-inf')
left_max = dfs(root.left)
right_max = dfs(root.right)
curr_val = root.val
curr_r... | binary-tree-maximum-path-sum | Simple Python3 DFS | Beats 98% | burnsdy | 0 | 9 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,346 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2657347/Python-DFS-with-explanations. | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.res = float('-inf') # save largest sum
def dfs(root): # return max path sums with (root.val + left or right)
if not root: return float('-inf') # boarder condition
l = dfs(root.left)
... | binary-tree-maximum-path-sum | Python DFS with explanations. | xiangyupeng1994 | 0 | 7 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,347 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2521484/Python-3-Recursive-Approach-Explained | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
max_sum = root.val
def recursion(node):
nonlocal max_sum
if not node:
return 0
left = recursion(node.left)
right = recursion(node.... | binary-tree-maximum-path-sum | Python 3 Recursive Approach Explained | sebikawa | 0 | 57 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,348 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2489925/Python-DFS-with-full-working-explanation | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int: # Time: O(n) and Space: O(h)
max_path = float('-inf')
def get_max_gain(node):
nonlocal max_path
if node is None:
return 0
gain_on_left = max(get_max_gain(node.left... | binary-tree-maximum-path-sum | Python DFS with full working explanation | DanishKhanbx | 0 | 54 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,349 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2326947/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-JULY-2022 | class Solution:
def __init__(self):
self.maxSum = float('-inf')
def maxPathSum(self, root: TreeNode) -> int:
def traverse(root):
if root:
left = traverse(root.left)
right = traverse(root.right)
self.maxSum = max(self.maxSum,root.val, ro... | binary-tree-maximum-path-sum | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms JULY 2022 | cucerdariancatalin | 0 | 120 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,350 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2326940/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-JULY-2022 | class Solution:
def __init__(self):
self.maxSum = float('-inf')
def maxPathSum(self, root: TreeNode) -> int:
def traverse(root):
if root:
left = traverse(root.left)
right = traverse(root.right)
self.maxSum = max(self.maxSum,root.val, ro... | binary-tree-maximum-path-sum | O(n)time/BEATS 99.97% MEMORY/SPEED 0ms JULY 2022 | cucerdariancatalin | 0 | 56 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,351 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2264280/Python-Easy-Solution | class Solution(object):
def maxPathSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
max_sum = [root.val]
def dfs(node):
if not node:
return 0
left = dfs(node.left)
right = dfs(node.... | binary-tree-maximum-path-sum | Python Easy Solution | Abhi_009 | 0 | 62 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,352 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/2016178/Python-Easy-to-Understand-Solution | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.ans = -float('inf')
def dfs(root):
if root:
l = dfs(root.left)
r = dfs(root.right)
ans = root.val
ans = max(ans, l + ans)
ans = max(... | binary-tree-maximum-path-sum | Python Easy to Understand Solution | user6397p | 0 | 65 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,353 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1987089/Python-or-Easy-to-understand-or-DFS | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
return self.maxPathSumOfTree(root)[0]
# return a 2-elements-tuple: (maxPathSum of tree, pathSum includes the root node)
# notice the "maxPathSum of tree" may or may not includ... | binary-tree-maximum-path-sum | Python | Easy to understand | DFS | AdairCLO | 0 | 37 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,354 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1836722/Python-Easy-Recursive-Solution-with-explanation-O(n)-complexities | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
res = [root.val] # using list as global variable
def dfs(root):
if not root : return 0 # if not root then go back
left = dfs(root.left) # as its dfs . so ... | binary-tree-maximum-path-sum | [Python] Easy Recursive Solution , with explanation ; O(n) complexities | Into_You | 0 | 74 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,355 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1740397/Python-DFS | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
def dfs(node):
if not node:
return 0
left = dfs(node.left)
right = dfs(node.right)
self.result = max(self.result, max(0, left) ... | binary-tree-maximum-path-sum | Python, DFS | blue_sky5 | 0 | 83 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,356 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1632388/Python3-Recursion-similar-way-to-finding-the-HEIGHT-for-each-node | class Solution:
def __init__(self):
self.maxSum = float(-inf)
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.recur_path(root)
return self.maxSum
def recur_path(self, current):
if current is None:
return 0
left = max(0,self.recur_path(current.l... | binary-tree-maximum-path-sum | [Python3] Recursion, similar way to finding the HEIGHT for each node | mch0717 | 0 | 23 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,357 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1597035/Python3-recursion | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.sum = float('-inf')
self.helper(root)
return self.sum
def helper(self, root):
if not root:
return 0
left, right = self.helper(root.left), self.helper(root.right)
... | binary-tree-maximum-path-sum | Python3 recursion | Mandyzmr | 0 | 77 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,358 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1579889/Python3-Solution-or-99.89-faster | class Solution:
def maxPathSum(self, root) -> int:
self.ans = float('-inf')
def PathSum(node):
if not node: return 0
left,right = PathSum(node.left),PathSum(node.right)
self.ans = max(self.ans, node.val+left+right)
return max(0, node.val+left, node.val... | binary-tree-maximum-path-sum | Python3 Solution | 99.89% faster | satyam2001 | 0 | 186 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,359 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1513230/Python-verbose-with-comments | class TreeInfo:
def __init__(self, max_as_branch, max_as_branch_or_triangle):
self.max_as_branch = max_as_branch
# max continuous path as branch/tree
self.max_as_branch_or_triangle = max_as_branch_or_triangle
class Solution:
def maxPathSum(self, root):
if not root:
... | binary-tree-maximum-path-sum | Python verbose with comments | paulonteri | 0 | 125 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,360 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1478762/python-3-easy-to-understand-recursion-dfs-O(n)-beats-98 | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
ans = -1000
def dfs(curr):
nonlocal ans
if curr is None:
return 0
left = dfs(curr.left)
right = dfs(curr.right)
ans = max(... | binary-tree-maximum-path-sum | python 3 easy to understand recursion dfs O(n) beats 98% | ashish_chiks | 0 | 83 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,361 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1445709/124.-Binary-Tree-Maximum-Path-Sum-or-Python-Solution | class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self._maxPathSum = -pow(2, 31) - 1
self.findMaxPathSum(root)
return self._maxPathSum
def findMaxPathSum(self, node):
if node is None:
return 0
leftTreeSum = self.findMaxPathSum(no... | binary-tree-maximum-path-sum | 124. Binary Tree Maximum Path Sum | Python Solution | rohanpednekar_ | 0 | 236 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,362 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1290757/Python-DP-recursion-with-three-cases | class Solution:
def maxPathSum(self, root: TreeNode) -> int:
single = {} # maxsum using the root and at most one child
double = {} # maxsum using the root and at most two child
no = {} # maxsum not using the root at all (i.e. using the substructures)
def process(root):
if... | binary-tree-maximum-path-sum | Python DP recursion with three cases | wanghua_wharton | 0 | 122 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,363 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/1245720/Python-or-Javascript-O(n)-solution-using-DFS | class Solution:
def __init__(self, ans = -999999):
self.ans = ans
def MaxPathSum(self, root: TreeNode) -> int:
if root == None:
return 0
left_sum, right_sum = self.MaxPathSum(root.left), self.MaxPathSum(root.right)
var1, var2 = (left_sum if left_sum >= 0 else 0) + ... | binary-tree-maximum-path-sum | Python | Javascript O(n) solution using DFS | m0biu5 | 0 | 70 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,364 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/882668/python3-beats-99.64 | class Solution:
gmax = 0
def maxPathSum(self, root: TreeNode) -> int:
self.gmax = root.val
self.mp(root)
return self.gmax
def mp(self,node):
if not node:
return 0
left = self.mp(node.left) + node.val
right = self.mp(node.right) + node.... | binary-tree-maximum-path-sum | python3 beats 99.64% | amrmahmoud96 | 0 | 331 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,365 |
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/367906/Python-Recursive-Not-sure-why-it's-incorrect | class Solution:
def maxPathSum(self, root: TreeNode) -> int:
return self.maxPathSumHelper(root)[2]
def maxPathSumHelper(self, root: TreeNode):
"""
(alo, connected, sum)
"""
if root == None:
return (False, False, 0)
(l_alo, l_connected, l_... | binary-tree-maximum-path-sum | Python Recursive - Not sure why it's incorrect | Olshansky | 0 | 105 | binary tree maximum path sum | 124 | 0.385 | Hard | 1,366 |
https://leetcode.com/problems/valid-palindrome/discuss/350929/Solution-in-Python-3-(beats-~100)-(two-lines)-(-O(1)-solution-as-well-) | class Solution:
def isPalindrome(self, s: str) -> bool:
s = [i for i in s.lower() if i.isalnum()]
return s == s[::-1] | valid-palindrome | Solution in Python 3 (beats ~100%) (two lines) ( O(1) solution as well ) | junaidmansuri | 50 | 8,600 | valid palindrome | 125 | 0.437 | Easy | 1,367 |
https://leetcode.com/problems/valid-palindrome/discuss/350929/Solution-in-Python-3-(beats-~100)-(two-lines)-(-O(1)-solution-as-well-) | class Solution:
def isPalindrome(self, s: str) -> bool:
i, j = 0, len(s) - 1
while i < j:
a, b = s[i].lower(), s[j].lower()
if a.isalnum() and b.isalnum():
if a != b: return False
else:
i, j = i + 1, j - 1
continue
i, j = i + (not a.isalnum()), j - (not b.is... | valid-palindrome | Solution in Python 3 (beats ~100%) (two lines) ( O(1) solution as well ) | junaidmansuri | 50 | 8,600 | valid palindrome | 125 | 0.437 | Easy | 1,368 |
https://leetcode.com/problems/valid-palindrome/discuss/1151720/Python3-O(n)-time-O(1)-space.-Well-explained | class Solution:
def isPalindrome(self, s: str) -> bool:
# i starts at beginning of s and j starts at the end
i, j = 0, len(s) - 1
# While i is still before j
while i < j:
# If i is not an alpha-numeric character then advance i
if n... | valid-palindrome | Python3 O(n) time, O(1) space. Well-explained | ryancodrai | 16 | 1,200 | valid palindrome | 125 | 0.437 | Easy | 1,369 |
https://leetcode.com/problems/valid-palindrome/discuss/2438656/Very-Easy-oror-100-oror-Fully-Explained-(Java-C%2B%2B-Python-JS-Python3) | class Solution(object):
def isPalindrome(self, s):
# Initialize two pointer variables, left and right and point them with the two ends of the input string...
left, right = 0, len(s) - 1
# Traverse all elements through the loop...
while left < right:
# Move the left pointe... | valid-palindrome | Very Easy || 100% || Fully Explained (Java, C++, Python, JS, Python3) | PratikSen07 | 13 | 2,300 | valid palindrome | 125 | 0.437 | Easy | 1,370 |
https://leetcode.com/problems/valid-palindrome/discuss/2438656/Very-Easy-oror-100-oror-Fully-Explained-(Java-C%2B%2B-Python-JS-Python3) | class Solution:
def isPalindrome(self, s: str) -> bool:
# Initialize two pointer variables, left and right and point them with the two ends of the input string...
left, right = 0, len(s) - 1
# Traverse all elements through the loop...
while left < right:
# Move the left p... | valid-palindrome | Very Easy || 100% || Fully Explained (Java, C++, Python, JS, Python3) | PratikSen07 | 13 | 2,300 | valid palindrome | 125 | 0.437 | Easy | 1,371 |
https://leetcode.com/problems/valid-palindrome/discuss/2661691/PYTHON-3-Simple-or-isnumeric()-or-isalpha()-or-Easy-to-Understand | class Solution:
def isPalindrome(self, s: str) -> bool:
a = ""
for x in [*s]:
if x.isalpha(): a += x.lower()
if x.isnumeric(): a += x
return a == a[::-1] | valid-palindrome | [PYTHON 3] Simple | isnumeric() | isalpha() | Easy to Understand | omkarxpatel | 7 | 1,900 | valid palindrome | 125 | 0.437 | Easy | 1,372 |
https://leetcode.com/problems/valid-palindrome/discuss/2097709/PYTHON-oror-94-TIME-oror-86-MEMORY-oror-EASY | class Solution:
def isPalindrome(self, s: str) -> bool:
i,j=0,len(s)-1
s=s.lower()
a={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
while (i<=j):
while i<=j and s[i] not in ... | valid-palindrome | ✔️PYTHON || ✔️94% TIME || 86% MEMORY || EASY | karan_8082 | 6 | 541 | valid palindrome | 125 | 0.437 | Easy | 1,373 |
https://leetcode.com/problems/valid-palindrome/discuss/2089935/Valid-Palindrome-in-Python-solution-92-faster | class Solution:
def isPalindrome(self, s: str) -> bool:
k = ''
for i in s:
if i.isalpha() or i.isdigit():
k += i.lower()
return k == k[::-1] | valid-palindrome | Valid Palindrome in Python solution 92 faster | Murod_Turaev | 6 | 403 | valid palindrome | 125 | 0.437 | Easy | 1,374 |
https://leetcode.com/problems/valid-palindrome/discuss/1447745/2-LINE-CODE-or-PYTHON-or-FASTER-THAN-95.88or-PLEASE-UPVOTE | class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join([char.casefold() for char in s if char.isalnum()])
return s == s[::-1] | valid-palindrome | 2 LINE CODE | PYTHON | FASTER THAN 95.88%| PLEASE UPVOTE 🙂 | nisargndoshi | 6 | 679 | valid palindrome | 125 | 0.437 | Easy | 1,375 |
https://leetcode.com/problems/valid-palindrome/discuss/998883/Python3-faster-than-90-Easy-to-understand | class Solution:
def isPalindrome(self, s: str) -> bool:
s1 = ''
for i in s:
if i.isalnum():
s1+=i.lower()
return s1[::-1] == s1 | valid-palindrome | Python3 faster than 90% Easy to understand | rotikapdamakan | 5 | 260 | valid palindrome | 125 | 0.437 | Easy | 1,376 |
https://leetcode.com/problems/valid-palindrome/discuss/2546948/2-Python-solutions-(2-pointers-solution-%2B-simple-2-lines-solution) | class Solution:
def isPalindrome(self, s: str) -> bool:
ptr1, ptr2 = 0, len(s)-1
while ptr1 < ptr2:
while ptr1 < ptr2 and not s[ptr1].isalnum():
ptr1 += 1
while ptr1 < ptr2 and not s[ptr2].isalnum():
ptr2 -= 1
if s[ptr1... | valid-palindrome | 📌 2 Python solutions (2 pointers solution + simple 2 lines solution) | croatoan | 3 | 280 | valid palindrome | 125 | 0.437 | Easy | 1,377 |
https://leetcode.com/problems/valid-palindrome/discuss/2546948/2-Python-solutions-(2-pointers-solution-%2B-simple-2-lines-solution) | class Solution:
def isPalindrome(self, s: str) -> bool:
filter = ''.join([i.lower() for i in s if i.isalnum()])
return filter == filter[::-1] | valid-palindrome | 📌 2 Python solutions (2 pointers solution + simple 2 lines solution) | croatoan | 3 | 280 | valid palindrome | 125 | 0.437 | Easy | 1,378 |
https://leetcode.com/problems/valid-palindrome/discuss/1619720/Python3-Time-O(n)-or-Space-O(1)-or-2-pointers-in-place | class Solution:
def isPalindrome(self, s: str) -> bool:
l, r = 0, len(s) - 1
while l < r:
if s[l].isalnum() and s[r].isalnum():
if s[l].lower() != s[r].lower():
return False
l += 1
r -= 1
else:
... | valid-palindrome | [Python3] Time O(n) | Space O(1) | 2 pointers in-place | PatrickOweijane | 3 | 351 | valid palindrome | 125 | 0.437 | Easy | 1,379 |
https://leetcode.com/problems/valid-palindrome/discuss/1395136/Python-solution-in-2-lines | class Solution:
def isPalindrome(self, s: str) -> bool:
output = ''.join([char for char in s.lower() if char.isalpha() or char.isdigit()])
return output == output[::-1] | valid-palindrome | Python solution in 2 lines | bob23 | 3 | 297 | valid palindrome | 125 | 0.437 | Easy | 1,380 |
https://leetcode.com/problems/valid-palindrome/discuss/366729/Python-2-line | class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(l for l in s.casefold() if l.isalnum())
return s == s[::-1] | valid-palindrome | Python 2 line | amchoukir | 3 | 520 | valid palindrome | 125 | 0.437 | Easy | 1,381 |
https://leetcode.com/problems/valid-palindrome/discuss/2822176/PYTHON-isalpha()-or-isnumeric()-or-Simple | class Solution:
def isPalindrome(self, s: str) -> bool:
a = ""
for x in [*s]:
if x.isalpha(): a += x.lower()
if x.isnumeric(): a += x
return a == a[::-1] | valid-palindrome | [PYTHON] isalpha() | isnumeric() | Simple | omkarxpatel | 2 | 31 | valid palindrome | 125 | 0.437 | Easy | 1,382 |
https://leetcode.com/problems/valid-palindrome/discuss/2773675/Clean-python-code-O(n) | class Solution:
def isPalindrome(self, s: str) -> bool:
left, right = 0, len(s)-1
while right > left:
if not s[right].isalnum():
right -= 1
continue
if not s[left].isalnum():
left += 1
continue
if s[l... | valid-palindrome | Clean python code O(n) | really_cool_person | 2 | 844 | valid palindrome | 125 | 0.437 | Easy | 1,383 |
https://leetcode.com/problems/valid-palindrome/discuss/2430462/Python-Super-Simple-Cake-Walk-Solution | class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(c for c in s if c.isalnum()) #isalnum() checks whether character is alphanumeric or not
s = s.lower() # converts the string to lowercase
return s == s[::-1] # checks whether reverse of string is equal to st... | valid-palindrome | 🐍🐲 Python Super Simple Cake Walk Solution 🐲🐍 | sHadowSparK | 2 | 107 | valid palindrome | 125 | 0.437 | Easy | 1,384 |
https://leetcode.com/problems/valid-palindrome/discuss/1982004/Simple-Python-Solution-or-Faster-than-92 | class Solution:
def isPalindrome(self, s: str) -> bool:
res = ""
for c in s:
if c.isalnum():
res += c.lower()
print(res)
return res == res[::-1] | valid-palindrome | Simple Python Solution | Faster than 92% | nikhitamore | 2 | 277 | valid palindrome | 125 | 0.437 | Easy | 1,385 |
https://leetcode.com/problems/valid-palindrome/discuss/1860184/PYTHON3-or-Fastest-solution | class Solution:
def isPalindrome(self, s: str) -> bool:
s=s.lower()
alpha="abcdefghijklmnopqrstuvwxyz0123456789"
a=""
for i in s:
if i in alpha:
a+=i
return a[:]==a[::-1] | valid-palindrome | PYTHON3 | Fastest solution | Anilchouhan181 | 2 | 169 | valid palindrome | 125 | 0.437 | Easy | 1,386 |
https://leetcode.com/problems/valid-palindrome/discuss/1796466/Best-Python-Solution | class Solution:
def isPalindrome(self, s: str) -> bool:
a = ''
for i in s.lower():
if i.isalnum():
a += i
return a==a[::-1] | valid-palindrome | Best Python Solution | himanshu11sgh | 2 | 163 | valid palindrome | 125 | 0.437 | Easy | 1,387 |
https://leetcode.com/problems/valid-palindrome/discuss/1722965/Python-not-the-Fastest-but-98.88-less-memory | class Solution:
def isPalindrome(self, s: str) -> bool:
#var to hold new string
stringcheck = ''
#iterate through the letters
for letter in s:
if letter.isalnum() == True:
stringcheck=stringcheck+letter
return(stringcheck.lower() == stringcheck[... | valid-palindrome | Python not the Fastest but 98.88% less memory | ovidaure | 2 | 281 | valid palindrome | 125 | 0.437 | Easy | 1,388 |
https://leetcode.com/problems/valid-palindrome/discuss/1623764/Python-solution-beats-98.59-on-run-time | class Solution:
def isPalindrome(self, s: str) -> bool:
s = ''.join(filter(str.isalnum, s))
s = s.lower()
return s == s[::-1] | valid-palindrome | Python solution beats 98.59% on run-time | sajid36 | 2 | 174 | valid palindrome | 125 | 0.437 | Easy | 1,389 |
https://leetcode.com/problems/valid-palindrome/discuss/1534623/Python-96%2B%2B-Faster-Solution-36ms | class Solution:
def isPalindrome(self, s: str) -> bool:
p = ""
for i in s.lower():
if i.isalnum():
p += i
else:
return p == p[::-1] | valid-palindrome | Python 96%++ Faster Solution-- 36ms | aaffriya | 2 | 314 | valid palindrome | 125 | 0.437 | Easy | 1,390 |
https://leetcode.com/problems/valid-palindrome/discuss/1331510/Python-easy-to-understand-short-solution | class Solution:
def isPalindrome(self, s: str) -> bool:
res = "".join(i.lower() for i in s if i.isalpha() or i.isnumeric())
return res == res[::-1] | valid-palindrome | Python easy-to-understand short solution | zedginidze | 2 | 218 | valid palindrome | 125 | 0.437 | Easy | 1,391 |
https://leetcode.com/problems/valid-palindrome/discuss/1311722/Python3-or-Faster-than-98.51-or-Using-alnum-and-lower | class Solution:
def isPalindrome(self, s: str) -> bool:
s = s.lower()
s = ''.join([x for x in s if x.isalnum()])
return s==s[::-1] | valid-palindrome | Python3 | Faster than 98.51% | Using alnum and lower | nishikantsinha | 2 | 127 | valid palindrome | 125 | 0.437 | Easy | 1,392 |
https://leetcode.com/problems/valid-palindrome/discuss/1122654/Python-solution-beats-99.9 | class Solution:
def isPalindrome(self, s: str) -> bool:
s = "".join( filter(str.isalnum, s.upper()) )
return (0,1)[s == s[::-1]] | valid-palindrome | Python solution beats 99.9% | malleswari1593 | 2 | 270 | valid palindrome | 125 | 0.437 | Easy | 1,393 |
https://leetcode.com/problems/valid-palindrome/discuss/503932/Python-2-pointers | class Solution:
def isPalindrome(self, s: str) -> bool:
p1 = 0
p2 = len(s)-1
while p1 < p2:
while p1 < p2 and not s[p1].isalnum():
p1 += 1
while p1 < p2 and not s[p2].isalnum():
p2 -= 1
if p1... | valid-palindrome | Python - 2 pointers | krion77 | 2 | 220 | valid palindrome | 125 | 0.437 | Easy | 1,394 |
https://leetcode.com/problems/valid-palindrome/discuss/2247140/Python3-Super-Easy-Solution-or-Efficient | class Solution:
def isPalindrome(self, s: str) -> bool:
s_out = ''
for char in s:
if char.isalnum():
s_out += char.lower()
return s_out == s_out[::-1] | valid-palindrome | ✅Python3 - Super Easy Solution | Efficient | thesauravs | 1 | 87 | valid palindrome | 125 | 0.437 | Easy | 1,395 |
https://leetcode.com/problems/valid-palindrome/discuss/2087958/Python3-O(N)-oror-O(N)-and-O(N)-oror-O(1)-solution | class Solution:
def isPalindrome(self, string: str) -> bool:
# return self.isPalindromeByList(string)
return self.isPalindromeOptimal(string)
# O(n) || O(1); worse: runtime: O(n^2)
# 106ms 11.29%
def isPalindromeOptimal(self, string):
if not string:
return True
... | valid-palindrome | Python3 O(N) || O(N) and O(N) || O(1) solution | arshergon | 1 | 120 | valid palindrome | 125 | 0.437 | Easy | 1,396 |
https://leetcode.com/problems/valid-palindrome/discuss/2047838/Python3-simple-solution-with-very-few-lines-of-code! | class Solution:
def isPalindrome(self, s: str) -> bool:
s1 = []
for key in s:
if key.isalpha() or key.isdigit():
s1.append(key.lower())
return s1 == s1[::-1] | valid-palindrome | Python3 simple solution with very few lines of code! | ji3010 | 1 | 203 | valid palindrome | 125 | 0.437 | Easy | 1,397 |
https://leetcode.com/problems/valid-palindrome/discuss/1762054/Python-solution | class Solution:
def isPalindrome(self, s: str) -> bool:
str1 = ""
str2 = ""
for i in s.lower():
if i.isalnum():
str1 = i + str1
if i.isalpha() or i.isdigit():
str2 += i
if str1 == str2:
return True
... | valid-palindrome | Python solution | payalsasmal | 1 | 92 | valid palindrome | 125 | 0.437 | Easy | 1,398 |
https://leetcode.com/problems/valid-palindrome/discuss/1762054/Python-solution | class Solution:
def alphanumeric(self, st):
if ((ord(st)>=48 and ord(st)<=57) or (ord(st) >= 65 and ord(st) <= 90) or (ord(st) >=97 and ord(st) <= 122)):
return True
return False
def isPalindrome(self, s: str) -> bool:
str1 = ""
str2 = ""
for i in s.... | valid-palindrome | Python solution | payalsasmal | 1 | 92 | valid palindrome | 125 | 0.437 | Easy | 1,399 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.