62-Unique-Paths
https://leetcode.com/problems/unique-paths
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# dp[i][j] := unique paths from (0, 0) to (i, j)
dp = [[1] * n for _ in range(m)]
for i …
Read More
621-Task-Scheduler
https://leetcode.com/problems/task-scheduler
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:
count = Counter(tasks)
maxFreq = max(count.values())
# Put the most frequent task in the slot first
maxFreqTaskOccupy = (maxFreq - 1) * (n …
Read More
624-Maximum-Distance-In-Arrays
https://leetcode.com/problems/maximum-distance-in-arrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxDistance(self, arrays: List[List[int]]) -> int:
ans = 0
mini = 10000
maxi = -10000
for A in arrays:
ans = max(ans, A[-1] - mini, maxi - A[0])
mini = min …
Read More
625-Minimum-Factorization
https://leetcode.com/problems/minimum-factorization
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestFactorization(self, num: int) -> int:
if num == 1:
return 1
ans = 0
base = 1
for i in range(9, 1, -1):
while num % i == 0:
num //= i
ans …
Read More
628-Maximum-Product-Of-Three-Numbers
https://leetcode.com/problems/maximum-product-of-three-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
nums.sort()
return max(nums[-1] * nums[0] * nums[1],
nums[-1] * nums[-2] * nums[-3])
new Solution().maximumProduct()
Score: 5
Read More
629-K-Inverse-Pairs-Array
https://leetcode.com/problems/k-inverse-pairs-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def kInversePairs(self, n: int, k: int) -> int:
kMod = 1_000_000_007
# dp[i][j] := # Of permutations of numbers 1..i with j inverse pairs
dp = [[0] * (k + 1) for …
Read More
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