683-K-Empty-Slots

Fri 14 November 2025

https://leetcode.com/problems/k-empty-slots

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def kEmptySlots(self, bulbs: List[int], k: int) -> int:
    n = len(bulbs)
    ans = math.inf
    # day[i] := the day when bulbs[i] is turned on
    day = [0] * n

    for …

Category: leetcode

Read More

684-Redundant-Connection

Fri 14 November 2025

https://leetcode.com/problems/redundant-connection

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class UnionFind:
  def __init__(self, n: int):
    self.id = [i for i in range(n + 1)]

  def union(self, u: int, v: int) -> bool:
    i = self.find(u)
    j = self.find …

Category: leetcode

Read More

685-Redundant-Connection-Ii

Fri 14 November 2025

https://leetcode.com/problems/redundant-connection-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class UnionFind:
  def __init__(self, n: int):
    self.id = [i for i in range(n + 1)]

  def union(self, u: int, v: int) -> bool:
    i = self.find(u)
    j = self.find …

Category: leetcode

Read More

686-Repeated-String-Match

Fri 14 November 2025

https://leetcode.com/problems/repeated-string-match

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def repeatedStringMatch(self, a: str, b: str) -> int:
    n = ceil(len(b) / len(a))
    s = a * n
    if b in s:
      return n
    if b in s + a:
      return …

Category: leetcode

Read More

687-Longest-Univalue-Path

Fri 14 November 2025

https://leetcode.com/problems/longest-univalue-path

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

    def longestUnivaluePathDownFrom(root: Optional[TreeNode]) -> int:
      nonlocal ans
      if not root:
        return 0

      l = longestUnivaluePathDownFrom(root.left)
      r = longestUnivaluePathDownFrom(root …

Category: leetcode

Read More

688-Knight-Probability-In-Chessboard

Fri 14 November 2025

https://leetcode.com/problems/knight-probability-in-chessboard

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def knightProbability(self, N: int, K: int, r: int, c: int) -> float:
    dirs = [(1, 2), (2, 1), (2, -1), (1, -2),
            (-1, -2), (-2, -1), (-2, 1), (-1, 2 …

Category: leetcode

Read More

689-Maximum-Sum-Of-3-Non-Overlapping-Subarrays

Fri 14 November 2025

https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]:
    ans = [-1] * 3
    subarrayCount = len(nums) - k + 1
    dp = [0] * subarrayCount
    summ = 0

    for i, num in enumerate(nums …

Category: leetcode

Read More

69-Sqrtx

Fri 14 November 2025

https://leetcode.com/problems/sqrtx

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def mySqrt(self, x: int) -> int:
    l = 1
    r = x + 1

    while l < r:
      m = (l + r) // 2
      if m * m > x:
        r = m
      else:
        l = m + 1

    # L …

Category: leetcode

Read More

690-Employee-Importance

Fri 14 November 2025

https://leetcode.com/problems/employee-importance

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def getImportance(self, employees: List['Employee'], id: int) -> int:
    idToEmployee = {employee.id: employee for employee in employees}

    def dfs(id: int) -> int:
      values = idToEmployee[id].importance
      for subId in …

Category: leetcode

Read More

691-Stickers-To-Spell-Word

Fri 14 November 2025

https://leetcode.com/problems/stickers-to-spell-word

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minStickers(self, stickers: List[str], target: str) -> int:
    n = len(target)
    maxMask = 1 << n
    # dp[i] := min # Of stickers to spell out i,
    # Where i is the bit …

Category: leetcode

Read More
Page 58 of 146

« Prev Next »