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
74-Search-A-2D-Matrix
https://leetcode.com/problems/search-a-2d-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix:
return False
m = len(matrix)
n = len(matrix[0])
l = 0
r = m * n
while l …
Read More
744-Find-Smallest-Letter-Greater-Than-Target
https://leetcode.com/problems/find-smallest-letter-greater-than-target
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
l = 0
r = len(letters)
while l < r:
m = (l + r) >> 1
if letters[m] <= target:
l = m + 1
else …
Read More
746-Min-Cost-Climbing-Stairs
https://leetcode.com/problems/min-cost-climbing-stairs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
cost.append(0)
for i in range(2, len(cost)):
cost[i] += min(cost[i - 1], cost[i - 2])
return cost[-1 …
Read More
747-Largest-Number-At-Least-Twice-Of-Others
https://leetcode.com/problems/largest-number-at-least-twice-of-others
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def dominantIndex(self, nums: List[int]) -> int:
max = 0
secondMax = 0
for i, num in enumerate(nums):
if num > max:
secondMax = max
max = num
ans = i
elif num > secondMax …
Read More
748-Shortest-Completing-Word
https://leetcode.com/problems/shortest-completing-word
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
def isMatch(word: str) -> bool:
wordCount = Counter(word)
return False if any(wordCount[i] < count[i] for i in string …
Read More