894-All-Possible-Full-Binary-Trees

Sat 17 May 2025

https://leetcode.com/problems/all-possible-full-binary-trees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

896-Monotonic-Array

Sat 17 May 2025

https://leetcode.com/problems/monotonic-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

897-Increasing-Order-Search-Tree

Sat 17 May 2025

https://leetcode.com/problems/increasing-order-search-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

899-Orderly-Queue

Sat 17 May 2025

https://leetcode.com/problems/orderly-queue

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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)))
new Solution …

Category: leetcode

Read More

9-Palindrome-Number

Sat 17 May 2025

https://leetcode.com/problems/palindrome-number

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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
new Solution …

Category: leetcode

Read More

90-Subsets-Ii

Sat 17 May 2025

https://leetcode.com/problems/subsets-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

902-Numbers-At-Most-N-Given-Digit-Set

Sat 17 May 2025

https://leetcode.com/problems/numbers-at-most-n-given-digit-set

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

904-Fruit-Into-Baskets

Sat 17 May 2025

https://leetcode.com/problems/fruit-into-baskets

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

905-Sort-Array-By-Parity

Sat 17 May 2025

https://leetcode.com/problems/sort-array-by-parity

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

906-Super-Palindromes

Sat 17 May 2025

https://leetcode.com/problems/super-palindromes

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 69 of 77

« Prev Next »