935-Knight-Dialer

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

942-Di-String-Match

Fri 14 November 2025

https://leetcode.com/problems/di-string-match

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def diStringMatch(self, S: str) -> List[int]:
    ans = []
    mini = 0
    maxi = len(S)

    for c in S:
      if c == 'I':
        ans.append(mini)
        mini += 1
      else:
        ans.append(maxi …

Category: leetcode

Read More

945-Minimum-Increment-To-Make-Array-Unique

Fri 14 November 2025

https://leetcode.com/problems/minimum-increment-to-make-array-unique

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

    A.sort()

    for a in A:
      ans += max(minAvailable - a, 0)
      minAvailable = max(minAvailable, a) + 1

    return ans …

Category: leetcode

Read More

946-Validate-Stack-Sequences

Fri 14 November 2025

https://leetcode.com/problems/validate-stack-sequences

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
    stack = []
    i = 0  # popped's index

    for x in pushed:
      stack.append(x)
      while stack and stack[-1] == popped …

Category: leetcode

Read More
Page 79 of 146

« Prev Next »