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/palindrome-number/discuss/2398263/9.-Palindrome-Number-solution-in-3-ways
|
class Solution:
def isPalindrome(self, x: int) -> bool:
x = str(x)
length = len(x)
l = int(length/2)
cnt = 0
for i in range(l):
if(x[i]==x[length-1-i]):
cnt += 1
else:
break
return cnt==l
|
palindrome-number
|
9. Palindrome Number solution in 3 ways
|
e-mm-u
| 1 | 364 |
palindrome number
| 9 | 0.53 |
Easy
| 400 |
https://leetcode.com/problems/palindrome-number/discuss/2398263/9.-Palindrome-Number-solution-in-3-ways
|
class Solution:
def isPalindrome(self, x: int) -> bool:
strX = str(x)
revStrX = strX[::-1]
return strX==revStrX[::-1]
|
palindrome-number
|
9. Palindrome Number solution in 3 ways
|
e-mm-u
| 1 | 364 |
palindrome number
| 9 | 0.53 |
Easy
| 401 |
https://leetcode.com/problems/palindrome-number/discuss/2398263/9.-Palindrome-Number-solution-in-3-ways
|
class Solution:
def isPalindrome(self, x: int) -> bool:
strX = str(x)
return strX==strX[::-1]
|
palindrome-number
|
9. Palindrome Number solution in 3 ways
|
e-mm-u
| 1 | 364 |
palindrome number
| 9 | 0.53 |
Easy
| 402 |
https://leetcode.com/problems/palindrome-number/discuss/2375083/90-99-fastest-solution-in-Python-to-the-Palindrome-problem
|
class Solution(object):
def isPalindrome(self,x):
return str(x) == str(x)[::-1]
|
palindrome-number
|
90-99% fastest solution in Python to the Palindrome problem
|
slzr_
| 1 | 187 |
palindrome number
| 9 | 0.53 |
Easy
| 403 |
https://leetcode.com/problems/palindrome-number/discuss/2319721/Python-one-liner
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]
|
palindrome-number
|
Python one liner
|
Balance-Coffee
| 1 | 77 |
palindrome number
| 9 | 0.53 |
Easy
| 404 |
https://leetcode.com/problems/palindrome-number/discuss/2259801/python3
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]
|
palindrome-number
|
python3
|
kobatatsu0514
| 1 | 175 |
palindrome number
| 9 | 0.53 |
Easy
| 405 |
https://leetcode.com/problems/palindrome-number/discuss/1914490/Python3-one-line-accepted-solution
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return [a for a in str(x)] == [a for a in str(x)][::-1]
|
palindrome-number
|
Python3 one line accepted solution
|
ElizaZoldyck
| 1 | 45 |
palindrome number
| 9 | 0.53 |
Easy
| 406 |
https://leetcode.com/problems/palindrome-number/discuss/1678221/Easy-10-line-python-code
|
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
num = x
reverse = 0
while(num):
reverse = reverse * 10 + num % 10 #taking the reverse of the number
num //= 10
return x == reverse
|
palindrome-number
|
Easy 10 line python code
|
Unexpected_Error
| 1 | 281 |
palindrome number
| 9 | 0.53 |
Easy
| 407 |
https://leetcode.com/problems/palindrome-number/discuss/1475543/Python3-oror-Simple-and-Correct-Solution
|
class Solution:
def isPalindrome(self, x: int) -> bool:
if x<0:
return False
else:
result = 0
dummy = x
while x!=0:
result = result*10 + x%10
x = x//10
return result == dummy
|
palindrome-number
|
Python3 || Simple and Correct Solution
|
ajinkya2021
| 1 | 215 |
palindrome number
| 9 | 0.53 |
Easy
| 408 |
https://leetcode.com/problems/palindrome-number/discuss/1373902/Python-89.99-faster
|
class Solution:
def isPalindrome(self, x: int) -> bool:
if x >= 0:
x = str(x)
if x == x[::-1]:
return True
return False
|
palindrome-number
|
Python 89.99% faster
|
SnakeUser
| 1 | 234 |
palindrome number
| 9 | 0.53 |
Easy
| 409 |
https://leetcode.com/problems/palindrome-number/discuss/1314676/Easy-Solution_-Python-3
|
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False # Negative number will not be Palindrome Number.
else:
rev = int(str(x)[::-1]) # Revise the number.
return rev == x # Compare the input number is equal to the revised number or not.
|
palindrome-number
|
Easy Solution_ Python 3
|
An_222
| 1 | 135 |
palindrome number
| 9 | 0.53 |
Easy
| 410 |
https://leetcode.com/problems/palindrome-number/discuss/824689/Python-One-Line-O(N)-oror-Easy
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]
# TC: O(N)
# SC: O(1)
|
palindrome-number
|
Python One Line O(N) || Easy
|
airksh
| 1 | 89 |
palindrome number
| 9 | 0.53 |
Easy
| 411 |
https://leetcode.com/problems/palindrome-number/discuss/445882/Python3-via-str()-99.74-faster-100.00-less-memory
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1] if x >= 0 else False
|
palindrome-number
|
Python3 via str() 99.74% faster, 100.00% less memory
|
eQualo
| 1 | 339 |
palindrome number
| 9 | 0.53 |
Easy
| 412 |
https://leetcode.com/problems/palindrome-number/discuss/2846729/ProCode-from-palindrome
|
class Solution:
def isPalindrome(self, x: int) -> bool:
x=str(x)
if x==x[::-1]:
return True
else:
return False
|
palindrome-number
|
ProCode from palindrome
|
AhensInitiative
| 0 | 2 |
palindrome number
| 9 | 0.53 |
Easy
| 413 |
https://leetcode.com/problems/palindrome-number/discuss/2844948/it-works
|
class Solution:
def isPalindrome(self, x: int) -> bool:
x=str(x)
if x==x[::-1]:
return True
else:
return False
|
palindrome-number
|
it works
|
venkatesh402
| 0 | 1 |
palindrome number
| 9 | 0.53 |
Easy
| 414 |
https://leetcode.com/problems/palindrome-number/discuss/2841490/Beginner-friendly-logic-(Python3)
|
class Solution:
def isPalindrome(self, x: int) -> bool:
originalList = []
palindromeList = []
xString = str(x)
for i in range(len(xString)):
element = xString[i]
originalList.append(element)
palindromeList.append(element)
palindromeList.reverse()
m = len(originalList)
checkList = []
for t in range(m):
if originalList[t] == palindromeList[t]:
checkList.append('T')
continue
else:
return False
if len(checkList) == m:
return True
|
palindrome-number
|
Beginner friendly logic (Python3)
|
hmrealistic
| 0 | 5 |
palindrome number
| 9 | 0.53 |
Easy
| 415 |
https://leetcode.com/problems/palindrome-number/discuss/2841194/palindrome-number
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x)==str(x)[::-1]
|
palindrome-number
|
palindrome number
|
bhalke_216
| 0 | 1 |
palindrome number
| 9 | 0.53 |
Easy
| 416 |
https://leetcode.com/problems/palindrome-number/discuss/2840495/Simple-Solution
|
class Solution:
def isPalindrome(self, x: int) -> bool:
if str(x) == str(x)[::-1]:
return True
else:
return False
|
palindrome-number
|
Simple Solution
|
Haz03
| 0 | 2 |
palindrome number
| 9 | 0.53 |
Easy
| 417 |
https://leetcode.com/problems/palindrome-number/discuss/2839014/Python-one-line
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]
|
palindrome-number
|
Python one line
|
samurai2
| 0 | 1 |
palindrome number
| 9 | 0.53 |
Easy
| 418 |
https://leetcode.com/problems/palindrome-number/discuss/2838247/Mathematical-solution
|
class Solution:
def isPalindrome(self, x: int) -> bool:
x1 = x
if x < 0:
return False
sum = 0
while x != 0:
rev = x%10 #last digit of integer goes to rev
sum = (sum * 10) + rev # multiplication of 10 here gives 0 to last digit of number after that adding rev value to 0.
x = x//10
if x1==sum:
return True
else:
return False
|
palindrome-number
|
Mathematical solution
|
viveksingh17
| 0 | 1 |
palindrome number
| 9 | 0.53 |
Easy
| 419 |
https://leetcode.com/problems/palindrome-number/discuss/2834950/Easy-way-to-slove
|
class Solution:
def isPalindrome(self, x: int) -> bool:
a=str(x)
s=int(len(a)/2)
if len(a)%2==1:
for i in range(0,s):
if a[i] != a[len(a)-1-i]:
return False
return True
elif len(a)%2==0:
for i in range(0,s):
if a[i] != a[len(a)-1-i]:
return False
return True
|
palindrome-number
|
Easy way to slove
|
NUUC_111356040
| 0 | 2 |
palindrome number
| 9 | 0.53 |
Easy
| 420 |
https://leetcode.com/problems/palindrome-number/discuss/2833114/Python-3-solution-using-slice
|
class Solution:
def isPalindrome(self, x: int) -> bool:
x2 = str(x)
x3 = x2[::-1]
if x3 == x2:
return True
return False
|
palindrome-number
|
Python 3 solution using slice
|
woxp
| 0 | 4 |
palindrome number
| 9 | 0.53 |
Easy
| 421 |
https://leetcode.com/problems/palindrome-number/discuss/2832577/Python-Easy-One-Line-Solution
|
class Solution:
def isPalindrome(self, x: int) -> bool:
return str(x) == str(x)[::-1]
|
palindrome-number
|
Python - Easy One-Line Solution
|
james_tf
| 0 | 2 |
palindrome number
| 9 | 0.53 |
Easy
| 422 |
https://leetcode.com/problems/palindrome-number/discuss/2830485/Simple-Solution
|
class Solution:
def isPalindrome(self, x: int) -> bool:
num_str = str(x)
size = len(num_str)
reversed_num = num_str[size::-1]
if str(x) == reversed_num:
return True
else:
return False
|
palindrome-number
|
Simple Solution
|
torzin
| 0 | 3 |
palindrome number
| 9 | 0.53 |
Easy
| 423 |
https://leetcode.com/problems/palindrome-number/discuss/2828822/List-and-Reversed
|
class Solution:
def isPalindrome(self, x: int) -> bool:
y = list(str(x))
return y== list(reversed(y))
|
palindrome-number
|
List and Reversed
|
user5687us
| 0 | 3 |
palindrome number
| 9 | 0.53 |
Easy
| 424 |
https://leetcode.com/problems/regular-expression-matching/discuss/2383634/Fastest-Solution-Explained0ms100-O(n)time-complexity-O(n)space-complexity
|
class Solution:
def isMatch(self, s, p):
n = len(s)
m = len(p)
dp = [[False for _ in range (m+1)] for _ in range (n+1)]
dp[0][0] = True
for c in range(1,m+1):
if p[c-1] == '*' and c > 1:
dp[0][c] = dp[0][c-2]
for r in range(1,n+1):
for c in range(1,m+1):
if p[c-1] == s[r-1] or p[c-1] == '.':
dp[r][c] = dp[r-1][c-1]
elif c > 1 and p[c-1] == '*':
if p[c-2] =='.' or s[r-1]==p[c-2]:
dp[r][c] =dp[r][c-2] or dp[r-1][c]
else:
dp[r][c] = dp[r][c-2]
return dp[n][m]
|
regular-expression-matching
|
[Fastest Solution Explained][0ms][100%] O(n)time complexity O(n)space complexity
|
cucerdariancatalin
| 10 | 1,300 |
regular expression matching
| 10 | 0.282 |
Hard
| 425 |
https://leetcode.com/problems/regular-expression-matching/discuss/366219/Python3dynamic-programming
|
class Solution:
def isMatch(self, s, p):
n = len(s)
m = len(p)
dp = [[False for _ in range (m+1)] for _ in range (n+1)]
dp[0][0] = True
for c in range(1,m+1):
if p[c-1] == '*' and c > 1:
dp[0][c] = dp[0][c-2]
for r in range(1,n+1):
for c in range(1,m+1):
if p[c-1] == s[r-1] or p[c-1] == '.':
dp[r][c] = dp[r-1][c-1]
elif c > 1 and p[c-1] == '*':
if p[c-2] =='.' or s[r-1]==p[c-2]:
dp[r][c] =dp[r][c-2] or dp[r-1][c]
else:
dp[r][c] = dp[r][c-2]
return dp[n][m]
|
regular-expression-matching
|
[Python3]dynamic programming
|
zhanweiting
| 7 | 1,000 |
regular expression matching
| 10 | 0.282 |
Hard
| 426 |
https://leetcode.com/problems/regular-expression-matching/discuss/2532721/Dynamic-programming-or-Python
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
n_s=len(s)
n_p=len(p)
dp=[[False]*(n_p+1) for _ in range(n_s+1)]
dp[0][0]=True
#For empty string but the "*" in pattern might return True
for i in range(1,n_p+1):
if p[i-1]=="*":
dp[0][i]=dp[0][i-2]
for i in range(1,n_s+1):
for j in range(1,n_p+1):
#When the character in string matches with the patter or the pattern has '.', which accepts any character
if s[i-1]==p[j-1] or p[j-1]=='.':
dp[i][j]=dp[i-1][j-1]
#When the pattern has "*", this shows that we need to check the [j-2] for the character, which can be the string character or '.'. In this case we will check the [i-1][j], to check if the character except the current one is True.
elif p[j-1]=="*":
dp[i][j]=dp[i][j-2]
if p[j-2]=='.' or p[j-2]==s[i-1]:
dp[i][j]=dp[i][j] or dp[i-1][j]
return dp[n_s][n_p]
|
regular-expression-matching
|
Dynamic programming | Python
|
ankush_A2U8C
| 4 | 343 |
regular expression matching
| 10 | 0.282 |
Hard
| 427 |
https://leetcode.com/problems/regular-expression-matching/discuss/2392690/Python-95-faster
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
dp = {}
def dfs(i,j):
if (i,j) in dp:
return dp[(i,j)]
if i>=len(s) and j>=len(p):
return True
if i<= len(s) and j>= len(p):
return False
match = i< len(s) and ((s[i] == p[j]) or (p[j] == '.'))
if j < len(p)-1 and p[j+1] == '*':
dp[(i,j)] = ( dfs(i,j+2) # dont use *
or (match and dfs(i+1,j)) )
return dp[(i,j)]
if match:
dp[(i,j)] = dfs(i+1,j+1)
return dp[(i,j)]
return dfs(0,0)
|
regular-expression-matching
|
Python 95% faster
|
Abhi_009
| 2 | 446 |
regular expression matching
| 10 | 0.282 |
Hard
| 428 |
https://leetcode.com/problems/regular-expression-matching/discuss/2199084/python-3-or-memoization
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m, n = len(s), len(p)
@lru_cache(None)
def helper(i, j):
if j == n:
return i == m
match = i != m and (p[j] == '.' or s[i] == p[j])
if j == n - 1 or p[j + 1] != '*':
return match and helper(i + 1, j + 1)
return helper(i, j + 2) or (match and helper(i + 1, j))
return helper(0, 0)
|
regular-expression-matching
|
python 3 | memoization
|
dereky4
| 2 | 287 |
regular expression matching
| 10 | 0.282 |
Hard
| 429 |
https://leetcode.com/problems/regular-expression-matching/discuss/1270958/python3-can-I-use-regex
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
# when pattern(p) is "", check with string(s)
if not p:
return s == p
# when the last char of pattern(p) is *
if p[-1] == "*":
# *=0,the char before * is repeated 0 times
if self.isMatch(s, p[: -2]):
return True
# when string(s) is "" and pattern(p) has chars
if not s:
return False
# delete once repeated times
if p[-2] == "." or p[-2] == s[-1]:
return self.isMatch(s[: -1], p)
# when string(s) is "" and pattern(p) has chars
if not s:
return False
# delete last repeated cher
if p[-1] == "." or p[-1] == s[-1]:
return self.isMatch(s[: -1], p[: -1])
|
regular-expression-matching
|
python3 can I use regex?
|
haoIfrit
| 1 | 123 |
regular expression matching
| 10 | 0.282 |
Hard
| 430 |
https://leetcode.com/problems/regular-expression-matching/discuss/1193646/Could-this-be-the-shortest-and-fastest-Python-Solution
|
class Solution:
@lru_cache
def isMatch(self, s, p):
if not p: return s == p
if p[-1] == '*':
if self.isMatch(s, p[:-2]): return True
if s and (s[-1] == p[-2] or p[-2] == '.') and self.isMatch(s[:-1], p): return True
return s and (p[-1] == s[-1] or p[-1] == '.') and self.isMatch(s[:-1], p[:-1])
|
regular-expression-matching
|
Could this be the shortest and fastest Python Solution?
|
A88888
| 1 | 409 |
regular expression matching
| 10 | 0.282 |
Hard
| 431 |
https://leetcode.com/problems/regular-expression-matching/discuss/2843241/Straightforward-recursive-solution-in-Python
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
while not (s=='' and p==''):
if len(p) >= 2:
if p[:2] == '.*':
for m in range(len(s)+1):
if self.isMatch(s[m:], p[2:]):
return True
return False
elif p[1] == '*':
for m in range(len(s)+1):
if s[:m] != p[0] * m:
return False
if self.isMatch(s[m:], p[2:]):
return True
return False
if s == '' or p == '':
return False
if p[0].isalnum():
if p[0] != s[0]:
return False
elif p[0] == '.':
pass
else:
return False
p = p[1:]
s = s[1:]
else:
return True
|
regular-expression-matching
|
Straightforward recursive solution in Python
|
clavicordio
| 0 | 4 |
regular expression matching
| 10 | 0.282 |
Hard
| 432 |
https://leetcode.com/problems/regular-expression-matching/discuss/2841578/PYTHON-3-Part-by-Part-Explained-Solution-beginner-friendly-%3A)
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
def helper(s, p):
for i, j in zip(s, p):
if j != "." and i != j:
return False
return True if len(s) == len(p) else False
def starc(s, p, x=0, y=0):
key = y
while x < len(p) and y < len(s):
if len(p[x]) == 2:
if s[y] == p[x][0] or p[x][0] == ".":
y += 1
elif s[y] != p[x][0]:
x += 1
else:
break
return True if y - key == len(s) else False
if "*" in p:
memo = []
cut = ""
for x in range(len(p)):
if p[x] == "*":
if any(cut[:-1]):
memo.append([cut[:-1]])
memo.append(cut[-1]+"*")
cut = ""
elif p[x] != "*":
cut = cut + p[x]
if any(cut): memo.append([cut])
for i in range(len(memo)):
if type(memo[i]) == list:
y = 0
for v in memo:
if type(v) == list:
if len(v) > 1:
y = v[1] + len(v[0])
g = sum((len(x[0]) for x in memo[i + 1:] if type(x) == list))
while y + len(memo[i][0]) <= len(s) - g:
if helper(s[y: y + len(memo[i][0])], memo[i][0]):
memo[i] = memo[i] + [y]
y += 1
if len(memo[i]) == 1:
return False
if not any([x for x in memo if type(x)==list and len(x)>1]):
return starc(s,memo)
must = [x for x in range(len(memo)) if type(memo[x])==list]
place = []
for z in must:
if not any(place):
for i in memo[z][1:]:
place.append([i])
else:
cut = len(place)
for k in range(len(place)):
a = place[k]
for i in memo[z][1:]:
place.append(a+[i])
place = place[cut:]
for cp in place:
if len(set(cp)) != len(cp):
continue
d = 0
s2 = s
howcut = []
while d < len(cp):
if d < len(cp)-1:
if cp[d] + len(memo[must[d]][0]) > cp[d+1]: break
back = []
for e in memo[:must[d]][::-1]:
if type(e) == str:
back.append(e)
else:
break
cc = 0
if any(howcut):
cc = sum(howcut)
if starc(s2[:cp[d]-cc], back[::-1]):
howcut.append(len(s2[:cp[d]-cc + len(memo[must[d]][0])]))
s2 = s2[(cp[d]-cc) + len(memo[must[d]][0]):]
if any(s2):
d += 1
continue
else:
break
else:
break
elif d == len(cp) -1:
back = []
for e in memo[:must[d]][::-1]:
if type(e) == str:
back.append(e)
else:
break
cc = 0
if any(howcut):
cc = sum(howcut)
if starc(s2[:cp[d]-cc], back[::-1]):
howcut.append(len(s2[:cp[d]-cc + len(memo[must[d]][0])]))
s2 = s2[(cp[d]-cc) + len(memo[must[d]][0]):]
front = []
for e in memo[must[d]+1:]:
if type(e) == str:
front.append(e)
else:
break
if starc(s2, front):
return True
else:
break
else:
break
return False
else:
return helper(s,p)
|
regular-expression-matching
|
PYTHON 3 Part by Part Explained Solution beginner friendly :)
|
BladeRunner61
| 0 | 6 |
regular expression matching
| 10 | 0.282 |
Hard
| 433 |
https://leetcode.com/problems/regular-expression-matching/discuss/2824878/Python-solution-regex-xx
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
find = re.findall(p, s)
if not(len(find)) or (len(find) and find[0] != s):
return False
return True
|
regular-expression-matching
|
Python - solution - regex - xx
|
user6314wG
| 0 | 3 |
regular expression matching
| 10 | 0.282 |
Hard
| 434 |
https://leetcode.com/problems/regular-expression-matching/discuss/2806456/Python3-Backtracking-%2B-Stack-(83ms)
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
n, m = len(s), len(p)
i, j = n-1, m-1
wildcard_backtracking = []
while i >=0:
# single char case
if j >= 0 and (p[j] == '.' or p[j] == s[i]):
i-=1
j-=1
# zero or more case, start from zero precending
elif j >= 0 and p[j] == '*':
if p[j-1] == '.' and j == 1:
return True
else:
j-=2
wildcard_backtracking.append((p[j+1], j,i))
# not match, backtracking
elif len(wildcard_backtracking) > 0:
p_back, j_back, i_back = wildcard_backtracking.pop()
i, j = i_back, j_back
if p_back == '.' or p_back == s[i]:
i-=1
wildcard_backtracking.append((p_back, j_back, i))
else:
return False
# remove all matched zero or more case
while j >= 0 and p[j] == '*':
j-=2
return i == -1 and j == -1
|
regular-expression-matching
|
[Python3] Backtracking + Stack (83ms)
|
user4722lM
| 0 | 4 |
regular expression matching
| 10 | 0.282 |
Hard
| 435 |
https://leetcode.com/problems/regular-expression-matching/discuss/2581890/python3-one-command
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
return re.fullmatch(p, s)
|
regular-expression-matching
|
python3 one command
|
eazykaye
| 0 | 93 |
regular expression matching
| 10 | 0.282 |
Hard
| 436 |
https://leetcode.com/problems/regular-expression-matching/discuss/2192499/Python-easy-to-read-and-understand-or-DP
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
n, m = len(s), len(p)
t = [[-1 for _ in range(n+1)] for _ in range(m+1)]
t[0][0] = True
for j in range(1, n+1):
t[0][j] = False
for i in range(1, m+1):
if p[i-1] == '*':
t[i][0] = t[i-2][0]
else:
t[i][0] = False
for i in range(1, m+1):
for j in range(1, n+1):
if p[i-1].isalpha():
if p[i-1] == s[j-1]:
t[i][j] = t[i-1][j-1]
else:
t[i][j] = False
elif p[i-1] == '.':
t[i][j] = t[i-1][j-1]
elif p[i-1] == '*':
if p[i-2] == s[j-1] or p[i-2] == '.':
t[i][j] = t[i-2][j] or t[i][j-1]
else:
t[i][j] = t[i-2][j]
return t[m][n]
|
regular-expression-matching
|
Python easy to read and understand | DP
|
sanial2001
| 0 | 267 |
regular expression matching
| 10 | 0.282 |
Hard
| 437 |
https://leetcode.com/problems/regular-expression-matching/discuss/2030811/python3-linked-list-of-sub-parsers.-Object-oriented-with-inheritance
|
class ParserNode:
def __init__(self, nxt=None):
self.nxt = nxt
def __str__(self): pass
#using "generator" since a ParserNode may consume a variety of combinations from p,
#this is basic form
def genparse(self, s):
if s and self.match(s[0]):
yield s[1:]
def parsealong(self, s):
#print(self, s)
for s_ in self.genparse(s):
#success: s is consumed (s_ == []) and no nxt
if not s_ and not self.nxt:
return True
#success: any downstream ParserNodes find success
if self.nxt and self.nxt.parsealong(s_):
return True
return False
####################################
class DotParserNode(ParserNode):
def __str__(self):
return f"DotParserNode => {self.nxt}"
def match(self, ch):
return True
####################################
class CharParserNode(ParserNode):
def __init__(self, ch):
ParserNode.__init__(self)
self.ch = ch
def __str__(self):
return f"CharParserNode({self.ch}) => {self.nxt}"
def match(self, ch):
return self.ch == ch
####################################
class StarParserNode(ParserNode):
def __init__(self, prev):
ParserNode.__init__(self)
self.prev = prev
def __str__(self):
return f"StarParserNode({self.prev}) => {self.nxt}"
def match(self, ch):
if self.prev:
return self.prev.match(ch)
return False
def genparse(self, s):
#0
yield s
#1+
if s:
for i in range(len(s)):
if self.match(s[i]):
yield s[i+1:]
else:
break
####################################
class Solution:
def isMatch(self, s: str, p: str) -> bool:
p = list(p)
#helper function to get parserNode from current, next character pairs
def parserNodeFactory(c, n):
if n == '*':
return StarParserNode(parserNodeFactory(c, None))
if c == '*':
return None
if c == '.':
return DotParserNode()
return CharParserNode(c)
#build linked list of ParserNodes from p
head, tail = None, None
if len(p) == 1:
head = parserNodeFactory(p[0], None)
else:
itr = zip(p, p[1:] + [None])
head = parserNodeFactory(*next(itr))
tail = head
for c, n in itr:
node = parserNodeFactory(c, n)
if node:
tail.nxt = node
tail = tail.nxt
return head.parsealong(s)
####################################
|
regular-expression-matching
|
python3, linked list of sub-parsers. Object-oriented with inheritance
|
cunning_potato
| 0 | 56 |
regular expression matching
| 10 | 0.282 |
Hard
| 438 |
https://leetcode.com/problems/regular-expression-matching/discuss/1924897/Python3-Solution-beats-90%3A-Sequential-Verification
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
arr = []
i = 0
while i < len(p):
if i+1 < len(p) and p[i+1] == '*':
arr.append(p[i]+'*')
i+=2
else:
arr.append(p[i])
i += 1
verified = set([-1])
def verify(idx, op,s):
ans = set()
if idx <= len(s):
if '*' not in op and idx < len(s) and (s[idx] == op or op == '.'):
ans.add(idx)
elif '*' in op:
ans.add(idx-1)
for i in range(idx,len(s)):
if op[0] == '.' or s[i] == op[0]:
ans.add(i)
else:
break
return ans
for vLevel in range(len(arr)):
newVerified = set()
for v in verified:
newVerified |= verify(v+1,arr[vLevel],s)
verified = newVerified
return len(s)-1 in verified
|
regular-expression-matching
|
Python3 Solution beats 90%: Sequential Verification
|
johnnyvessey
| 0 | 127 |
regular expression matching
| 10 | 0.282 |
Hard
| 439 |
https://leetcode.com/problems/regular-expression-matching/discuss/1872215/Python-solution-using-built-in-functions
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
regex = re.compile(p)
if regex.fullmatch(s):
return True
return False
|
regular-expression-matching
|
Python solution using built-in functions
|
alishak1999
| 0 | 234 |
regular expression matching
| 10 | 0.282 |
Hard
| 440 |
https://leetcode.com/problems/regular-expression-matching/discuss/1787376/Python-DP-Solution-O(m*n)-time-O(n)-space
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
n = len(p)
dp = [False] * (n + 1)
prev = True
# pretreat dp for p like "a*b*ccc*", dp[2] and dp[4] ought to be true.
for j in range(2, n + 1, 2):
if p[j - 1] != '*':
break
dp[j] = True
for i in range(len(s)):
for j in range(1, n + 1):
temp = dp[j] # to store dp[i-1][j-1] (in the original big dp matrix)
if p[j - 1] == '*':
dp[j] = dp[j - 2] or dp[j - 1] or (
dp[j] and (s[i] == p[j - 2] or p[j - 2] == '.'))
else:
dp[j] = prev and (p[j - 1] == '.' or s[i] == p[j - 1])
prev = temp
prev = False # dp[i][0] should be false (in the original big dp matrix)
return dp[n]
|
regular-expression-matching
|
Python DP Solution, O(m*n) time, O(n) space
|
celestez
| 0 | 292 |
regular expression matching
| 10 | 0.282 |
Hard
| 441 |
https://leetcode.com/problems/regular-expression-matching/discuss/1659103/Python3-Top-Down-2D-DP-Limit-Recursion-93-runtime-91-Memory
|
class Solution(object):
def isMatch(self, s, p):
# To prevent hitting max recursion limit, iterate over list of test cases.
# This limits recursive calls to isMatch to only conditions when the
# entire string or entire pattern has been consumed.
test_cases = [[s, p]]
while len(test_cases) > 0:
s, p = test_cases.pop()
sl = len(s)
pl = len(p)
# Check success/failure conditions
if sl == 0 and pl == 0:
return True
elif pl == 0:
return False
elif sl == 0 and p[-1] == '*':
return self.isMatch(s, p[:-2])
elif sl == 0:
return False
# Iterate over the string and pattern
while len(s) > 0 and len(p) > 0:
# Handle wildcards
if p[-1] == '*':
# When wildcard character matches, add test cases for single-character
# matches and multi-character matches
if p[-2] == s[-1] or p[-2] == '.':
test_cases.append([s, p[:-1]])
test_cases.append([s[:-1], p])
# Whether the wildcard character matches or not, always check for
# zero-character matches
s, p = s, p[:-2]
# For non-wildcard matches, consume the last character of
# the pattern and the string
elif s[-1] == p[-1] or p[-1] == '.':
s = s[:-1]
p = p[:-1]
# For non-wildcard mismatches, terminate this test case
else:
s, p = '0', ''
# Once either the pattern or string has been consumed,
# recheck success/failure conditions. On failure, move
# on to next test case
if self.isMatch(s, p):
return True
# If loop is exited with no successful test cases, return False
return False
|
regular-expression-matching
|
[Python3] Top-Down 2D DP, Limit Recursion, 93% runtime, 91% Memory
|
nrscer
| 0 | 139 |
regular expression matching
| 10 | 0.282 |
Hard
| 442 |
https://leetcode.com/problems/regular-expression-matching/discuss/1650354/Use-a-FSM-(finite-state-machine)-to-find-the-solution-(faster-than-99)
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
next_= defaultdict(list)
state=lastnostar= 0
for c,nc in zip(p,p[1:]+" "):
if c == "*":
continue
state+= 1
for i in range(lastnostar,state):
next_[(i,c)].append(state)
if nc != "*":
lastnostar= state
else:
next_[(state,c)].append(state)
states, laststate= set([0]), state
for c in s:
states= {nextstate
for state in states
for symbol in (c,".")
if (state,symbol) in next_
for nextstate in next_[(state,symbol)]
}
if not states:
return False
return any(i in states for i in range(lastnostar,laststate+1))
|
regular-expression-matching
|
Use a FSM (finite-state machine) to find the solution (faster than 99%)
|
pknoe3lh
| 0 | 188 |
regular expression matching
| 10 | 0.282 |
Hard
| 443 |
https://leetcode.com/problems/regular-expression-matching/discuss/1149851/python-3-regex
|
class Solution:
def isMatch(self, s: str, p: str) -> bool:
import re
x=re.match(p,s)
if not x:
return False
if x.start()==0 and x.end()==len(s):
return True
return False
|
regular-expression-matching
|
python 3 regex
|
janhaviborde23
| 0 | 120 |
regular expression matching
| 10 | 0.282 |
Hard
| 444 |
https://leetcode.com/problems/regular-expression-matching/discuss/1173232/Could-this-be-the-shortest-python-solution
|
class Solution:
@lru_cache
def isMatch(self, s, p):
if not p: return not s
if p[-1] == '*':
return (self.isMatch(s, p[:-2])) or (s and (s[-1] == p[-2] or p[-2] == '.') and self.isMatch(s[:-1], p))
return s and (p[-1] == s[-1] or p[-1] == '.') and self.isMatch(s[:-1], p[:-1])
|
regular-expression-matching
|
Could this be the shortest python solution?
|
A88888
| -1 | 244 |
regular expression matching
| 10 | 0.282 |
Hard
| 445 |
https://leetcode.com/problems/container-with-most-water/discuss/1915108/Python3-GREEDY-TWO-POINTERS-~(~)-Explained
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r, area = 0, len(height) - 1, 0
while l < r:
area = max(area, (r - l) * min(height[l], height[r]))
if height[l] < height[r]:
l += 1
else:
r -= 1
return area
|
container-with-most-water
|
✔️ [Python3] GREEDY TWO POINTERS ~(˘▾˘~), Explained
|
artod
| 133 | 9,600 |
container with most water
| 11 | 0.543 |
Medium
| 446 |
https://leetcode.com/problems/container-with-most-water/discuss/1038263/Python-Solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l = 0
r = len(height)-1
res = 0
while l < r:
area = (r - l) * min(height[l], height[r])
res = max(area,res)
if height[l]<height[r]:
l = l+1
else:
r = r-1
return res
|
container-with-most-water
|
Python Solution
|
UttasargaSingh
| 18 | 3,000 |
container with most water
| 11 | 0.543 |
Medium
| 447 |
https://leetcode.com/problems/container-with-most-water/discuss/2803384/Python-or-Easy-Solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
maxx = 0
i = 0
j = len(height)-1
while i < j:
width = abs(i-j)
area = width * min(height[i],height[j])
maxx = max(area,maxx)
if height[i] > height[j]:
j -=1
else:
i +=1
return maxx
|
container-with-most-water
|
Python | Easy Solution✔
|
manayathgeorgejames
| 17 | 1,500 |
container with most water
| 11 | 0.543 |
Medium
| 448 |
https://leetcode.com/problems/container-with-most-water/discuss/1633791/python-fast-simple-faster-than-92
|
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area, i, j = 0, 0, len(height)-1
while i != j:
if height[j] > height[i]:
area = height[i] * (j - i)
i += 1
else:
area = height[j] * (j - i)
j -= 1
max_area = max(max_area, area)
return max_area
|
container-with-most-water
|
python fast simple faster than 92%
|
fatmakahveci
| 6 | 485 |
container with most water
| 11 | 0.543 |
Medium
| 449 |
https://leetcode.com/problems/container-with-most-water/discuss/1296853/Python-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
ret = 0
left, right = 0, len(height)-1
while left < right:
ret = max(ret, (right-left) * min(height[left], height[right]))
if height[left] < height[right]:
left += 1
else:
right -= 1
return ret
|
container-with-most-water
|
Python solution
|
5tigerjelly
| 5 | 310 |
container with most water
| 11 | 0.543 |
Medium
| 450 |
https://leetcode.com/problems/container-with-most-water/discuss/2716014/Python-Simple-Two-Pointer-Approach
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height)-1
res = 0
while l < r:
area = min(height[l], height[r]) * (r-l)
res = max(res, area)
if height[l] >= height[r]:
r -= 1
else:
l += 1
return res
|
container-with-most-water
|
Python Simple Two Pointer Approach
|
jonathanbrophy47
| 4 | 806 |
container with most water
| 11 | 0.543 |
Medium
| 451 |
https://leetcode.com/problems/container-with-most-water/discuss/1454052/Python3-two-pointers-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height)-1
result = 0
while left < right:
result = max(result, min(height[left],height[right])*(right-left))
if height[left] < height[right]: left += 1
else: right -= 1
return result
|
container-with-most-water
|
Python3 two-pointers solution
|
Janetcxy
| 4 | 442 |
container with most water
| 11 | 0.543 |
Medium
| 452 |
https://leetcode.com/problems/container-with-most-water/discuss/330811/Simple-Python-3-Solution-Faster-than-99.8
|
class Solution:
def maxArea(self, height: List[int]) -> int:
h = height
A = 0
i, j = 0, len(h)-1
while j-i > 0:
if h[i] < h[j]:
a = (j-i)*h[i]
i += 1
else:
a = (j-i)*h[j]
j -= 1
if a > A:
A = a
return A
- Python 3
|
container-with-most-water
|
Simple Python 3 Solution - Faster than 99.8%
|
junaidmansuri
| 4 | 1,400 |
container with most water
| 11 | 0.543 |
Medium
| 453 |
https://leetcode.com/problems/container-with-most-water/discuss/2548410/Python-solution-using-two-pointers-and-current-max
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height)-1
cur_max = float('-inf')
while l < r:
cur_area = (r-l) * min(height[l], height[r])
cur_max = max(cur_max, cur_area)
if height[l] <= height[r]:
l+=1
else:
r-=1
return cur_max
|
container-with-most-water
|
📌 Python solution using two pointers and current max
|
croatoan
| 3 | 54 |
container with most water
| 11 | 0.543 |
Medium
| 454 |
https://leetcode.com/problems/container-with-most-water/discuss/2379254/Simple-and-easy-python3
|
class Solution:
def maxArea(self, height: List[int]) -> int:
res = 0
l, r = 0,len(height) - 1
while l < r:
res = max(res, min(height[l], height[r]) * (r - l))
if height[l] < height[r]:
l += 1
elif height[r] <= height[l]:
r -= 1
return res
|
container-with-most-water
|
Simple and easy python3
|
__Simamina__
| 3 | 136 |
container with most water
| 11 | 0.543 |
Medium
| 455 |
https://leetcode.com/problems/container-with-most-water/discuss/2332801/Easy-To-Understand-Python-Two-Pointers
|
class Solution:
def maxArea(self, height: List[int]) -> int:
area = 0
l,r= 0,len(height)-1
while l<r:
width_height = (r-l) * min(height[l],height[r]) #Compute the current area.
area = width_height if width_height > area else area #Compare area with previous computed area.
if height[l] < height[r] : #Close in the pointer depends on whichever one is shorter.
l+=1
else:
r-=1
return area
|
container-with-most-water
|
Easy To Understand Python Two Pointers
|
zhibin-wang09
| 3 | 169 |
container with most water
| 11 | 0.543 |
Medium
| 456 |
https://leetcode.com/problems/container-with-most-water/discuss/1923615/Easiest-and-most-efficient-python-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l,r=0,len(height)-1
ans=0
while l<r:
ans=max(ans,(r-l)*min(height[l],height[r]))
if height[l]<height[r]:
l+=1
else:
r-=1
return ans
|
container-with-most-water
|
Easiest and most efficient python solution
|
tkdhimanshusingh
| 3 | 163 |
container with most water
| 11 | 0.543 |
Medium
| 457 |
https://leetcode.com/problems/container-with-most-water/discuss/2798149/CJavaPython3JavaScript-Solution-(Faster-than-95-)
|
class Solution:
def maxArea(self, height: List[int]) -> int:
i = 0;
j = len(height)-1;
maxAmt = 0;
while(j>i):
iH = height[i];
jH = height[j];
if(iH>jH):
cal = jH*(j-i);
j-=1;
else:
cal = iH*(j-i);
i+=1;
if(cal>maxAmt):
maxAmt = cal;
return maxAmt;
|
container-with-most-water
|
👾C#,Java,Python3,JavaScript Solution (Faster than 95% )
|
HEWITT001
| 2 | 260 |
container with most water
| 11 | 0.543 |
Medium
| 458 |
https://leetcode.com/problems/container-with-most-water/discuss/2425738/Python-Beats-99-Faster-Solution-using-Two-Pointers-oror-Documented
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r, = 0, len(height)-1 # two pointers to left and right
area = 0 # max area
# Repeat until the pointers left and right meet each other
while l < r:
minH = min(height[l], height[r]) # min-height
area = max(area, (r-l) * minH ) # area = distance * min-height
if height[l] < height[r]:
while height[l] <= minH and l < r:
l += 1 # forward left pointer
else:
while height[r] <= minH and l < r:
r -= 1 # backward right pointer
return area
|
container-with-most-water
|
[Python] Beats 99%, Faster Solution using Two Pointers || Documented
|
Buntynara
| 2 | 141 |
container with most water
| 11 | 0.543 |
Medium
| 459 |
https://leetcode.com/problems/container-with-most-water/discuss/1811148/Python-or-O(N)-or-2-pointers-or-with-comments
|
class Solution:
def maxArea(self, height: List[int]) -> int:
# 2 points one from left and one from right
# aim is to
left,right = 0,len(height)-1
mxArea = 0
while left<right:
# area is (distance between 2 heights) x (miminum among 2 heights)
mxArea = max(mxArea, min(height[left],height[right])*(right-left))
#keep moving either left or right till becomes a bigger height.
if height[left]>height[right]:
right-=1
else:
left+=1
return mxArea
# Time Complexity: O(N), Space Complexity: O(1)
|
container-with-most-water
|
Python | O(N) | 2 pointers | with comments
|
vishyarjun1991
| 2 | 237 |
container with most water
| 11 | 0.543 |
Medium
| 460 |
https://leetcode.com/problems/container-with-most-water/discuss/1069627/Python-or-Two-pointers-or-beats-99.7
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l = m = 0
r = len(height)-1
while l<r:
if height[l]>height[r]:
a = height[r]*(r-l)
if a>m:
m = a
r -= 1
else:
a = height[l]*(r-l)
if a>m:
m = a
l += 1
return m
|
container-with-most-water
|
Python | Two pointers | beats 99.7 %
|
SlavaHerasymov
| 2 | 331 |
container with most water
| 11 | 0.543 |
Medium
| 461 |
https://leetcode.com/problems/container-with-most-water/discuss/990214/Two-pointer-python-Solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
water = []
right = 0
left = len(height) - 1
while (right < left):
water.append(min(height[right], height[left]) * (left - right))
if (height[right] < height[left]):
right += 1
else:
left -= 1
return max(water)
|
container-with-most-water
|
Two pointer python Solution
|
mintukrish
| 2 | 254 |
container with most water
| 11 | 0.543 |
Medium
| 462 |
https://leetcode.com/problems/container-with-most-water/discuss/2826971/Constructive-and-Intuitive-Solution-in-Linear-Time-The-Best
|
class Solution:
def maxArea(self, height: list[int]) -> int:
area_left_higher = self.max_area_when_left_higher(height)
height.reverse()
area_right_higher = self.max_area_when_left_higher(height)
return max(area_left_higher, area_right_higher)
def max_area_when_left_higher(self, height: list[int]) -> int:
max_area = 0
ascending_indexes, ascending_heights = [], []
ascending_height_max = -1
for right_index, right_height in enumerate(height):
if right_height > ascending_height_max:
ascending_height_max = right_height
ascending_indexes.append(right_index)
ascending_heights.append(right_height)
else:
left_index = ascending_indexes[bisect.bisect_left(ascending_heights, right_height)]
max_area = max(max_area, (right_index - left_index) * right_height)
return max_area
|
container-with-most-water
|
Constructive and Intuitive Solution in Linear Time, The Best
|
Triquetra
| 1 | 10 |
container with most water
| 11 | 0.543 |
Medium
| 463 |
https://leetcode.com/problems/container-with-most-water/discuss/2464001/Python3-solution(explained)
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
res = 0
while l < r:
res = max(res, min(height[l], height[r]) * (r - l))
if height[l] < height[r]:
l += 1
elif height[r] <= height[l]:
r -= 1
return res
|
container-with-most-water
|
Python3 solution(explained)
|
__Simamina__
| 1 | 121 |
container with most water
| 11 | 0.543 |
Medium
| 464 |
https://leetcode.com/problems/container-with-most-water/discuss/2462266/Simple-python-solution-with-two-pointers
|
class Solution:
def maxArea(self, height: List[int]) -> int:
start = 0
end = len(height) - 1
res = 0
while start < end:
res = max(res, (end-start) * min(height[start], height[end]))
if height[start] <= height[end]:
start += 1
else:
end -= 1
return res
|
container-with-most-water
|
Simple python solution with two pointers
|
Gilbert770
| 1 | 130 |
container with most water
| 11 | 0.543 |
Medium
| 465 |
https://leetcode.com/problems/container-with-most-water/discuss/2436340/python-easy
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l=0
r=len(height)-1
m=0
while l<r:
area=(r-l)*min(height[r],height[l])
m=max(area,m)
if(height[l] < height[r]):
l+=1
else:
r-=1
return m
|
container-with-most-water
|
python easy
|
samudralanithin
| 1 | 121 |
container with most water
| 11 | 0.543 |
Medium
| 466 |
https://leetcode.com/problems/container-with-most-water/discuss/2327827/Python3-Time%3A-O(N)-Space%3A-O(1)-Two-Pointers
|
class Solution:
def maxArea(self, height: List[int]) -> int:
maxArea=0
l=0
r=len(height)-1
while l<r:
area=(r-l)*min(height[r],height[l])
maxArea=max(maxArea,area)
if height[r]>height[l]:
l+=1
else:
r-=1
return maxArea
|
container-with-most-water
|
Python3 Time: O(N) Space: O(1) Two Pointers
|
Simon-Huang-1
| 1 | 80 |
container with most water
| 11 | 0.543 |
Medium
| 467 |
https://leetcode.com/problems/container-with-most-water/discuss/2324493/Python-3-oror-O(n)-Efficient-Solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right, result = 0, len(height) - 1, 0
while left != right:
result = max(result, min(height[left], height[right]) * (right - left))
if height[left] < height[right]: left += 1
else: right -= 1
return result
|
container-with-most-water
|
Python 3 || O(n) Efficient Solution
|
sagarhasan273
| 1 | 61 |
container with most water
| 11 | 0.543 |
Medium
| 468 |
https://leetcode.com/problems/container-with-most-water/discuss/2288302/Easy-Python-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l=0
r=len(height)-1
max_area = min(height[l],height[r])*(r-l)
while r > l:
if height[l] > height[r]:
r-= 1
else:
l += 1
max_area = max(max_area,min(height[l],height[r])*(r-l) )
return max_area
|
container-with-most-water
|
Easy Python solution
|
sunakshi132
| 1 | 96 |
container with most water
| 11 | 0.543 |
Medium
| 469 |
https://leetcode.com/problems/container-with-most-water/discuss/2230240/Python-Simple-Python-Solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
i=0
j=len(height)-1
maxheight=0
while i<j:
area = (j-i)*(min(height[i],height[j]))
maxheight = max(maxheight,area)
if height[i]<height[j]:
i+=1
else:
j-=1
return maxheight
|
container-with-most-water
|
[ Python ] ✅ Simple Python Solution ✅✅
|
vaibhav0077
| 1 | 153 |
container with most water
| 11 | 0.543 |
Medium
| 470 |
https://leetcode.com/problems/container-with-most-water/discuss/2142920/Python-easy
|
class Solution:
def maxArea(self, height: List[int]) -> int:
i,j = 0,len(height)-1
final_area = 0
while i<j:
curr_area = (j-i) * min(height[i],height[j])
final_area = curr_area if curr_area>final_area else final_area
if height[i]<height[j]:
i += 1
else:
j -= 1
return final_area
|
container-with-most-water
|
Python easy
|
NiketaM
| 1 | 150 |
container with most water
| 11 | 0.543 |
Medium
| 471 |
https://leetcode.com/problems/container-with-most-water/discuss/1984560/Two-pointer-solution-or-O(n)
|
class Solution:
def maxArea(self, height: List[int]) -> int:
res = 0
l, r = 0, len(height) - 1
while l < r:
area = (r - l) * min(height[r], height[l])
res = max(res, area)
if height[l] < height[r]:
l += 1
else:
r -= 1
return res
|
container-with-most-water
|
Two pointer solution | O(n)
|
nikhitamore
| 1 | 121 |
container with most water
| 11 | 0.543 |
Medium
| 472 |
https://leetcode.com/problems/container-with-most-water/discuss/1913586/Python-Two-Pointer-O(n)
|
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
left, right = 0, len(height) - 1
while left < right:
current_area = (right - left) * min(height[left], height[right])
max_area = max(current_area, max_area)
if height[left] < height[right]:
left += 1
elif height[left] > height[right]:
right -= 1
else:
left += 1
right -= 1
return max_area
|
container-with-most-water
|
Python Two-Pointer O(n)
|
doubleimpostor
| 1 | 104 |
container with most water
| 11 | 0.543 |
Medium
| 473 |
https://leetcode.com/problems/container-with-most-water/discuss/1861428/Python-or-6-lines
|
class Solution:
def maxArea(self, height: List[int]) -> int:
hi, lo, maxArea = len(height)-1, 0, 0
while lo < hi:
maxArea = max(maxArea, (hi-lo) * min(height[lo], height[hi]))
if height[lo] < height[hi]: lo += 1
else: hi -= 1
return maxArea
|
container-with-most-water
|
✅ Python | 6 lines
|
dhananjay79
| 1 | 149 |
container with most water
| 11 | 0.543 |
Medium
| 474 |
https://leetcode.com/problems/container-with-most-water/discuss/1861428/Python-or-6-lines
|
class Solution:
def maxArea(self, height: List[int]) -> int:
def maxArea(lo,hi,maxi):
if lo >= hi: return maxi
if height[lo] < height[hi]:
return maxArea(lo+1, hi, max(maxi, height[lo] * (hi-lo)))
return maxArea(lo, hi-1, max(maxi, height[hi] * (hi-lo)))
return maxArea(0, len(height)-1, 0)
|
container-with-most-water
|
✅ Python | 6 lines
|
dhananjay79
| 1 | 149 |
container with most water
| 11 | 0.543 |
Medium
| 475 |
https://leetcode.com/problems/container-with-most-water/discuss/1772015/Fast-and-simple-python-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height) - 1
maxarea = 0
while l < r:
height_l, height_r = height[l], height[r]
if height_l <= height_r:
maxarea = max(maxarea, (r - l) * height_l)
while height_l >= height[l] and l < r: l += 1
else:
maxarea = max(maxarea, (r - l) * height_r)
while height_r >= height[r] and l < r: r -= 1
return maxarea
|
container-with-most-water
|
Fast and simple python solution
|
xuauul
| 1 | 134 |
container with most water
| 11 | 0.543 |
Medium
| 476 |
https://leetcode.com/problems/container-with-most-water/discuss/1629497/Python-3-start-and-end-pointer-(my-first-solution)
|
class Solution:
def maxArea(self, height: List[int]) -> int:
retArea = 0
start = 0
end = len(height)-1
while(start != end):
if(height[start] < height[end]):
retArea = max(retArea,height[start] * (end - start))
start +=1
else:
retArea = max(retArea,height[end] * (end - start))
end -=1
return retArea
|
container-with-most-water
|
Python 3, start and end pointer (my first solution)
|
vinija
| 1 | 109 |
container with most water
| 11 | 0.543 |
Medium
| 477 |
https://leetcode.com/problems/container-with-most-water/discuss/1531999/Py3Py-Two-pointer-solution-w-comments
|
class Solution:
def maxArea(self, height: List[int]) -> int:
# Init
area = -inf
i = 0
j = len(height)-1
# Two pointer solution
while i < j:
# Calc breath
breath = j-i
# Keep track of max length
if height[i] < height[j]:
length = height[i]
i += 1
else:
length = height[j]
j -= 1
# Calc area
area = max(area, breath * length)
return area
|
container-with-most-water
|
[Py3/Py] Two pointer solution w/ comments
|
ssshukla26
| 1 | 233 |
container with most water
| 11 | 0.543 |
Medium
| 478 |
https://leetcode.com/problems/container-with-most-water/discuss/1343946/Python-oror-Two-pointer-oror-O(N)
|
class Solution:
def maxArea(self, heights: List[int]) -> int:
gap = len(heights)-1
left = 0; right = len(heights)-1
maxx = 0
while left < right:
maxx = max(maxx, gap*min(heights[left], heights[right]))
if heights[left] <= heights[right]: left += 1
else: right -= 1
gap -= 1
return maxx
|
container-with-most-water
|
Python || Two pointer || O(N)
|
airksh
| 1 | 98 |
container with most water
| 11 | 0.543 |
Medium
| 479 |
https://leetcode.com/problems/container-with-most-water/discuss/1264294/Explained-Python-Attack-Both-Ways
|
class Solution(object):
def maxArea(self, height):
left = 0
right = len(height)-1
max_area = 0
while left<right:
max_area = max(min(height[left],height[right]) * (right-left),max_area)
if height[left]<height[right]:
left+=1
else:
right-=1
return max_area
|
container-with-most-water
|
[Explained] Python — Attack Both Ways
|
akashadhikari
| 1 | 236 |
container with most water
| 11 | 0.543 |
Medium
| 480 |
https://leetcode.com/problems/container-with-most-water/discuss/794967/Python-3-commented
|
class Solution:
def maxArea(self, h: List[int]) -> int:
# Maximum Volume
m = 0
# Left Index
l = 0
# Right Index
r = len(h) - 1
while l != r:
# Volume is height of lowest wall times distance between walls
v = min(h[l], h[r]) * (r - l)
# If volume is bigger than current maximum, save new max
if v > m:
m = v
# Keep highest wall and move the other end towards it
if h[l] > h[r]:
r -= 1
else:
l += 1
return m
|
container-with-most-water
|
Python 3 commented
|
denisrasulev
| 1 | 96 |
container with most water
| 11 | 0.543 |
Medium
| 481 |
https://leetcode.com/problems/container-with-most-water/discuss/506120/Python-Faster-than-95-simple-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
i = 0
j = len(height) - 1
mx = 0
while i < j:
area = min(height[i], height[j]) * (j-i)
mx = max(mx, area)
if (height[i] < height[j]):
l = height[i]
while (height[i+1] < l) and (i < j):
i += 1
else:
i += 1
else:
r = height[j]
while (height[j-1] < r) and (i < j):
j -= 1
else:
j -= 1
return mx
|
container-with-most-water
|
Python - Faster than 95% - simple solution
|
frankv55
| 1 | 223 |
container with most water
| 11 | 0.543 |
Medium
| 482 |
https://leetcode.com/problems/container-with-most-water/discuss/407454/Python3-%3A-148-ms-faster-than-52.60
|
class Solution:
def maxArea(self, height: List[int]) -> int:
head_ = 0
tail_ = len(height)-1
area = 0
while( head_ < tail_ ):
area = max(area, ((tail_-head_)*min(height[tail_], height[head_])))
if height[head_] <= height[tail_]:
head_ += 1
else:
tail_ -= 1
return area
|
container-with-most-water
|
Python3 : 148 ms, faster than 52.60%
|
rnnnnn
| 1 | 190 |
container with most water
| 11 | 0.543 |
Medium
| 483 |
https://leetcode.com/problems/container-with-most-water/discuss/2847080/PYTHON-MAD-EASY-100-ON-GOD
|
class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height)-1
res = 0
while right > left:
a = (right-left)*min(height[right],height[left])
res = a if a > res else res
if height[left] > height[right]:
right -= 1
else:
left += 1
return res
|
container-with-most-water
|
[PYTHON] MAD EASY 100% ON GOD
|
omkarxpatel
| 0 | 2 |
container with most water
| 11 | 0.543 |
Medium
| 484 |
https://leetcode.com/problems/container-with-most-water/discuss/2838263/Python-or-Two-Pointer-or-Comments
|
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height)-1
max_area = 0
while left < right: # < because left can't equal right or else it can't trap water
length = right - left # the length in length * height = area
height_ = min(height[left], height[right]) # we take the minimum because it would overflow above the min
current_area = length * height_
max_area = max(max_area, current_area)
if height[left] > height[right]: # move the lesser pointer because we're trying to find MAX
right -= 1
else: # if right <= left
left += 1
return max_area
|
container-with-most-water
|
🎉Python | Two Pointer | Comments
|
Arellano-Jann
| 0 | 2 |
container with most water
| 11 | 0.543 |
Medium
| 485 |
https://leetcode.com/problems/container-with-most-water/discuss/2836014/EASY-solution-Python
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r, area = 0, len(height) - 1, 0
while l<r:
area = max(area, (r-l)*min(height[l],height[r]))
if height[l]<height[r]:
l+=1
else:
r-=1
return area
|
container-with-most-water
|
EASY solution Python
|
atyantjain2
| 0 | 3 |
container with most water
| 11 | 0.543 |
Medium
| 486 |
https://leetcode.com/problems/container-with-most-water/discuss/2834495/Simple-python-solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height) -1
curMax = 0
while left < right:
curVal= min(height[left],height[right]) * (right - left)
curMax = max(curVal,curMax)
if(height[left] > height[right]):
right-=1
else:
left+=1
return curMax
|
container-with-most-water
|
Simple python solution
|
btulsi
| 0 | 2 |
container with most water
| 11 | 0.543 |
Medium
| 487 |
https://leetcode.com/problems/container-with-most-water/discuss/2822097/Simple-Python-implementation-using-two-pointers-approach
|
class Solution:
def maxArea(self, height: List[int]) -> int:
start, end = 0, len(height)-1
area = 0
llist = []
maxx = 0
while start < end:
width = abs(start - end )
area = min( height[start], height[end] )*width
maxx = max(area, maxx)
#llist.append(area)
if height[start] > height[end]:
end -= 1
else:
start += 1
return maxx
|
container-with-most-water
|
Simple Python implementation using two pointers approach
|
rahul-tiwari-95
| 0 | 3 |
container with most water
| 11 | 0.543 |
Medium
| 488 |
https://leetcode.com/problems/container-with-most-water/discuss/2820193/Dynamic-programming-beats-99.69-memory-usage
|
class Solution:
def maxArea(self, height: List[int]) -> int:
i = v = 0
j = l = len(height) - 1
while i < j:
if v < l*min(height[i], height[j]):
v = l*min(height[i], height[j])
if height[i] < height[j]:
i += 1
else:
j -= 1
l -= 1
return v
|
container-with-most-water
|
Dynamic programming beats 99.69% memory usage
|
Molot84
| 0 | 4 |
container with most water
| 11 | 0.543 |
Medium
| 489 |
https://leetcode.com/problems/container-with-most-water/discuss/2815851/Easy-Python-Solution-w-intuition
|
class Solution:
def maxArea(self, height: List[int]) -> int:
l, r = 0, len(height)-1
max_area = 0
while l < r:
area = min(height[l], height[r])*(r-l)
if area > max_area:
max_area = area
if height[l] > height[r]:
r -=1
else:
l += 1
return max_area
|
container-with-most-water
|
Easy Python Solution w/ intuition
|
emilyyijack
| 0 | 4 |
container with most water
| 11 | 0.543 |
Medium
| 490 |
https://leetcode.com/problems/container-with-most-water/discuss/2815182/Streamlined-Two-Pointers-Python
|
class Solution:
def maxArea(self, height: List[int]) -> int:
w, L, R = 0, 0, len(height) - 1
while L < R:
w = max(w, (R - L) * min(height[R], height[L]))
L, R = (L + 1, R) if height[L] < height[R] else (L, R - 1)
return w
|
container-with-most-water
|
Streamlined Two Pointers [Python]
|
constantstranger
| 0 | 5 |
container with most water
| 11 | 0.543 |
Medium
| 491 |
https://leetcode.com/problems/container-with-most-water/discuss/2815176/Two-Pointers-Approach%3A-Easy-Explanation-Python
|
class Solution:
def maxArea(self, height: List[int]) -> int:
leftPointer = 0
rightPointer = len(height) - 1
maxArea = (rightPointer - leftPointer)*min(height[leftPointer],height[rightPointer])
while leftPointer < rightPointer:
currHeightL = height[leftPointer]
currHeightR = height[rightPointer]
maxArea = max(maxArea,(rightPointer - leftPointer)*min(currHeightL,currHeightR))
if currHeightL < currHeightR:
leftPointer += 1
else:
rightPointer -= 1
return maxArea
|
container-with-most-water
|
Two Pointers Approach: Easy Explanation Python
|
kenanR
| 0 | 2 |
container with most water
| 11 | 0.543 |
Medium
| 492 |
https://leetcode.com/problems/container-with-most-water/discuss/2808291/SimpleandClearandOrdinary
|
class Solution:
def maxArea(self, height: List[int]) -> int:
firstIndex = 0
lastIndex = len(height)-1
Max = 0
while firstIndex < lastIndex:
first, last = height[firstIndex], height[lastIndex]
temp = (lastIndex - firstIndex)*min(first, last)
if Max < temp:
Max = temp
if last <= first:
lastIndex = lastIndex -1
else:
firstIndex = firstIndex + 1
return Max
|
container-with-most-water
|
Simple&Clear&Ordinary
|
Digger_
| 0 | 3 |
container with most water
| 11 | 0.543 |
Medium
| 493 |
https://leetcode.com/problems/container-with-most-water/discuss/2805709/Python3-two-pointer-O(N)-Solution
|
class Solution:
def maxArea(self, height: List[int]) -> int:
areaMax=0
l,r=0,len(height)-1
while l<r:
area=(r-l) * min(height[l],height[r])
areaMax=max(areaMax,area)
if height[l]<=height[r]:
l+=1
else:
r-=1
return areaMax
|
container-with-most-water
|
Python3 two pointer O(N) Solution
|
nachikethkc
| 0 | 2 |
container with most water
| 11 | 0.543 |
Medium
| 494 |
https://leetcode.com/problems/container-with-most-water/discuss/2803006/Python-3-solution-using-two-pointers
|
class Solution:
def maxArea(self, height: List[int]) -> int:
"""Uses two pointers.
Left pointer starts with 0th index and
Right pointer starts at last index.
Calculate the area and update if greater than
area variable.
If heigh[right] is greater than heigh[left], increment
left, otherwise decrement right.
"""
area = 0
left = 0
right = len(height) -1
while left < right:
side = min(height[left], height[right])
width = right - left
area = max(side * width, area)
if height[right] > height[left]:
left += 1
else:
right -= 1
return area
|
container-with-most-water
|
Python 3 solution using two pointers
|
ankitjaiswal07
| 0 | 4 |
container with most water
| 11 | 0.543 |
Medium
| 495 |
https://leetcode.com/problems/integer-to-roman/discuss/2724200/Python's-Simple-and-Easy-to-Understand-Solution-or-99-Faster
|
class Solution:
def intToRoman(self, num: int) -> str:
# Creating Dictionary for Lookup
num_map = {
1: "I",
5: "V", 4: "IV",
10: "X", 9: "IX",
50: "L", 40: "XL",
100: "C", 90: "XC",
500: "D", 400: "CD",
1000: "M", 900: "CM",
}
# Result Variable
r = ''
for n in [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]:
# If n in list then add the roman value to result variable
while n <= num:
r += num_map[n]
num-=n
return r
|
integer-to-roman
|
✔️ Python's Simple and Easy to Understand Solution | 99% Faster 🔥
|
pniraj657
| 53 | 2,600 |
integer to roman
| 12 | 0.615 |
Medium
| 496 |
https://leetcode.com/problems/integer-to-roman/discuss/2723880/Fastest-Python-solution-using-mapping
|
class Solution:
def intToRoman(self, num: int) -> str:
i=1
dic={1:'I',5:'V',10:'X',50:'L',100:'C',500:'D',1000:'M'}
s=""
while num!=0:
y=num%pow(10,i)//pow(10,i-1)
if y==5:
s=dic[y*pow(10,i-1)]+s
elif y==1:
s=dic[y*pow(10,i-1)]+s
elif y==4:
s=dic[1*pow(10,i-1)]+dic[5*pow(10,i-1)]+s
elif y==9:
s=dic[1*pow(10,i-1)]+dic[1*pow(10,i)]+s
elif y<4:
s=dic[pow(10,i-1)]*y+s
elif y>5 and y<9:
y-=5
s=dic[5*pow(10,i-1)]+dic[pow(10,i-1)]*y+s
num=num//pow(10,i)
num*=pow(10,i)
i+=1
return s
|
integer-to-roman
|
Fastest Python solution using mapping
|
shubham_1307
| 16 | 2,400 |
integer to roman
| 12 | 0.615 |
Medium
| 497 |
https://leetcode.com/problems/integer-to-roman/discuss/1102756/Python-Stack-Solution
|
class Solution:
def intToRoman(self, num: int) -> str:
dct = {1: 'I', 4: 'IV', 5: 'V', 9: 'IX', 10: 'X', 40: 'XL', 50: 'L', 90: 'XC', 100: 'C', 400: 'CD', 500: 'D', 900: 'CM', 1000: 'M'}
stack = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
ans = []
while num > 0:
if stack[-1] > num:
stack.pop()
else:
num -= stack[-1]
ans.append(dct[stack[-1]])
return "".join(map(str, ans))
|
integer-to-roman
|
Python Stack Solution
|
mariandanaila01
| 11 | 1,000 |
integer to roman
| 12 | 0.615 |
Medium
| 498 |
https://leetcode.com/problems/integer-to-roman/discuss/389377/Easy-to-understand-python3
|
class Solution:
def intToRoman(self, num: int) -> str:
mapping = {
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M",
}
arr = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
result = ""
for a in arr:
while num >= a:
num = num - a
result = result + mapping[a]
return result
|
integer-to-roman
|
Easy to understand python3
|
WhiteSeraph
| 8 | 591 |
integer to roman
| 12 | 0.615 |
Medium
| 499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.