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

1015-Smallest-Integer-Divisible-By-K

Sat 17 May 2025

https://leetcode.com/problems/smallest-integer-divisible-by-k

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestRepunitDivByK(self, K: int) -> int:
    if K % 10 not in {1, 3, 7, 9}:
      return -1

    seen = set()
    N = 0

    for length in range(1, K + 1):
      N …

Category: leetcode

Read More

1016-Binary-String-With-Substrings-Representing-1-To-N

Sat 17 May 2025

https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def queryString(self, S: str, N: int) -> bool:
    if N > 1511:
      return False

    for i in range(N, N // 2, -1):
      if format(i, 'b') not in S:
        return …

Category: leetcode

Read More

1017-Convert-To-Base-2

Sat 17 May 2025

https://leetcode.com/problems/convert-to-base-2

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def baseNeg2(self, N: int) -> str:
    ans = ''

    while N:
      ans = str(N & 1) + ans
      N = -(N >> 1)

    return '0' if ans == '' else ans
new Solution().baseNeg2()

Score: 5

Category: leetcode

Read More

1018-Binary-Prefix-Divisible-By-5

Sat 17 May 2025

https://leetcode.com/problems/binary-prefix-divisible-by-5

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

    for a in A:
      num = (num * 2 + a) % 5
      ans.append(num % 5 == 0)

    return ans
new Solution …

Category: leetcode

Read More
Page 2 of 77

« Prev Next »