930-Binary-Subarrays-With-Sum

Sat 17 May 2025

https://leetcode.com/problems/binary-subarrays-with-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numSubarraysWithSum(self, A: List[int], S: int) -> int:
    ans = 0
    prefix = 0
    count = Counter({0: 1})

    for a in A:
      prefix += a
      ans += count[prefix - S]
      count[prefix …

Category: leetcode

Read More

931-Minimum-Falling-Path-Sum

Sat 17 May 2025

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

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

    for i in range(1, n):
      for j in range(n):
        mini = math.inf
        for k in range …

Category: leetcode

Read More

932-Beautiful-Array

Sat 17 May 2025

https://leetcode.com/problems/beautiful-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def beautifulArray(self, n: int) -> List[int]:
    A = [i for i in range(1, n + 1)]

    def partition(l: int, r: int, mask: int) -> int:
      nextSwapped = l
      for i …

Category: leetcode

Read More

935-Knight-Dialer

Sat 17 May 2025

https://leetcode.com/problems/knight-dialer

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def knightDialer(self, n: int) -> int:
    kMod = 1_000_000_007
    dirs = [(-2, 1), (-1, 2), (1, 2), (2, 1),
            (2, -1), (1, -2), (-1, -2), (-2, -1)]

    # dp[i][j …

Category: leetcode

Read More

936-Stamping-The-Sequence

Sat 17 May 2025

https://leetcode.com/problems/stamping-the-sequence

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def movesToStamp(self, stamp: str, target: str) -> List[int]:
    def stampify(s: int) -> int:
      stampified = len(stamp)

      for i, st in enumerate(stamp):
        if target[s + i] == '*':
          stampified -= 1 …

Category: leetcode

Read More

937-Reorder-Data-In-Log-Files

Sat 17 May 2025

https://leetcode.com/problems/reorder-data-in-log-files

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def reorderLogFiles(self, logs: List[str]) -> List[str]:
    digitLogs = []
    letterLogs = []

    for log in logs:
      i = log.index(' ')
      if log[i + 1].isdigit():
        digitLogs.append(log)
      else:
        letterLogs.append((log …

Category: leetcode

Read More

939-Minimum-Area-Rectangle

Sat 17 May 2025

https://leetcode.com/problems/minimum-area-rectangle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minAreaRect(self, points: List[List[int]]) -> int:
    ans = math.inf
    xToYs = defaultdict(set)

    for x, y in points:
      xToYs[x].add(y)

    for i in range(len(points …

Category: leetcode

Read More

94-Binary-Tree-Inorder-Traversal

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-inorder-traversal

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
    ans = []
    stack = []

    while root or stack:
      while root:
        stack.append(root)
        root = root.left
      root = stack.pop()
      ans.append(root.val …

Category: leetcode

Read More

940-Distinct-Subsequences-Ii

Sat 17 May 2025

https://leetcode.com/problems/distinct-subsequences-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def distinctSubseqII(self, s: str) -> int:
    kMod = 1_000_000_007
    # endsWith[i] := # Of subseqs ends with 'a' + i
    endsWith = [0] * 26

    for c in s:
      endsWith[ord(c) - ord('a …

Category: leetcode

Read More

941-Valid-Mountain-Array

Sat 17 May 2025

https://leetcode.com/problems/valid-mountain-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def validMountainArray(self, A: List[int]) -> bool:
    if len(A) < 3:
      return False

    l = 0
    r = len(A) - 1

    while l + 1 < len(A) and A[l] < A[l …

Category: leetcode

Read More
Page 72 of 77

« Prev Next »