726-Number-Of-Atoms
https://leetcode.com/problems/number-of-atoms
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countOfAtoms(self, formula: str) -> str:
def parse() -> dict:
ans = defaultdict(int)
nonlocal i
while i < n:
if formula[i] == '(':
i += 1
for elem, freq in parse().items():
ans …
Read More
728-Self-Dividing-Numbers
https://leetcode.com/problems/self-dividing-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def selfDividingNumbers(self, left: int, right: int) -> List[int]:
return [num for num in range(left, right + 1) if all(n != 0 and num % n == 0 for n in …
Read More
73-Set-Matrix-Zeroes
https://leetcode.com/problems/set-matrix-zeroes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
m = len(matrix)
n = len(matrix[0])
shouldFillFirstRow = 0 in matrix[0]
shouldFillFirstCol = 0 in list(zip(*matrix))[0]
# Store the …
Read More
730-Count-Different-Palindromic-Subsequences
https://leetcode.com/problems/count-different-palindromic-subsequences
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countPalindromicSubsequences(self, s: str) -> int:
def count(l: int, r: int) -> int:
if l > r:
return 0
if l == r:
return 1
key = l * len(s) + r
if …
Read More
733-Flood-Fill
https://leetcode.com/problems/flood-fill
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def floodFill(self, image: List[List[int]],
sr: int, sc: int, newColor: int) -> List[List[int]]:
startColor = image[sr][sc]
seen = set()
def dfs(i: int, j: int) -> None …
Read More
734-Sentence-Similarity
https://leetcode.com/problems/sentence-similarity
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def areSentencesSimilar(self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]) -> bool:
if len(sentence1) != len(sentence2):
return False
# map[key] := all similar words of key
map …
Read More
735-Asteroid-Collision
https://leetcode.com/problems/asteroid-collision
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
stack = []
for a in asteroids:
if a > 0:
stack.append(a)
else: # A < 0
# Destroy previous positive one(s)
while stack …
Read More
736-Parse-Lisp-Expression
https://leetcode.com/problems/parse-lisp-expression
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def evaluate(self, expression: str) -> int:
def evaluate(e: str, prevScope: dict) -> int:
if e[0].isdigit() or e[0] == '-':
return int(e)
if e in prevScope:
return prevScope …
Read More
737-Sentence-Similarity-Ii
https://leetcode.com/problems/sentence-similarity-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def areSentencesSimilarTwo(self, words1: List[str], words2: List[str], pairs: List[List[str]]) -> bool:
if len(words1) != len(words2):
return False
# graph[key] := all similar words of key
graph …
Read More
739-Daily-Temperatures
https://leetcode.com/problems/daily-temperatures
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
ans = [0] * len(temperatures)
stack = []
for i, t in enumerate(temperatures):
while stack and t > temperatures[stack[-1]]:
index = stack.pop …
Read More