5-Longest-Palindromic-Substring
https://leetcode.com/problems/longest-palindromic-substring
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestPalindrome(self, s: str) -> str:
if not s:
return ''
indices = [0, 0]
# Returns [start, end] indices of the longest palindrome extended from s[i..j]
def extend(s …
Read More
50-Powx-N
https://leetcode.com/problems/powx-n
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def myPow(self, x: float, n: int) -> float:
if n == 0:
return 1
if n < 0:
return 1 / self.myPow(x, -n)
if n & 1:
return x * self.myPow …
Read More
500-Keyboard-Row
https://leetcode.com/problems/keyboard-row
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findWords(self, words: List[str]) -> List[str]:
ans = []
rows = [set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')]
for word in words:
lowerWord = set(word.lower())
if any(lowerWord <= row for …
Read More
501-Find-Mode-In-Binary-Search-Tree
https://leetcode.com/problems/find-mode-in-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMode(self, root: Optional[TreeNode]) -> List[int]:
self.ans = []
self.pred = None
self.count = 0
self.maxCount = 0
def updateCount(root: Optional[TreeNode]) -> None:
if self.pred and …
Read More
507-Perfect-Number
https://leetcode.com/problems/perfect-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkPerfectNumber(self, num: int) -> bool:
return num in {6, 28, 496, 8128, 33550336}
new Solution().checkPerfectNumber()
Score: 5
Read More
508-Most-Frequent-Subtree-Sum
https://leetcode.com/problems/most-frequent-subtree-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
count = Counter()
def dfs(root: Optional[TreeNode]) -> int:
if not root:
return 0
summ = root.val + dfs …
Read More
509-Fibonacci-Number
https://leetcode.com/problems/fibonacci-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def fib(self, N: int) -> int:
if N < 2:
return N
dp = [0, 0, 1]
for i in range(2, N + 1):
dp[0] = dp[1]
dp[1] = dp …
Read More
51-N-Queens
https://leetcode.com/problems/n-queens
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
ans = []
cols = [False] * n
diag1 = [False] * (2 * n - 1)
diag2 = [False] * (2 * n - 1)
def dfs(i: int, board: List[int …
Read More
513-Find-Bottom-Left-Tree-Value
https://leetcode.com/problems/find-bottom-left-tree-value
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q = deque([root])
while q:
root = q.popleft()
if root.right:
q.append(root.right)
if root.left:
q.append(root.left …
Read More
514-Freedom-Trail
https://leetcode.com/problems/freedom-trail
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findRotateSteps(self, ring: str, key: str) -> int:
# Number of rotates of ring to match key[index:]
@functools.lru_cache(None)
def dfs(ring: str, index: int) -> int:
if index …
Read More