1019-Next-Greater-Node-In-Linked-List
https://leetcode.com/problems/next-greater-node-in-linked-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextLargerNodes(self, head: ListNode) -> List[int]:
ans = []
stack = []
while head:
while stack and head.val > ans[stack[-1]]:
index = stack.pop()
ans[index] = head.val
stack.append(len …
Read More
102-Binary-Tree-Level-Order-Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def levelOrder(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
1023-Camelcase-Matching
https://leetcode.com/problems/camelcase-matching
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
def isMatch(q: str) -> bool:
j = 0
for i, c in enumerate(q):
if j < len(pattern) and c …
Read More
1024-Video-Stitching
https://leetcode.com/problems/video-stitching
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def videoStitching(self, clips: List[List[int]], time: int) -> int:
ans = 0
end = 0
farthest = 0
clips.sort()
i = 0
while farthest < time:
while i < len(clips) and clips …
Read More
1025-Divisor-Game
https://leetcode.com/problems/divisor-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def divisorGame(self, N: int) -> bool:
return N % 2 == 0
new Solution().divisorGame()
Score: 5
Read More
1027-Longest-Arithmetic-Subsequence
https://leetcode.com/problems/longest-arithmetic-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestArithSeqLength(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
# dp[i][k] := length of the longest arithmetic subseq ofnums
# nums[0..i] with k = diff + 500 …
Read More
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