545-Boundary-Of-Binary-Tree

Fri 14 November 2025

https://leetcode.com/problems/boundary-of-binary-tree

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

    ans = [root.val]

    def dfs(root: Optional[TreeNode], lb: bool, rb: bool):
      """
      1. root.left is left …

Category: leetcode

Read More

546-Remove-Boxes

Fri 14 November 2025

https://leetcode.com/problems/remove-boxes

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def removeBoxes(self, boxes: List[int]) -> int:
    # Dp(i, j, k) := max score of boxes[i..j] if k boxes equal to boxes[j]
    @functools.lru_cache(None)
    def dp …

Category: leetcode

Read More

547-Number-Of-Provinces

Fri 14 November 2025

https://leetcode.com/problems/number-of-provinces

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class UnionFind:
  def __init__(self, n: int):
    self.count = n
    self.id = list(range(n))

  def union(self, u: int, v: int) -> None:
    i = self.find(u)
    j = self.find(v …

Category: leetcode

Read More

55-Jump-Game

Fri 14 November 2025

https://leetcode.com/problems/jump-game

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def canJump(self, nums: List[int]) -> bool:
    i = 0
    reach = 0

    while i < len(nums) and i <= reach:
      reach = max(reach, i + nums[i])
      i += 1

    return i == len …

Category: leetcode

Read More

551-Student-Attendance-Record-I

Fri 14 November 2025

https://leetcode.com/problems/student-attendance-record-i

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def checkRecord(self, s: str) -> bool:
    return s.count('A') <= 1 and 'LLL' not in s
new Solution().checkRecord()

Score: 5

Category: leetcode

Read More

552-Student-Attendance-Record-Ii

Fri 14 November 2025

https://leetcode.com/problems/student-attendance-record-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def checkRecord(self, n: int) -> int:
    kMod = 1_000_000_007
    # dp[i][j] := length so far w/ i A's and the latest chars are j L's
    dp = [[0 …

Category: leetcode

Read More

553-Optimal-Division

Fri 14 November 2025

https://leetcode.com/problems/optimal-division

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

    if len(nums) == 1:
      return ans
    if len(nums) == 2:
      return ans + '/' + str(nums[1])

    ans += '/(' + str(nums …

Category: leetcode

Read More

554-Brick-Wall

Fri 14 November 2025

https://leetcode.com/problems/brick-wall

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def leastBricks(self, wall: List[List[int]]) -> int:
    maxFreq = 0
    count = defaultdict(int)

    for row in wall:
      prefix = 0
      for i in range(len(row) - 1):
        prefix += row[i …

Category: leetcode

Read More

555-Split-Concatenated-Strings

Fri 14 November 2025

https://leetcode.com/problems/split-concatenated-strings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def splitLoopedString(self, strs: List[str]) -> str:
    ans = ''
    sortedStrs = [max(s, s[::-1]) for s in strs]

    for i, sortedStr in enumerate(sortedStrs):
      for s in (sortedStr, sortedStr[::-1 …

Category: leetcode

Read More

556-Next-Greater-Element-Iii

Fri 14 November 2025

https://leetcode.com/problems/next-greater-element-iii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def nextGreaterElement(self, n: int) -> int:
    def nextPermutation(s: List[chr]) -> str:
      i = len(s) - 2
      while i >= 0:
        if s[i] < s[i + 1]:
          break
        i -= 1

      if …

Category: leetcode

Read More
Page 49 of 146

« Prev Next »