894-All-Possible-Full-Binary-Trees
https://leetcode.com/problems/all-possible-full-binary-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
@functools.lru_cache(None)
def allPossibleFBT(self, n: int) -> List[Optional[TreeNode]]:
if n % 2 == 0:
return []
if n == 1:
return [TreeNode(0)]
ans = []
for leftCount in range(n):
rightCount …
Read More
896-Monotonic-Array
https://leetcode.com/problems/monotonic-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isMonotonic(self, A: List[int]) -> bool:
increasing = True
decreasing = True
for i in range(1, len(A)):
increasing &= A[i - 1] <= A[i]
decreasing &= A[i - 1] >= A …
Read More
897-Increasing-Order-Search-Tree
https://leetcode.com/problems/increasing-order-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def increasingBST(self, root: TreeNode, tail: TreeNode = None) -> TreeNode:
if not root:
return tail
res = self.increasingBST(root.left, root)
root.left = None
root.right = self.increasingBST(root.right …
Read More
899-Orderly-Queue
https://leetcode.com/problems/orderly-queue
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def orderlyQueue(self, S: str, K: int) -> str:
return ''.join(sorted(S)) if K > 1 else min(S[i:] + S[:i] for i in range(len(S)))
Read More
9-Palindrome-Number
https://leetcode.com/problems/palindrome-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
rev = 0
y = x
while y:
rev = rev * 10 + y % 10
y //= 10
return rev == x
Read More
90-Subsets-Ii
https://leetcode.com/problems/subsets-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
ans = []
def dfs(s: int, path: List[int]) -> None:
ans.append(path)
if s == len(nums):
return
for i in …
Read More
902-Numbers-At-Most-N-Given-Digit-Set
https://leetcode.com/problems/numbers-at-most-n-given-digit-set
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def atMostNGivenDigitSet(self, D: List[str], N: int) -> int:
ans = 0
num = str(N)
for i in range(1, len(num)):
ans += len(D)**i
for i, c in …
Read More
904-Fruit-Into-Baskets
https://leetcode.com/problems/fruit-into-baskets
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def totalFruit(self, tree: List[int]) -> int:
ans = 0
count = defaultdict(int)
l = 0
for r, t in enumerate(tree):
count[t] += 1
while len(count) > 2:
count[tree …
Read More
905-Sort-Array-By-Parity
https://leetcode.com/problems/sort-array-by-parity
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortArrayByParity(self, A: List[int]) -> List[int]:
l = 0
r = len(A) - 1
while l < r:
if A[l] % 2 == 1 and A[r] % 2 == 0:
A[l …
Read More
906-Super-Palindromes
https://leetcode.com/problems/super-palindromes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
def nextPalindrome(num: int) -> int:
s = str(num)
n = len(s)
half = s[0:(n + 1) // 2]
reversedHalf = half[:n // 2 …
Read More