310-Minimum-Height-Trees

Sat 17 May 2025

https://leetcode.com/problems/minimum-height-trees

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
    if n == 1 or not edges:
      return [0]

    ans = []
    graph = defaultdict(set)

    for u, v in edges:
      graph …

Category: leetcode

Read More

311-Sparse-Matrix-Multiplication

Sat 17 May 2025

https://leetcode.com/problems/sparse-matrix-multiplication

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
    m = len(mat1)
    n = len(mat2)
    l = len(mat2[0])
    ans = [[0] * l for _ …

Category: leetcode

Read More

312-Burst-Balloons

Sat 17 May 2025

https://leetcode.com/problems/burst-balloons

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxCoins(self, nums: List[int]) -> int:
    A = [1] + nums + [1]

    @functools.lru_cache(None)
    def dp(i: int, j: int) -> int:
      if i > j:
        return 0

      return max(dp …

Category: leetcode

Read More

313-Super-Ugly-Number

Sat 17 May 2025

https://leetcode.com/problems/super-ugly-number

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
    k = len(primes)
    nums = [1]
    indices = [0] * k

    while len(nums) < n:
      nexts = [0] * k
      for i in range(k …

Category: leetcode

Read More

314-Binary-Tree-Vertical-Order-Traversal

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-vertical-order-traversal

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
    if not root:
      return []

    range_ = [0] * 2

    def getRange(root: Optional[TreeNode], x: int) -> None:
      if not root:
        return

      range_ …

Category: leetcode

Read More

315-Count-Of-Smaller-Numbers-After-Self

Sat 17 May 2025

https://leetcode.com/problems/count-of-smaller-numbers-after-self

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class FenwickTree:
  def __init__(self, n: int):
    self.sums = [0] * (n + 1)

  def update(self, i: int, delta: int) -> None:
    while i < len(self.sums):
      self.sums[i] += delta
      i …

Category: leetcode

Read More

316-Remove-Duplicate-Letters

Sat 17 May 2025

https://leetcode.com/problems/remove-duplicate-letters

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def removeDuplicateLetters(self, s: str) -> str:
    ans = []
    count = Counter(s)
    used = [False] * 26

    for c in s:
      count[c] -= 1
      if used[ord(c) - ord('a')]:
        continue
      while ans …

Category: leetcode

Read More

317-Shortest-Distance-From-All-Buildings

Sat 17 May 2025

https://leetcode.com/problems/shortest-distance-from-all-buildings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def shortestDistance(self, grid: List[List[int]]) -> int:
    m = len(grid)
    n = len(grid[0])
    dirs = [0, 1, 0, -1, 0]
    nBuildings = sum(a == 1 for row in grid …

Category: leetcode

Read More

318-Maximum-Product-Of-Word-Lengths

Sat 17 May 2025

https://leetcode.com/problems/maximum-product-of-word-lengths

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxProduct(self, words: List[str]) -> int:
    ans = 0

    def getMask(word: str) -> int:
      mask = 0
      for c in word:
        mask |= 1 << ord(c) - ord('a')
      return mask

    masks …

Category: leetcode

Read More

319-Bulb-Switcher

Sat 17 May 2025

https://leetcode.com/problems/bulb-switcher

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def bulbSwitch(self, n: int) -> int:
    return int(sqrt(n))
new Solution().bulbSwitch()

Score: 5

Category: leetcode

Read More
Page 26 of 77

« Prev Next »