310-Minimum-Height-Trees
https://leetcode.com/problems/minimum-height-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
if n == 1 or not edges:
return [0]
ans = []
graph = defaultdict(set)
for u, v in edges:
graph …
Read More
311-Sparse-Matrix-Multiplication
https://leetcode.com/problems/sparse-matrix-multiplication
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def multiply(self, mat1: List[List[int]], mat2: List[List[int]]) -> List[List[int]]:
m = len(mat1)
n = len(mat2)
l = len(mat2[0])
ans = [[0] * l for _ …
Read More
312-Burst-Balloons
https://leetcode.com/problems/burst-balloons
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxCoins(self, nums: List[int]) -> int:
A = [1] + nums + [1]
@functools.lru_cache(None)
def dp(i: int, j: int) -> int:
if i > j:
return 0
return max(dp …
Read More
313-Super-Ugly-Number
https://leetcode.com/problems/super-ugly-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
k = len(primes)
nums = [1]
indices = [0] * k
while len(nums) < n:
nexts = [0] * k
for i in range(k …
Read More
314-Binary-Tree-Vertical-Order-Traversal
https://leetcode.com/problems/binary-tree-vertical-order-traversal
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
range_ = [0] * 2
def getRange(root: Optional[TreeNode], x: int) -> None:
if not root:
return
range_ …
Read More
315-Count-Of-Smaller-Numbers-After-Self
https://leetcode.com/problems/count-of-smaller-numbers-after-self
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class FenwickTree:
def __init__(self, n: int):
self.sums = [0] * (n + 1)
def update(self, i: int, delta: int) -> None:
while i < len(self.sums):
self.sums[i] += delta
i …
Read More
316-Remove-Duplicate-Letters
https://leetcode.com/problems/remove-duplicate-letters
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeDuplicateLetters(self, s: str) -> str:
ans = []
count = Counter(s)
used = [False] * 26
for c in s:
count[c] -= 1
if used[ord(c) - ord('a')]:
continue
while ans …
Read More
317-Shortest-Distance-From-All-Buildings
https://leetcode.com/problems/shortest-distance-from-all-buildings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shortestDistance(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
dirs = [0, 1, 0, -1, 0]
nBuildings = sum(a == 1 for row in grid …
Read More
318-Maximum-Product-Of-Word-Lengths
https://leetcode.com/problems/maximum-product-of-word-lengths
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxProduct(self, words: List[str]) -> int:
ans = 0
def getMask(word: str) -> int:
mask = 0
for c in word:
mask |= 1 << ord(c) - ord('a')
return mask
masks …
Read More
319-Bulb-Switcher
https://leetcode.com/problems/bulb-switcher
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def bulbSwitch(self, n: int) -> int:
return int(sqrt(n))
new Solution().bulbSwitch()
Score: 5
Read More