645-Set-Mismatch

Fri 14 November 2025

https://leetcode.com/problems/set-mismatch

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findErrorNums(self, nums: List[int]) -> List[int]:
    for num in nums:
      if nums[abs(num) - 1] < 0:
        duplicate = abs(num)
      else:
        nums[abs(num) - 1] *= -1

    for i …

Category: leetcode

Read More

646-Maximum-Length-Of-Pair-Chain

Fri 14 November 2025

https://leetcode.com/problems/maximum-length-of-pair-chain

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findLongestChain(self, pairs: List[List[int]]) -> int:
    ans = 0
    prevEnd = -math.inf

    for s, e in sorted(pairs, key=lambda x: x[1]):
      if s > prevEnd:
        ans += 1 …

Category: leetcode

Read More

647-Palindromic-Substrings

Fri 14 November 2025

https://leetcode.com/problems/palindromic-substrings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def countSubstrings(self, s: str) -> int:
    def extendPalindromes(l: int, r: int) -> int:
      count = 0

      while l >= 0 and r < len(s) and s[l] == s[r]:
        count += 1 …

Category: leetcode

Read More

648-Replace-Words

Fri 14 November 2025

https://leetcode.com/problems/replace-words

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def __init__(self):
    self.root = {}

  def insert(self, word: str) -> None:
    node = self.root
    for c in word:
      if c not in node:
        node[c] = {}
      node = node[c]
    node …

Category: leetcode

Read More

65-Valid-Number

Fri 14 November 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isNumber(self, s: str) -> bool:
    s = s.strip()
    if not s:
      return False

    seenNum = False
    seenDot = False
    seenE = False

    for i, c in enumerate(s):
      if c == '.':
        if …

Category: leetcode

Read More

650-2-Keys-Keyboard

Fri 14 November 2025

https://leetcode.com/problems/2-keys-keyboard

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minSteps(self, n: int) -> int:
    if n <= 1:
      return 0

    # dp[i] := min steps to get i 'A'
    # Copy 'A', then paste 'A' i - 1 times
    dp = [i …

Category: leetcode

Read More

652-Find-Duplicate-Subtrees

Fri 14 November 2025

https://leetcode.com/problems/find-duplicate-subtrees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
    ans = []
    count = Counter()

    def encode(root: Optional[TreeNode]) -> str:
      if not root:
        return ''

      encoded = str(root.val) + '#' + \
          encode(root.left …

Category: leetcode

Read More

653-Two-Sum-Iv-Input-Is-A-Bst

Fri 14 November 2025

https://leetcode.com/problems/two-sum-iv-input-is-a-bst

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class BSTIterator:
  def __init__(self, root: Optional[TreeNode], leftToRight: bool):
    self.stack = []
    self.leftToRight = leftToRight
    self.pushUntilNone(root)

  def next(self) -> int:
    node = self.stack.pop()
    if self.leftToRight:
      self.pushUntilNone …

Category: leetcode

Read More

654-Maximum-Binary-Tree

Fri 14 November 2025

https://leetcode.com/problems/maximum-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
    def build(i: int, j: int) -> Optional[TreeNode]:
      if i > j:
        return None

      maxNum = max(nums[i:j + 1])
      maxIndex = nums …

Category: leetcode

Read More

655-Print-Binary-Tree

Fri 14 November 2025

https://leetcode.com/problems/print-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def printTree(self, root: Optional[TreeNode]) -> List[List[str]]:
    def maxHeight(root: Optional[TreeNode]) -> int:
      if not root:
        return 0
      return 1 + max(maxHeight(root.left), maxHeight(root.right …

Category: leetcode

Read More
Page 55 of 146

« Prev Next »