1057-Campus-Bikes
https://leetcode.com/problems/campus-bikes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> List[int]:
ans = [-1] * len(workers)
usedBikes = [False] * len(bikes)
# buckets[k] := (i, j), where k = dist(workers …
Read More
1058-Minimize-Rounding-Error-To-Meet-Target
https://leetcode.com/problems/minimize-rounding-error-to-meet-target
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minimizeError(self, prices: List[str], target: int) -> str:
# A[i] := (costCeil - costFloor, costCeil, costFloor)
# The lower the costCeil - costFloor, the cheaper to ceil it
A = []
sumFloored = 0
sumCeiled …
Read More
106-Construct-Binary-Tree-From-Inorder-And-Postorder-Traversal
https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
inToIndex = {num: i for i, num in enumerate(inorder)}
def build(inStart: int, inEnd: int, postStart: int, postEnd …
Read More
1063-Number-Of-Valid-Subarrays
https://leetcode.com/problems/number-of-valid-subarrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validSubarrays(self, nums: List[int]) -> int:
# For each num in nums, each element x in the stack can be the leftmost
# Element s.t. [x, num] forms a …
Read More
1066-Campus-Bikes-Ii
https://leetcode.com/problems/campus-bikes-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> int:
def dist(p1: List[int], p2: List[int]) -> int:
return abs(p1[0] - p2[0]) + abs(p1 …
Read More
107-Binary-Tree-Level-Order-Traversal-Ii
https://leetcode.com/problems/binary-tree-level-order-traversal-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
ans = []
q = deque([root])
while q:
currLevel = []
for _ in range(len(q)):
node = q.popleft …
Read More
1071-Greatest-Common-Divisor-Of-Strings
https://leetcode.com/problems/greatest-common-divisor-of-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def gcdOfStrings(self, str1: str, str2: str) -> str:
def mod(s1: str, s2: str) -> str:
while s1.startswith(s2):
s1 = s1[len(s2):]
return s1
if len(str1) < len …
Read More
1072-Flip-Columns-For-Maximum-Number-Of-Equal-Rows
https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
patterns = [tuple(a ^ row[0] for a in row) for row in matrix]
return max(Counter(patterns).values())
new Solution().maxEqualRowsAfterFlips …
Read More
1073-Adding-Two-Negabinary-Numbers
https://leetcode.com/problems/adding-two-negabinary-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]:
ans = []
carry = 0
while carry or arr1 or arr2:
if arr1:
carry += arr1.pop()
if arr2:
carry += arr2 …
Read More
1074-Number-Of-Submatrices-That-Sum-To-Target
https://leetcode.com/problems/number-of-submatrices-that-sum-to-target
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numSubmatrixSumTarget(self, matrix: List[List[int]], target: int) -> int:
m = len(matrix)
n = len(matrix[0])
ans = 0
# Transfer each row of matrix to prefix sum
for row …
Read More