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
656-Coin-Path
https://leetcode.com/problems/coin-path
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def cheapestJump(self, coins: List[int], maxJump: int) -> List[int]:
if coins[-1] == -1:
return []
n = len(coins)
# dp[i] := min cost to jump from i to n - 1 …
Read More
657-Robot-Return-To-Origin
https://leetcode.com/problems/robot-return-to-origin
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def judgeCircle(self, moves: str) -> bool:
return moves.count('R') == moves.count('L') and moves.count('U') == moves.count('D')
new Solution().judgeCircle()
Score: 5
Read More
658-Find-K-Closest-Elements
https://leetcode.com/problems/find-k-closest-elements
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
l = 0
r = len(arr) - k
while l < r:
m = (l + r) // 2
if x - arr[m …
Read More
66-Plus-One
https://leetcode.com/problems/plus-one
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
for i, d in reversed(list(enumerate(digits))):
if d < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1 …
Read More