63-Unique-Paths-Ii
https://leetcode.com/problems/unique-paths-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
m = len(obstacleGrid)
n = len(obstacleGrid[0])
# dp[i][j] := unique paths from (0, 0) to (i - 1, j - 1)
dp …
Read More
630-Course-Schedule-Iii
https://leetcode.com/problems/course-schedule-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def scheduleCourse(self, courses: List[List[int]]) -> int:
time = 0
maxHeap = []
for duration, lastDay in sorted(courses, key=lambda x: x[1]):
heapq.heappush(maxHeap, -duration)
time += duration
# If …
Read More
632-Smallest-Range-Covering-Elements-From-K-Lists
https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
minHeap = [(row[0], i, 0) for i, row in enumerate(nums)]
heapq.heapify(minHeap)
maxRange = max(row[0] for row …
Read More
633-Sum-Of-Square-Numbers
https://leetcode.com/problems/sum-of-square-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def judgeSquareSum(self, c: int) -> bool:
l = 0
r = int(sqrt(c))
while l <= r:
summ = l * l + r * r
if summ == c:
return True
if summ < c:
l …
Read More
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