62-Unique-Paths

Sat 17 May 2025

https://leetcode.com/problems/unique-paths

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

621-Task-Scheduler

Sat 17 May 2025

https://leetcode.com/problems/task-scheduler

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

624-Maximum-Distance-In-Arrays

Sat 17 May 2025

https://leetcode.com/problems/maximum-distance-in-arrays

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

625-Minimum-Factorization

Sat 17 May 2025

https://leetcode.com/problems/minimum-factorization

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

628-Maximum-Product-Of-Three-Numbers

Sat 17 May 2025

https://leetcode.com/problems/maximum-product-of-three-numbers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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

Category: leetcode

Read More

629-K-Inverse-Pairs-Array

Sat 17 May 2025

https://leetcode.com/problems/k-inverse-pairs-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

63-Unique-Paths-Ii

Sat 17 May 2025

https://leetcode.com/problems/unique-paths-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

630-Course-Schedule-Iii

Sat 17 May 2025

https://leetcode.com/problems/course-schedule-iii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

632-Smallest-Range-Covering-Elements-From-K-Lists

Sat 17 May 2025

https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

633-Sum-Of-Square-Numbers

Sat 17 May 2025

https://leetcode.com/problems/sum-of-square-numbers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 47 of 77

« Prev Next »