986-Interval-List-Intersections
https://leetcode.com/problems/interval-list-intersections
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
987-Vertical-Order-Traversal-Of-A-Binary-Tree
https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
989-Add-To-Array-Form-Of-Integer
https://leetcode.com/problems/add-to-array-form-of-integer
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
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