634-Find-The-Derangement-Of-An-Array

Sat 17 May 2025

https://leetcode.com/problems/find-the-derangement-of-an-array

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

Category: leetcode

Read More

638-Shopping-Offers

Sat 17 May 2025

https://leetcode.com/problems/shopping-offers

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

Category: leetcode

Read More

64-Minimum-Path-Sum

Sat 17 May 2025

https://leetcode.com/problems/minimum-path-sum

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

Category: leetcode

Read More

640-Solve-The-Equation

Sat 17 May 2025

https://leetcode.com/problems/solve-the-equation

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

Category: leetcode

Read More

643-Maximum-Average-Subarray-I

Sat 17 May 2025

https://leetcode.com/problems/maximum-average-subarray-i

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

Category: leetcode

Read More

644-Maximum-Average-Subarray-Ii

Sat 17 May 2025

https://leetcode.com/problems/maximum-average-subarray-ii

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

Category: leetcode

Read More

645-Set-Mismatch

Sat 17 May 2025

https://leetcode.com/problems/set-mismatch

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

Category: leetcode

Read More

646-Maximum-Length-Of-Pair-Chain

Sat 17 May 2025

https://leetcode.com/problems/maximum-length-of-pair-chain

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

Category: leetcode

Read More

647-Palindromic-Substrings

Sat 17 May 2025

https://leetcode.com/problems/palindromic-substrings

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

Category: leetcode

Read More

648-Replace-Words

Sat 17 May 2025

https://leetcode.com/problems/replace-words

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

Category: leetcode

Read More
Page 48 of 77

« Prev Next »