41-First-Missing-Positive

Sat 17 May 2025

https://leetcode.com/problems/first-missing-positive

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

    # Correct slot:
    # nums[i] = i + 1
    # nums[i] - 1 = i
    # nums[nums[i] - 1] = nums[i]
    for i in …

Category: leetcode

Read More

410-Split-Array-Largest-Sum

Sat 17 May 2025

https://leetcode.com/problems/split-array-largest-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def splitArray(self, nums: List[int], m: int) -> int:
    n = len(nums)
    prefix = [0] + list(itertools.accumulate(nums))

    # Dp(i, k) := min of largest sum to split first i …

Category: leetcode

Read More

411-Minimum-Unique-Word-Abbreviation

Sat 17 May 2025

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

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

    def getMask(word: str) -> int:
      # mask[i] = 0 := target[i] == word[i]
      # mask[i] = 1 := target[i …

Category: leetcode

Read More

412-Fizz-Buzz

Sat 17 May 2025

https://leetcode.com/problems/fizz-buzz

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def fizzBuzz(self, n: int) -> List[str]:
    d = {3: 'Fizz', 5: 'Buzz'}
    return [''.join([d[k] for k in d if i % k == 0]) or str(i) for i …

Category: leetcode

Read More

415-Add-Strings

Sat 17 May 2025

https://leetcode.com/problems/add-strings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def addStrings(self, num1: str, num2: str) -> str:
    ans = []
    carry = 0
    i = len(num1) - 1
    j = len(num2) - 1

    while i >= 0 or j >= 0 or carry:
      if i …

Category: leetcode

Read More

416-Partition-Equal-Subset-Sum

Sat 17 May 2025

https://leetcode.com/problems/partition-equal-subset-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def canPartition(self, nums: List[int]) -> bool:
    summ = sum(nums)
    if summ & 1:
      return False
    return self.knapsack_(nums, summ // 2)

  def knapsack_(self, nums: List[int], subsetSum: int …

Category: leetcode

Read More

417-Pacific-Atlantic-Water-Flow

Sat 17 May 2025

https://leetcode.com/problems/pacific-atlantic-water-flow

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
    m = len(heights)
    n = len(heights[0])
    dirs = [0, 1, 0, -1, 0]
    qP = deque()
    qA = deque()
    seenP = [[False …

Category: leetcode

Read More

418-Sentence-Screen-Fitting

Sat 17 May 2025

https://leetcode.com/problems/sentence-screen-fitting

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
    combined = ' '.join(sentence) + ' '
    n = len(combined)
    i = 0

    for _ in range(rows):
      i += cols
      if combined[i …

Category: leetcode

Read More

42-Trapping-Rain-Water

Sat 17 May 2025

https://leetcode.com/problems/trapping-rain-water

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def trap(self, height: List[int]) -> int:
    n = len(height)
    l = [0] * n  # l[i] := max(height[0..i])
    r = [0] * n  # r[i] := max(height[i..n))

    for …

Category: leetcode

Read More

423-Reconstruct-Original-Digits-From-English

Sat 17 May 2025

https://leetcode.com/problems/reconstruct-original-digits-from-english

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def originalDigits(self, s: str) -> str:
    count = [0] * 10

    for c in s:
      if c == 'z':
        count[0] += 1
      if c == 'o':
        count[1] += 1
      if c == 'w':
        count …

Category: leetcode

Read More
Page 33 of 77

« Prev Next »