330-Patching-Array
https://leetcode.com/problems/patching-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minPatches(self, nums: List[int], n: int) -> int:
ans = 0
i = 0 # Point to nums
miss = 1 # Min sum in [1, n] we might miss
while miss <= n …
Read More
331-Verify-Preorder-Serialization-Of-A-Binary-Tree
https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValidSerialization(self, preorder: str) -> bool:
degree = 1 # OutDegree (children) - inDegree (parent)
for node in preorder.split(','):
degree -= 1
if degree < 0:
return False
if node != '#':
degree += 2
return …
Read More
332-Reconstruct-Itinerary
https://leetcode.com/problems/reconstruct-itinerary
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
ans = []
graph = defaultdict(list)
for a, b in reversed(sorted(tickets)):
graph[a].append(b)
def dfs(u: str) -> None …
Read More
334-Increasing-Triplet-Subsequence
https://leetcode.com/problems/increasing-triplet-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
first = math.inf
second = math.inf
for num in nums:
if num <= first:
first = num
elif num <= second: # First < num <= second
second …
Read More
335-Self-Crossing
https://leetcode.com/problems/self-crossing
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isSelfCrossing(self, x: List[int]) -> bool:
if len(x) <= 3:
return False
for i in range(3, len(x)):
if x[i - 2] <= x[i] and x[i …
Read More
336-Palindrome-Pairs
https://leetcode.com/problems/palindrome-pairs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def palindromePairs(self, words: List[str]) -> List[List[int]]:
ans = []
dict = {word[::-1]: i for i, word in enumerate(words)}
for i, word in enumerate(words):
if "" in dict …
Read More
337-House-Robber-Iii
https://leetcode.com/problems/house-robber-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rob(self, root: Optional[TreeNode]) -> int:
def robOrNot(root: Optional[TreeNode]) -> tuple:
if not root:
return (0, 0)
robLeft, notRobLeft = robOrNot(root.left)
robRight, notRobRight = robOrNot(root.right …
Read More
338-Counting-Bits
https://leetcode.com/problems/counting-bits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countBits(self, n: int) -> List[int]:
# Let f(i) := i's # Of 1's in bitmask
# F(i) = f(i / 2) + i % 2
ans = [0] * (n + 1)
for …
Read More
339-Nested-List-Weight-Sum
https://leetcode.com/problems/nested-list-weight-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def depthSum(self, nestedList: List[NestedInteger]) -> int:
ans = 0
depth = 0
q = deque()
def addIntegers(nestedList: List[NestedInteger]) -> None:
for ni in nestedList:
q.append(ni)
addIntegers(nestedList)
while …
Read More
34-Find-First-And-Last-Position-Of-Element-In-Sorted-Array
https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
l = bisect_left(nums, target)
if l == len(nums) or nums[l] != target:
return -1, -1
r = bisect_right(nums, target …
Read More