124-Binary-Tree-Maximum-Path-Sum

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-maximum-path-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def maxPathSum(self, root: Optional[TreeNode]) -> int:
    ans = -math.inf

    def maxPathSumDownFrom(root: Optional[TreeNode]) -> int:
      nonlocal ans
      if not root:
        return 0

      l = max(0, maxPathSumDownFrom(root.left …

Category: leetcode

Read More

125-Valid-Palindrome

Sat 17 May 2025

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

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

    while l < r:
      while l < r and not s[l].isalnum():
        l += 1
      while l < r and …

Category: leetcode

Read More

126-Word-Ladder-Ii

Sat 17 May 2025

https://leetcode.com/problems/word-ladder-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:
    wordSet = set(wordList)
    if endWord not in wordList:
      return []

    # {"hit": ["hot"], "hot": ["dot", "lot"], ...}
    graph: Dict …

Category: leetcode

Read More

127-Word-Ladder

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
    wordSet = set(wordList)
    if endWord not in wordSet:
      return 0

    ans = 0
    q = deque([beginWord])

    while q:
      ans …

Category: leetcode

Read More

128-Longest-Consecutive-Sequence

Sat 17 May 2025

https://leetcode.com/problems/longest-consecutive-sequence

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

    for num in nums:
      if num - 1 in seen:
        continue
      length = 0
      while num in seen:
        num …

Category: leetcode

Read More

129-Sum-Root-To-Leaf-Numbers

Sat 17 May 2025

https://leetcode.com/problems/sum-root-to-leaf-numbers

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

    def dfs(root: Optional[TreeNode], path: int) -> None:
      nonlocal ans
      if not root:
        return
      if not root.left and not …

Category: leetcode

Read More

13-Roman-To-Integer

Sat 17 May 2025

https://leetcode.com/problems/roman-to-integer

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def romanToInt(self, s: str) -> int:
    ans = 0
    roman = {'I': 1, 'V': 5, 'X': 10, 'L': 50,
             'C': 100, 'D': 500, 'M': 1000}

    for a, b in zip(s …

Category: leetcode

Read More

130-Surrounded-Regions

Sat 17 May 2025

https://leetcode.com/problems/surrounded-regions

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def solve(self, board: List[List[str]]) -> None:
    if not board:
      return

    m = len(board)
    n = len(board[0])
    dirs = [0, 1, 0, -1, 0]
    q = deque()

    for i …

Category: leetcode

Read More

131-Palindrome-Partitioning

Sat 17 May 2025

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

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

    def isPalindrome(s: str) -> bool:
      return s == s[::-1]

    def dfs(s: str, j: int, path: List[str], ans: List …

Category: leetcode

Read More

132-Palindrome-Partitioning-Ii

Sat 17 May 2025

https://leetcode.com/problems/palindrome-partitioning-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minCut(self, s: str) -> int:
    n = len(s)
    cut = [0] * n
    dp = [[False] * n for _ in range(n)]

    for i in range(n):
      mini = i
      for j …

Category: leetcode

Read More
Page 9 of 77

« Prev Next »