330-Patching-Array

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minPatches(self, nums: List[int], n: int) -> int:
    ans = 0
    i = 0     # Point to nums
    miss = 1  # Min sum in [1, n] we might miss

    while miss <= n …

Category: leetcode

Read More

331-Verify-Preorder-Serialization-Of-A-Binary-Tree

Sat 17 May 2025

https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isValidSerialization(self, preorder: str) -> bool:
    degree = 1  # OutDegree (children) - inDegree (parent)

    for node in preorder.split(','):
      degree -= 1
      if degree < 0:
        return False
      if node != '#':
        degree += 2

    return …

Category: leetcode

Read More

332-Reconstruct-Itinerary

Sat 17 May 2025

https://leetcode.com/problems/reconstruct-itinerary

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findItinerary(self, tickets: List[List[str]]) -> List[str]:
    ans = []
    graph = defaultdict(list)

    for a, b in reversed(sorted(tickets)):
      graph[a].append(b)

    def dfs(u: str) -> None …

Category: leetcode

Read More

334-Increasing-Triplet-Subsequence

Sat 17 May 2025

https://leetcode.com/problems/increasing-triplet-subsequence

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def increasingTriplet(self, nums: List[int]) -> bool:
    first = math.inf
    second = math.inf

    for num in nums:
      if num <= first:
        first = num
      elif num <= second:  # First < num <= second
        second …

Category: leetcode

Read More

335-Self-Crossing

Sat 17 May 2025

https://leetcode.com/problems/self-crossing

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isSelfCrossing(self, x: List[int]) -> bool:
    if len(x) <= 3:
      return False

    for i in range(3, len(x)):
      if x[i - 2] <= x[i] and x[i …

Category: leetcode

Read More

336-Palindrome-Pairs

Sat 17 May 2025

https://leetcode.com/problems/palindrome-pairs

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def palindromePairs(self, words: List[str]) -> List[List[int]]:
    ans = []
    dict = {word[::-1]: i for i, word in enumerate(words)}

    for i, word in enumerate(words):
      if "" in dict …

Category: leetcode

Read More

337-House-Robber-Iii

Sat 17 May 2025

https://leetcode.com/problems/house-robber-iii

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

      robLeft, notRobLeft = robOrNot(root.left)
      robRight, notRobRight = robOrNot(root.right …

Category: leetcode

Read More

338-Counting-Bits

Sat 17 May 2025

https://leetcode.com/problems/counting-bits

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def countBits(self, n: int) -> List[int]:
    # Let f(i) := i's # Of 1's in bitmask
    # F(i) = f(i / 2) + i % 2
    ans = [0] * (n + 1)

    for …

Category: leetcode

Read More

339-Nested-List-Weight-Sum

Sat 17 May 2025

https://leetcode.com/problems/nested-list-weight-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def depthSum(self, nestedList: List[NestedInteger]) -> int:
    ans = 0
    depth = 0
    q = deque()

    def addIntegers(nestedList: List[NestedInteger]) -> None:
      for ni in nestedList:
        q.append(ni)

    addIntegers(nestedList)

    while …

Category: leetcode

Read More

34-Find-First-And-Last-Position-Of-Element-In-Sorted-Array

Sat 17 May 2025

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def searchRange(self, nums: List[int], target: int) -> List[int]:
    l = bisect_left(nums, target)
    if l == len(nums) or nums[l] != target:
      return -1, -1
    r = bisect_right(nums, target …

Category: leetcode

Read More
Page 28 of 77

« Prev Next »