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
545-Boundary-Of-Binary-Tree
https://leetcode.com/problems/boundary-of-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def boundaryOfBinaryTree(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
ans = [root.val]
def dfs(root: Optional[TreeNode], lb: bool, rb: bool):
"""
1. root.left is left …
Read More
546-Remove-Boxes
https://leetcode.com/problems/remove-boxes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeBoxes(self, boxes: List[int]) -> int:
# Dp(i, j, k) := max score of boxes[i..j] if k boxes equal to boxes[j]
@functools.lru_cache(None)
def dp …
Read More
547-Number-Of-Provinces
https://leetcode.com/problems/number-of-provinces
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class UnionFind:
def __init__(self, n: int):
self.count = n
self.id = list(range(n))
def union(self, u: int, v: int) -> None:
i = self.find(u)
j = self.find(v …
Read More