99-Recover-Binary-Search-Tree

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

998-Maximum-Binary-Tree-Ii

Fri 14 November 2025

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

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

Category: leetcode

Read More

999-Available-Captures-For-Rook

Fri 14 November 2025

https://leetcode.com/problems/available-captures-for-rook

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

    for i in range(8):
      for j in range(8):
        if board[i][j] == 'R':
          i0 = i
          j0 = j …

Category: leetcode

Read More

Abstract-With-Ast

Fri 14 November 2025
!python --version
Python 3.12.4
import ast
# Python code as a string
code = """
def greet(name):
    return f"Hello, {name}!"

x = 10
y = x + 20
print(greet("Python"))
"""
# Parse the code into an AST
tree = ast.parse(code)
# Define a visitor class to analyze nodes
class CodeAnalyzer(ast.NodeVisitor …

Category: basics

Read More
Page 83 of 146

« Prev Next »