533-Lonely-Pixel-Ii
https://leetcode.com/problems/lonely-pixel-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
m = len(picture)
n = len(picture[0])
ans = 0
rows = [row.count('B') for row in picture]
cols = [col …
Read More
537-Complex-Number-Multiplication
https://leetcode.com/problems/complex-number-multiplication
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def complexNumberMultiply(self, a: str, b: str) -> str:
def getRealAndImag(s: str) -> tuple:
return int(s[:s.index('+')]), int(s[s.index('+') + 1:-1])
A, B = getRealAndImag(a)
C …
Read More
538-Convert-Bst-To-Greater-Tree
https://leetcode.com/problems/convert-bst-to-greater-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
prefix = 0
def reversedInorder(root: Optional[TreeNode]) -> None:
nonlocal prefix
if not root:
return
reversedInorder(root.right)
prefix += root.val
root …
Read More
539-Minimum-Time-Difference
https://leetcode.com/problems/minimum-time-difference
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMinDifference(self, timePoints: List[str]) -> int:
ans = 24 * 60
nums = sorted([int(timePoint[:2]) * 60 + int(timePoint[3:])
for timePoint in timePoints])
for a, b in zip(nums …
Read More
54-Spiral-Matrix
https://leetcode.com/problems/spiral-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if not matrix:
return []
m = len(matrix)
n = len(matrix[0])
ans = []
r1 = 0
c1 = 0
r2 = m - 1
c2 …
Read More
540-Single-Element-In-A-Sorted-Array
https://leetcode.com/problems/single-element-in-a-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
l = 0
r = len(nums) - 1
while l < r:
m = (l + r) // 2
if m % 2 == 1:
m -= 1
if nums[m …
Read More
541-Reverse-String-Ii
https://leetcode.com/problems/reverse-string-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseStr(self, s: str, k: int) -> str:
return s[:k][::-1] + s[k:2 * k] + self.reverseStr(s[2 * k:], k) if s else ""
new Solution().reverseStr()
Score …
Read More
542-01-Matrix
https://leetcode.com/problems/01-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
m = len(mat)
n = len(mat[0])
dirs = [0, 1, 0, -1, 0]
q = deque()
seen = [[False] * n for …
Read More
543-Diameter-Of-Binary-Tree
https://leetcode.com/problems/diameter-of-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
ans = 0
def maxDepth(root: Optional[TreeNode]) -> int:
nonlocal ans
if not root:
return 0
l = maxDepth(root.left)
r = maxDepth(root …
Read More
544-Output-Contest-Matches
https://leetcode.com/problems/output-contest-matches
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findContestMatch(self, n: int) -> str:
def generateMatches(matches: List[str]) -> str:
if len(matches) == 1:
return matches[0]
nextMatches = []
for i in range(len(matches) // 2):
nextMatches.append …
Read More