1-Two-Sum
https://leetcode.com/problems/two-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
numToIndex = {}
for i, num in enumerate(nums):
if target - num in numToIndex:
return numToIndex[target - num], i
numToIndex[num] = i
Score: 5
1-Two-Sums
# https://leetcode.com/problems/two-sum
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml311; pyv: 3.11.10 (main, Oct 3 2024, 07:29:13) [GCC 11.2.0]'
print(pyu.ps2("haystack-ai ollama-haystack python-dotenv"))
haystack-ai==2.8.0
ollama-haystack is not installed in the current environment.
python-dotenv==0.21 …
Read More
10-Regular-Expression-Matching
https://leetcode.com/problems/regular-expression-matching
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isMatch(self, s: str, p: str) -> bool:
m = len(s)
n = len(p)
# dp[i][j] := True if s[0..i) matches p[0..j)
dp = [[False] * (n …
Read More
100-Same-Tree
https://leetcode.com/problems/same-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p or not q:
return p == q
return p.val == q.val and \
self.isSameTree(p.left …
Read More
1001-Grid-Illumination
https://leetcode.com/problems/grid-illumination
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def gridIllumination(self, N: int, lamps: List[List[int]], queries: List[List[int]]) -> List[int]:
ans = []
rows = Counter()
cols = Counter()
diag1 = Counter()
diag2 = Counter()
lampsSet = set()
for i, j …
Read More
1002-Find-Common-Characters
https://leetcode.com/problems/find-common-characters
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def commonChars(self, A: List[str]) -> List[str]:
ans = []
commonCount = [math.inf] * 26
for a in A:
count = [0] * 26
for c in a:
count[ord(c) - ord('a …
Read More
1003-Check-If-Word-Is-Valid-After-Substitutions
https://leetcode.com/problems/check-if-word-is-valid-after-substitutions
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for c in s:
if c == 'c':
if len(stack) < 2 or stack[-2] != 'a' or stack[-1] != 'b':
return False
stack …
Read More
1006-Clumsy-Factorial
https://leetcode.com/problems/clumsy-factorial
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def clumsy(self, N: int) -> int:
if N == 1:
return 1
if N == 2:
return 2
if N == 3:
return 6
if N == 4:
return 7
if N % 4 …
Read More
1008-Construct-Binary-Search-Tree-From-Preorder-Traversal
https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def bstFromPreorder(self, preorder: List[int]) -> Optional[TreeNode]:
root = TreeNode(preorder[0])
stack = [root]
for i in range(1, len(preorder)):
parent = stack[-1]
child = TreeNode(preorder[i])
# Adjust …
Read More
1009-Complement-Of-Base-10-Integer
https://leetcode.com/problems/complement-of-base-10-integer
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def bitwiseComplement(self, N: int) -> int:
mask = 1
while mask < N:
mask = (mask << 1) + 1
return mask ^ N
new Solution().bitwiseComplement()
Score: 5
Read More