1034-Coloring-A-Border

Sat 17 May 2025

https://leetcode.com/problems/coloring-a-border

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def colorBorder(self, grid: List[List[int]], r0: int, c0: int, color: int) -> List[List[int]]:
    def dfs(i: int, j: int, originalColor: int) -> None:
      if not 0 <= i …

Category: leetcode

Read More

1035-Uncrossed-Lines

Sat 17 May 2025

https://leetcode.com/problems/uncrossed-lines

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxUncrossedLines(self, A: List[int], B: List[int]) -> int:
    m = len(A)
    n = len(B)
    dp = [[0] * (n + 1) for _ in range(m + 1)]

    for i in …

Category: leetcode

Read More

1036-Escape-A-Large-Maze

Sat 17 May 2025

https://leetcode.com/problems/escape-a-large-maze

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isEscapePossible(self, blocked: List[List[int]], source: List[int], target: List[int]) -> bool:
    def dfs(i: int, j: int, target: List[int], visited: set) -> bool:
      if not 0 …

Category: leetcode

Read More

1037-Valid-Boomerang

Sat 17 May 2025

https://leetcode.com/problems/valid-boomerang

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isBoomerang(self, points: List[List[int]]) -> bool:
    return (points[1][0] - points[0][0]) * (points[2][1] - points[1][1]) != \
        (points[1][1] - points[0][1]) * (points[2 …

Category: leetcode

Read More

104-Maximum-Depth-Of-Binary-Tree

Sat 17 May 2025

https://leetcode.com/problems/maximum-depth-of-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxDepth(self, root: Optional[TreeNode]) -> int:
    if not root:
      return 0
    return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
new Solution().maxDepth()

Score: 5

Category: leetcode

Read More

1040-Moving-Stones-Until-Consecutive-Ii

Sat 17 May 2025

https://leetcode.com/problems/moving-stones-until-consecutive-ii

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

    stones.sort()

    l = 0
    for r, stone in enumerate(stones):
      while stone - stones[l] + 1 > n …

Category: leetcode

Read More

1041-Robot-Bounded-In-Circle

Sat 17 May 2025

https://leetcode.com/problems/robot-bounded-in-circle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isRobotBounded(self, instructions: str) -> bool:
    x = 0
    y = 0
    d = 0
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]

    for instruction in instructions:
      if instruction == 'G':
        x …

Category: leetcode

Read More

1042-Flower-Planting-With-No-Adjacent

Sat 17 May 2025

https://leetcode.com/problems/flower-planting-with-no-adjacent

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
    ans = [0] * n  # ans[i] := 1, 2, 3, or 4
    graph = [[] for _ in range(n)]

    for a …

Category: leetcode

Read More

1044-Longest-Duplicate-Substring

Sat 17 May 2025

https://leetcode.com/problems/longest-duplicate-substring

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def longestDupSubstring(self, s: str) -> str:
    kMod = 1_000_000_007
    bestStart = -1
    l = 1
    r = len(s)

    def val(c: str) -> int:
      return ord(c) - ord('a')

    # K := length of …

Category: leetcode

Read More

1048-Longest-String-Chain

Sat 17 May 2025

https://leetcode.com/problems/longest-string-chain

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

    # Dp(s) := longest chain where s is the last word
    @functools.lru_cache(None)
    def dp(s: str) -> int:
      ans …

Category: leetcode

Read More
Page 4 of 77

« Prev Next »