986-Interval-List-Intersections

Sat 17 May 2025

https://leetcode.com/problems/interval-list-intersections

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
    ans = []
    i = 0
    j = 0

    while i < len(firstList) and j < len(secondList):
      # Lo := the …

Category: leetcode

Read More

987-Vertical-Order-Traversal-Of-A-Binary-Tree

Sat 17 May 2025

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

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

    def dfs(node: Optional[TreeNode], x: int, y: int) -> None:
      if not node:
        return

      xToNodes[x …

Category: leetcode

Read More

989-Add-To-Array-Form-Of-Integer

Sat 17 May 2025

https://leetcode.com/problems/add-to-array-form-of-integer

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def addToArrayForm(self, num: List[int], k: int) -> List[int]:
    for i in reversed(range(len(num))):
      k, num[i] = divmod(num[i] + k, 10)

    while k > 0:
      num …

Category: leetcode

Read More

99-Recover-Binary-Search-Tree

Sat 17 May 2025

https://leetcode.com/problems/recover-binary-search-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def recoverTree(self, root: Optional[TreeNode]) -> None:
    def swap(x: Optional[TreeNode], y: Optional[TreeNode]) -> None:
      temp = x.val
      x.val = y.val
      y.val = temp

    def inorder(root …

Category: leetcode

Read More

990-Satisfiability-Of-Equality-Equations

Sat 17 May 2025

https://leetcode.com/problems/satisfiability-of-equality-equations

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 = list(range(n))

  def union(self, u: int, v: int) -> None:
    self.id[self.find(u)] = self.find(v)

  def find(self …

Category: leetcode

Read More

991-Broken-Calculator

Sat 17 May 2025

https://leetcode.com/problems/broken-calculator

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def brokenCalc(self, X: int, Y: int) -> int:
    ops = 0

    while X < Y:
      if Y % 2 == 0:
        Y //= 2
      else:
        Y += 1
      ops += 1

    return ops + X - Y
new …

Category: leetcode

Read More

992-Subarrays-With-K-Different-Integers

Sat 17 May 2025

https://leetcode.com/problems/subarrays-with-k-different-integers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def subarraysWithKDistinct(self, A: List[int], K: int) -> int:
    def subarraysWithAtMostKDistinct(K: int) -> int:
      ans = 0
      count = Counter()

      l = 0
      for r, a in enumerate(A):
        count[a] += 1 …

Category: leetcode

Read More

995-Minimum-Number-Of-K-Consecutive-Bit-Flips

Sat 17 May 2025

https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips

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

    for r, a in enumerate(A):
      if r >= K and A[r - K] == 2:
        flippedTime -= 1 …

Category: leetcode

Read More

996-Number-Of-Squareful-Arrays

Sat 17 May 2025

https://leetcode.com/problems/number-of-squareful-arrays

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numSquarefulPerms(self, A: List[int]) -> int:
    ans = 0
    used = [False] * len(A)

    def isSquare(num: int) -> bool:
      root = int(sqrt(num))
      return root * root == num

    def dfs(path …

Category: leetcode

Read More

997-Find-The-Town-Judge

Sat 17 May 2025

https://leetcode.com/problems/find-the-town-judge

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findJudge(self, n: int, trust: List[List[int]]) -> int:
    count = [0] * (n + 1)

    for a, b in trust:
      count[a] -= 1
      count[b] += 1

    for i in range …

Category: leetcode

Read More
Page 76 of 77

« Prev Next »