1029-Two-City-Scheduling
https://leetcode.com/problems/two-city-scheduling
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def twoCitySchedCost(self, costs: List[List[int]]) -> int:
n = len(costs) // 2
# How much money can we save if we fly a person to A instead of B?
# To …
Read More
103-Binary-Tree-Zigzag-Level-Order-Traversal
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
ans = []
q = deque([root])
isLeftToRight = True
while q:
currLevel = []
for _ in range(len(q)):
if …
Read More
1031-Maximum-Sum-Of-Two-Non-Overlapping-Subarrays
https://leetcode.com/problems/maximum-sum-of-two-non-overlapping-subarrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
def helper(l: int, r: int) -> int:
n = len(nums)
left = [0] * n
summ = 0
for i in …
Read More
1033-Moving-Stones-Until-Consecutive
https://leetcode.com/problems/moving-stones-until-consecutive
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numMovesStones(self, a: int, b: int, c: int) -> List[int]:
nums = sorted([a, b, c])
if nums[2] - nums[0] == 2:
return [0, 0]
return [1 if min …
Read More
1034-Coloring-A-Border
https://leetcode.com/problems/coloring-a-border
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def colorBorder(self, grid: List[List[int]], r0: int, c0: int, color: int) -> List[List[int]]:
def dfs(i: int, j: int, originalColor: int) -> None:
if not 0 <= i …
Read More
1035-Uncrossed-Lines
https://leetcode.com/problems/uncrossed-lines
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxUncrossedLines(self, A: List[int], B: List[int]) -> int:
m = len(A)
n = len(B)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in …
Read More
1036-Escape-A-Large-Maze
https://leetcode.com/problems/escape-a-large-maze
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isEscapePossible(self, blocked: List[List[int]], source: List[int], target: List[int]) -> bool:
def dfs(i: int, j: int, target: List[int], visited: set) -> bool:
if not 0 …
Read More
1037-Valid-Boomerang
https://leetcode.com/problems/valid-boomerang
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isBoomerang(self, points: List[List[int]]) -> bool:
return (points[1][0] - points[0][0]) * (points[2][1] - points[1][1]) != \
(points[1][1] - points[0][1]) * (points[2 …
Read More
104-Maximum-Depth-Of-Binary-Tree
https://leetcode.com/problems/maximum-depth-of-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
new Solution().maxDepth()
Score: 5
Read More
1040-Moving-Stones-Until-Consecutive-Ii
https://leetcode.com/problems/moving-stones-until-consecutive-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numMovesStonesII(self, stones: List[int]) -> List[int]:
n = len(stones)
minMoves = n
stones.sort()
l = 0
for r, stone in enumerate(stones):
while stone - stones[l] + 1 > n …
Read More