525-Contiguous-Array

Sat 17 May 2025

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

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

    for i, num in enumerate(nums):
      prefix += 1 if num else -1
      if prefix in …

Category: leetcode

Read More

527-Word-Abbreviation

Sat 17 May 2025

https://leetcode.com/problems/word-abbreviation

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

    def getAbbrev(s: str, prefixIndex: int) -> str:
      n = len(s)
      num = n - (prefixIndex + 1) - 1
      numLength = 1 if …

Category: leetcode

Read More

528-Random-Pick-With-Weight

Sat 17 May 2025

https://leetcode.com/problems/random-pick-with-weight

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def __init__(self, w: List[int]):
    self.prefix = list(itertools.accumulate(w))

  def pickIndex(self) -> int:
    target = randint(0, self.prefix[-1] - 1)
    l = 0
    r = len(self.prefix …

Category: leetcode

Read More

529-Minesweeper

Sat 17 May 2025

https://leetcode.com/problems/minesweeper

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
    if board[click[0]][click[1]] == 'M':
      board[click[0]][click[1]] = 'X'
      return board

    dirs …

Category: leetcode

Read More

53-Maximum-Subarray

Sat 17 May 2025

https://leetcode.com/problems/maximum-subarray

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxSubArray(self, nums: List[int]) -> int:
    ans = -math.inf
    summ = 0

    for num in nums:
      summ += num
      ans = max(ans, summ)
      summ = max(summ, 0)

    return ans
new …

Category: leetcode

Read More

531-Lonely-Pixel-I

Sat 17 May 2025

https://leetcode.com/problems/lonely-pixel-i

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findLonelyPixel(self, picture: List[List[str]]) -> int:
    m = len(picture)
    n = len(picture[0])
    ans = 0
    rows = [0] * m  # rows[i] := # Of Bs in rows i
    cols = [0 …

Category: leetcode

Read More

532-K-Diff-Pairs-In-An-Array

Sat 17 May 2025

https://leetcode.com/problems/k-diff-pairs-in-an-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findPairs(self, nums: List[int], k: int) -> int:
    ans = 0
    numToIndex = {num: i for i, num in enumerate(nums)}

    for i, num in enumerate(nums):
      target = num + k …

Category: leetcode

Read More

533-Lonely-Pixel-Ii

Sat 17 May 2025

https://leetcode.com/problems/lonely-pixel-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
    m = len(picture)
    n = len(picture[0])
    ans = 0
    rows = [row.count('B') for row in picture]
    cols = [col …

Category: leetcode

Read More

537-Complex-Number-Multiplication

Sat 17 May 2025

https://leetcode.com/problems/complex-number-multiplication

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def complexNumberMultiply(self, a: str, b: str) -> str:
    def getRealAndImag(s: str) -> tuple:
      return int(s[:s.index('+')]), int(s[s.index('+') + 1:-1])

    A, B = getRealAndImag(a)
    C …

Category: leetcode

Read More

538-Convert-Bst-To-Greater-Tree

Sat 17 May 2025

https://leetcode.com/problems/convert-bst-to-greater-tree

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

    def reversedInorder(root: Optional[TreeNode]) -> None:
      nonlocal prefix
      if not root:
        return

      reversedInorder(root.right)
      prefix += root.val
      root …

Category: leetcode

Read More
Page 41 of 77

« Prev Next »