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
1078-Occurrences-After-Bigram
https://leetcode.com/problems/occurrences-after-bigram
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
words = text.split()
return [c for a, b, c in zip(words, words[1:], words[2:]) if a …
Read More
1079-Letter-Tile-Possibilities
https://leetcode.com/problems/letter-tile-possibilities
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
count = Counter(tiles)
def dfs(count: Dict[int, int]) -> int:
possibleSequences = 0
for k, v in count.items():
if v == 0:
continue
# Put …
Read More
108-Convert-Sorted-Array-To-Binary-Search-Tree
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
def build(l: int, r: int) -> Optional[TreeNode]:
if l > r:
return None
m = (l + r) // 2
return TreeNode(nums[m …
Read More
1081-Smallest-Subsequence-Of-Distinct-Characters
https://leetcode.com/problems/smallest-subsequence-of-distinct-characters
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestSubsequence(self, text: str) -> str:
ans = []
count = Counter(text)
used = [False] * 26
for c in text:
count[c] -= 1
if used[ord(c) - ord('a')]:
continue
while ans …
Read More