1066-Campus-Bikes-Ii

Sat 17 May 2025

https://leetcode.com/problems/campus-bikes-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> int:
    def dist(p1: List[int], p2: List[int]) -> int:
      return abs(p1[0] - p2[0]) + abs(p1 …

Category: leetcode

Read More

107-Binary-Tree-Level-Order-Traversal-Ii

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-level-order-traversal-ii

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

    ans = []
    q = deque([root])

    while q:
      currLevel = []
      for _ in range(len(q)):
        node = q.popleft …

Category: leetcode

Read More

1071-Greatest-Common-Divisor-Of-Strings

Sat 17 May 2025

https://leetcode.com/problems/greatest-common-divisor-of-strings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def gcdOfStrings(self, str1: str, str2: str) -> str:
    def mod(s1: str, s2: str) -> str:
      while s1.startswith(s2):
        s1 = s1[len(s2):]
      return s1

    if len(str1) < len …

Category: leetcode

Read More

1072-Flip-Columns-For-Maximum-Number-Of-Equal-Rows

Sat 17 May 2025

https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
    patterns = [tuple(a ^ row[0] for a in row) for row in matrix]
    return max(Counter(patterns).values())
new Solution().maxEqualRowsAfterFlips …

Category: leetcode

Read More

1073-Adding-Two-Negabinary-Numbers

Sat 17 May 2025

https://leetcode.com/problems/adding-two-negabinary-numbers

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

    while carry or arr1 or arr2:
      if arr1:
        carry += arr1.pop()
      if arr2:
        carry += arr2 …

Category: leetcode

Read More

1074-Number-Of-Submatrices-That-Sum-To-Target

Sat 17 May 2025

https://leetcode.com/problems/number-of-submatrices-that-sum-to-target

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numSubmatrixSumTarget(self, matrix: List[List[int]], target: int) -> int:
    m = len(matrix)
    n = len(matrix[0])
    ans = 0

    # Transfer each row of matrix to prefix sum
    for row …

Category: leetcode

Read More

1078-Occurrences-After-Bigram

Sat 17 May 2025

https://leetcode.com/problems/occurrences-after-bigram

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
    words = text.split()
    return [c for a, b, c in zip(words, words[1:], words[2:]) if a …

Category: leetcode

Read More

1079-Letter-Tile-Possibilities

Sat 17 May 2025

https://leetcode.com/problems/letter-tile-possibilities

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numTilePossibilities(self, tiles: str) -> int:
    count = Counter(tiles)

    def dfs(count: Dict[int, int]) -> int:
      possibleSequences = 0

      for k, v in count.items():
        if v == 0:
          continue
        # Put …

Category: leetcode

Read More

108-Convert-Sorted-Array-To-Binary-Search-Tree

Sat 17 May 2025

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree

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

      m = (l + r) // 2
      return TreeNode(nums[m …

Category: leetcode

Read More

1081-Smallest-Subsequence-Of-Distinct-Characters

Sat 17 May 2025

https://leetcode.com/problems/smallest-subsequence-of-distinct-characters

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def smallestSubsequence(self, text: str) -> str:
    ans = []
    count = Counter(text)
    used = [False] * 26

    for c in text:
      count[c] -= 1
      if used[ord(c) - ord('a')]:
        continue
      while ans …

Category: leetcode

Read More
Page 6 of 77

« Prev Next »