634-Find-The-Derangement-Of-An-Array
https://leetcode.com/problems/find-the-derangement-of-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findDerangement(self, n: int) -> int:
kMod = 1_000_000_007
@functools.lru_cache(None)
def dp(i: int) -> int:
if i == 0:
return 1
if i == 1:
return 0
return (i …
Read More
638-Shopping-Offers
https://leetcode.com/problems/shopping-offers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shoppingOffers(self, price: List[int], special: List[List[int]], needs: List[int]) -> int:
def dfs(s: int) -> int:
ans = 0
for i, need in enumerate(needs):
ans += need …
Read More
64-Minimum-Path-Sum
https://leetcode.com/problems/minimum-path-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
for i in range(m):
for j in range(n):
if i > 0 and …
Read More
640-Solve-The-Equation
https://leetcode.com/problems/solve-the-equation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def solveEquation(self, equation: str) -> str:
def calculate(s: str) -> tuple:
coefficient = 0
constant = 0
num = 0
sign = 1
for i, c in enumerate(s):
if c.isdigit():
num …
Read More
643-Maximum-Average-Subarray-I
https://leetcode.com/problems/maximum-average-subarray-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMaxAverage(self, nums: List[int], k: int) -> float:
summ = sum(nums[:k])
ans = summ
for i in range(k, len(nums)):
summ += nums[i] - nums[i - k]
ans …
Read More
644-Maximum-Average-Subarray-Ii
https://leetcode.com/problems/maximum-average-subarray-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMaxAverage(self, nums: List[int], k: int) -> float:
kErr = 1e-5
l = min(nums)
r = max(nums)
# True if there's a subarray with length >= k and average sum …
Read More
645-Set-Mismatch
https://leetcode.com/problems/set-mismatch
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
for num in nums:
if nums[abs(num) - 1] < 0:
duplicate = abs(num)
else:
nums[abs(num) - 1] *= -1
for i …
Read More
646-Maximum-Length-Of-Pair-Chain
https://leetcode.com/problems/maximum-length-of-pair-chain
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLongestChain(self, pairs: List[List[int]]) -> int:
ans = 0
prevEnd = -math.inf
for s, e in sorted(pairs, key=lambda x: x[1]):
if s > prevEnd:
ans += 1 …
Read More
647-Palindromic-Substrings
https://leetcode.com/problems/palindromic-substrings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countSubstrings(self, s: str) -> int:
def extendPalindromes(l: int, r: int) -> int:
count = 0
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1 …
Read More
648-Replace-Words
https://leetcode.com/problems/replace-words
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self):
self.root = {}
def insert(self, word: str) -> None:
node = self.root
for c in word:
if c not in node:
node[c] = {}
node = node[c]
node …
Read More