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
55-Jump-Game
https://leetcode.com/problems/jump-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canJump(self, nums: List[int]) -> bool:
i = 0
reach = 0
while i < len(nums) and i <= reach:
reach = max(reach, i + nums[i])
i += 1
return i == len …
Read More
551-Student-Attendance-Record-I
https://leetcode.com/problems/student-attendance-record-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkRecord(self, s: str) -> bool:
return s.count('A') <= 1 and 'LLL' not in s
new Solution().checkRecord()
Score: 5
Read More
552-Student-Attendance-Record-Ii
https://leetcode.com/problems/student-attendance-record-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkRecord(self, n: int) -> int:
kMod = 1_000_000_007
# dp[i][j] := length so far w/ i A's and the latest chars are j L's
dp = [[0 …
Read More
553-Optimal-Division
https://leetcode.com/problems/optimal-division
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def optimalDivision(self, nums: List[int]) -> str:
ans = str(nums[0])
if len(nums) == 1:
return ans
if len(nums) == 2:
return ans + '/' + str(nums[1])
ans += '/(' + str(nums …
Read More
554-Brick-Wall
https://leetcode.com/problems/brick-wall
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def leastBricks(self, wall: List[List[int]]) -> int:
maxFreq = 0
count = defaultdict(int)
for row in wall:
prefix = 0
for i in range(len(row) - 1):
prefix += row[i …
Read More
555-Split-Concatenated-Strings
https://leetcode.com/problems/split-concatenated-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def splitLoopedString(self, strs: List[str]) -> str:
ans = ''
sortedStrs = [max(s, s[::-1]) for s in strs]
for i, sortedStr in enumerate(sortedStrs):
for s in (sortedStr, sortedStr[::-1 …
Read More
556-Next-Greater-Element-Iii
https://leetcode.com/problems/next-greater-element-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextGreaterElement(self, n: int) -> int:
def nextPermutation(s: List[chr]) -> str:
i = len(s) - 2
while i >= 0:
if s[i] < s[i + 1]:
break
i -= 1
if …
Read More