1003-Check-If-Word-Is-Valid-After-Substitutions

Sat 17 May 2025

https://leetcode.com/problems/check-if-word-is-valid-after-substitutions

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isValid(self, s: str) -> bool:
    stack = []

    for c in s:
      if c == 'c':
        if len(stack) < 2 or stack[-2] != 'a' or stack[-1] != 'b':
          return False
        stack …

Category: leetcode

Read More

1006-Clumsy-Factorial

Sat 17 May 2025

https://leetcode.com/problems/clumsy-factorial

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def clumsy(self, N: int) -> int:
    if N == 1:
      return 1
    if N == 2:
      return 2
    if N == 3:
      return 6
    if N == 4:
      return 7
    if N % 4 …

Category: leetcode

Read More

1008-Construct-Binary-Search-Tree-From-Preorder-Traversal

Sat 17 May 2025

https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal

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

    for i in range(1, len(preorder)):
      parent = stack[-1]
      child = TreeNode(preorder[i])
      # Adjust …

Category: leetcode

Read More

1009-Complement-Of-Base-10-Integer

Sat 17 May 2025

https://leetcode.com/problems/complement-of-base-10-integer

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def bitwiseComplement(self, N: int) -> int:
    mask = 1

    while mask < N:
      mask = (mask << 1) + 1

    return mask ^ N
new Solution().bitwiseComplement()

Score: 5

Category: leetcode

Read More

101-Symmetric-Tree

Sat 17 May 2025

https://leetcode.com/problems/symmetric-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isSymmetric(self, root: Optional[TreeNode]) -> bool:
    def isSymmetric(p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
      if not p or not q:
        return p == q

      return p.val == q …

Category: leetcode

Read More

1010-Pairs-Of-Songs-With-Total-Durations-Divisible-By-60

Sat 17 May 2025

https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numPairsDivisibleBy60(self, time: List[int]) -> int:
    ans = 0
    count = [0] * 60

    for t in time:
      t %= 60
      ans += count[(60 - t) % 60]
      count[t] += 1

    return ans
new …

Category: leetcode

Read More

1011-Capacity-To-Ship-Packages-Within-D-Days

Sat 17 May 2025

https://leetcode.com/problems/capacity-to-ship-packages-within-d-days

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def shipWithinDays(self, weights: List[int], days: int) -> int:
    l = max(weights)
    r = sum(weights)

    def shipDays(shipCapacity: int) -> int:
      days = 1
      capacity = 0
      for weight in weights:
        if …

Category: leetcode

Read More

1012-Numbers-With-Repeated-Digits

Sat 17 May 2025

https://leetcode.com/problems/numbers-with-repeated-digits

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numDupDigitsAtMostN(self, n: int) -> int:
    return n - self._countSpecialNumbers(n)

  def _countSpecialNumbers(self, n: int) -> int:
    s = str(n)
    digitSize = int(log10(n)) + 1

    # Dp(i, j, k …

Category: leetcode

Read More

1013-Partition-Array-Into-Three-Parts-With-Equal-Sum

Sat 17 May 2025

https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def canThreePartsEqualSum(self, A: List[int]) -> bool:
    summ = sum(A)
    prefix = 0
    parts = 1

    for a in A:
      prefix += a
      if prefix == summ * parts // 3:
        parts += 1

    return summ …

Category: leetcode

Read More

1014-Best-Sightseeing-Pair

Sat 17 May 2025

https://leetcode.com/problems/best-sightseeing-pair

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

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

    return ans
new Solution …

Category: leetcode

Read More
Page 3 of 146

« Prev Next »