907-Sum-Of-Subarray-Minimums

Sat 17 May 2025

https://leetcode.com/problems/sum-of-subarray-minimums

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def sumSubarrayMins(self, arr: List[int]) -> int:
    kMod = 1_000_000_007
    n = len(arr)
    ans = 0
    # prev[i] := index k s.t. arr[k] is the prev min in arr …

Category: leetcode

Read More

908-Smallest-Range-I

Sat 17 May 2025

https://leetcode.com/problems/smallest-range-i

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestRangeI(self, A: List[int], K: int) -> int:
    return max(0, max(A) - min(A) - 2 * K)
new Solution().smallestRangeI()

Score: 5

Category: leetcode

Read More

909-Snakes-And-Ladders

Sat 17 May 2025

https://leetcode.com/problems/snakes-and-ladders

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def snakesAndLadders(self, board: List[List[int]]) -> int:
    n = len(board)
    ans = 0
    q = deque([1])
    seen = set()
    A = [0] * (1 + n * n)  # 2D -> 1D

    for i …

Category: leetcode

Read More

91-Decode-Ways

Sat 17 May 2025

https://leetcode.com/problems/decode-ways

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numDecodings(self, s: str) -> int:
    n = len(s)
    # dp[i] := # Of ways to decode s[i..n)
    dp = [0] * n + [1]

    def isValid(a: chr, b=None) -> bool …

Category: leetcode

Read More

910-Smallest-Range-Ii

Sat 17 May 2025

https://leetcode.com/problems/smallest-range-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestRangeII(self, A: List[int], K: int) -> int:
    A.sort()

    ans = A[-1] - A[0]
    left = A[0] + K
    right = A[-1] - K

    for a, b in zip …

Category: leetcode

Read More

912-Sort-An-Array

Sat 17 May 2025

https://leetcode.com/problems/sort-an-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def sortArray(self, nums: List[int]) -> List[int]:
    def mergeSort(A: List[int], l: int, r: int) -> None:
      if l >= r:
        return

      def merge(A: List[int], l: int …

Category: leetcode

Read More

913-Cat-And-Mouse

Sat 17 May 2025

https://leetcode.com/problems/cat-and-mouse

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
from enum import IntEnum


class State(IntEnum):
  kDraw = 0
  kMouseWin = 1
  kCatWin = 2


class Solution:
  def catMouseGame(self, graph: List[List[int]]) -> int:
    n = len(graph)
    # Result of (cat, mouse, move …

Category: leetcode

Read More

914-X-Of-A-Kind-In-A-Deck-Of-Cards

Sat 17 May 2025

https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def hasGroupsSizeX(self, deck: List[int]) -> bool:
    count = Counter(deck)
    return functools.reduce(math.gcd, count.values()) >= 2
new Solution().hasGroupsSizeX()

Score: 5

Category: leetcode

Read More

915-Partition-Array-Into-Disjoint-Intervals

Sat 17 May 2025

https://leetcode.com/problems/partition-array-into-disjoint-intervals

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def partitionDisjoint(self, A: List[int]) -> int:
    n = len(A)
    mini = [0] * (n - 1) + [A[-1]]
    maxi = -math.inf

    for i in range(n - 2, - 1, -1):
      mini[i …

Category: leetcode

Read More

916-Word-Subsets

Sat 17 May 2025

https://leetcode.com/problems/word-subsets

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def wordSubsets(self, A: List[str], B: List[str]) -> List[str]:
    count = Counter()

    for b in B:
      count = count | Counter(b)

    return [a for a in A if Counter …

Category: leetcode

Read More
Page 70 of 77

« Prev Next »