99-Recover-Binary-Search-Tree
https://leetcode.com/problems/recover-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
990-Satisfiability-Of-Equality-Equations
https://leetcode.com/problems/satisfiability-of-equality-equations
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
991-Broken-Calculator
https://leetcode.com/problems/broken-calculator
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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
Read More
992-Subarrays-With-K-Different-Integers
https://leetcode.com/problems/subarrays-with-k-different-integers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
995-Minimum-Number-Of-K-Consecutive-Bit-Flips
https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
996-Number-Of-Squareful-Arrays
https://leetcode.com/problems/number-of-squareful-arrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
997-Find-The-Town-Judge
https://leetcode.com/problems/find-the-town-judge
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
998-Maximum-Binary-Tree-Ii
https://leetcode.com/problems/maximum-binary-tree-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
999-Available-Captures-For-Rook
https://leetcode.com/problems/available-captures-for-rook
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
Abstract-With-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 …
Read More