645-Set-Mismatch
https://leetcode.com/problems/set-mismatch
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
for num in nums:
if nums[abs(num) - 1] < 0:
duplicate = abs(num)
else:
nums[abs(num) - 1] *= -1
for i …
Read More
646-Maximum-Length-Of-Pair-Chain
https://leetcode.com/problems/maximum-length-of-pair-chain
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLongestChain(self, pairs: List[List[int]]) -> int:
ans = 0
prevEnd = -math.inf
for s, e in sorted(pairs, key=lambda x: x[1]):
if s > prevEnd:
ans += 1 …
Read More
647-Palindromic-Substrings
https://leetcode.com/problems/palindromic-substrings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countSubstrings(self, s: str) -> int:
def extendPalindromes(l: int, r: int) -> int:
count = 0
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1 …
Read More
648-Replace-Words
https://leetcode.com/problems/replace-words
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self):
self.root = {}
def insert(self, word: str) -> None:
node = self.root
for c in word:
if c not in node:
node[c] = {}
node = node[c]
node …
Read More
65-Valid-Number
https://leetcode.com/problems/valid-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isNumber(self, s: str) -> bool:
s = s.strip()
if not s:
return False
seenNum = False
seenDot = False
seenE = False
for i, c in enumerate(s):
if c == '.':
if …
Read More
650-2-Keys-Keyboard
https://leetcode.com/problems/2-keys-keyboard
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minSteps(self, n: int) -> int:
if n <= 1:
return 0
# dp[i] := min steps to get i 'A'
# Copy 'A', then paste 'A' i - 1 times
dp = [i …
Read More
652-Find-Duplicate-Subtrees
https://leetcode.com/problems/find-duplicate-subtrees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
ans = []
count = Counter()
def encode(root: Optional[TreeNode]) -> str:
if not root:
return ''
encoded = str(root.val) + '#' + \
encode(root.left …
Read More
653-Two-Sum-Iv-Input-Is-A-Bst
https://leetcode.com/problems/two-sum-iv-input-is-a-bst
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class BSTIterator:
def __init__(self, root: Optional[TreeNode], leftToRight: bool):
self.stack = []
self.leftToRight = leftToRight
self.pushUntilNone(root)
def next(self) -> int:
node = self.stack.pop()
if self.leftToRight:
self.pushUntilNone …
Read More
654-Maximum-Binary-Tree
https://leetcode.com/problems/maximum-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
def build(i: int, j: int) -> Optional[TreeNode]:
if i > j:
return None
maxNum = max(nums[i:j + 1])
maxIndex = nums …
Read More
655-Print-Binary-Tree
https://leetcode.com/problems/print-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def printTree(self, root: Optional[TreeNode]) -> List[List[str]]:
def maxHeight(root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(maxHeight(root.left), maxHeight(root.right …
Read More