257-Binary-Tree-Paths

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-paths

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

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

Category: leetcode

Read More

258-Add-Digits

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def addDigits(self, num: int) -> int:
    return 0 if num == 0 else 1 + (num - 1) % 9
new Solution().addDigits()

Score: 5

Category: leetcode

Read More

259-3Sum-Smaller

Sat 17 May 2025

https://leetcode.com/problems/3sum-smaller

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def threeSumSmaller(self, nums: List[int], target: int) -> int:
    if len(nums) < 3:
      return 0

    ans = 0

    nums.sort()

    for i in range(len(nums) - 2):
      l = i + 1 …

Category: leetcode

Read More

26-Remove-Duplicates-From-Sorted-Array

Sat 17 May 2025

https://leetcode.com/problems/remove-duplicates-from-sorted-array

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

    for num in nums:
      if i < 1 or num > nums[i - 1]:
        nums[i] = num
        i += 1

    return i
new …

Category: leetcode

Read More

260-Single-Number-Iii

Sat 17 May 2025

https://leetcode.com/problems/single-number-iii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def singleNumber(self, nums: List[int]) -> List[int]:
    xors = functools.reduce(operator.xor, nums)
    lowbit = xors & -xors
    ans = [0, 0]

    # Seperate nums into two groups by the lowbit
    for …

Category: leetcode

Read More

261-Graph-Valid-Tree

Sat 17 May 2025

https://leetcode.com/problems/graph-valid-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def validTree(self, n: int, edges: List[List[int]]) -> bool:
    if n == 0 or len(edges) != n - 1:
      return False

    graph = [[] for _ in range(n)]
    q = deque([0 …

Category: leetcode

Read More

263-Ugly-Number

Sat 17 May 2025

https://leetcode.com/problems/ugly-number

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isUgly(self, n: int) -> bool:
    if n == 0:
      return False

    for prime in 2, 3, 5:
      while n % prime == 0:
        n //= prime

    return n == 1
new Solution().isUgly …

Category: leetcode

Read More

264-Ugly-Number-Ii

Sat 17 May 2025

https://leetcode.com/problems/ugly-number-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def nthUglyNumber(self, n: int) -> int:
    nums = [1]
    i2 = 0
    i3 = 0
    i5 = 0

    while len(nums) < n:
      next2 = nums[i2] * 2
      next3 = nums[i3] * 3
      next5 = nums[i5 …

Category: leetcode

Read More

265-Paint-House-Ii

Sat 17 May 2025

https://leetcode.com/problems/paint-house-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def minCostII(self, costs: List[List[int]]) -> int:
    prevIndex = -1  # The previous minimum index
    prevMin1 = 0    # Minimum cost so far
    prevMin2 = 0    # 2nd minimum cost so far

    for …

Category: leetcode

Read More

266-Palindrome-Permutation

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def canPermutePalindrome(self, s: str) -> bool:
    seen = set()

    for c in s:
      if c in seen:
        seen.remove(c)
      else:
        seen.add(c)

    return len(seen) <= 1
new Solution …

Category: leetcode

Read More
Page 21 of 77

« Prev Next »