65-Valid-Number

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isNumber(self, s: str) -> bool:
    s = s.strip()
    if not s:
      return False

    seenNum = False
    seenDot = False
    seenE = False

    for i, c in enumerate(s):
      if c == '.':
        if …

Category: leetcode

Read More

650-2-Keys-Keyboard

Sat 17 May 2025

https://leetcode.com/problems/2-keys-keyboard

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

    # dp[i] := min steps to get i 'A'
    # Copy 'A', then paste 'A' i - 1 times
    dp = [i …

Category: leetcode

Read More

652-Find-Duplicate-Subtrees

Sat 17 May 2025

https://leetcode.com/problems/find-duplicate-subtrees

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

    def encode(root: Optional[TreeNode]) -> str:
      if not root:
        return ''

      encoded = str(root.val) + '#' + \
          encode(root.left …

Category: leetcode

Read More

653-Two-Sum-Iv-Input-Is-A-Bst

Sat 17 May 2025

https://leetcode.com/problems/two-sum-iv-input-is-a-bst

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class BSTIterator:
  def __init__(self, root: Optional[TreeNode], leftToRight: bool):
    self.stack = []
    self.leftToRight = leftToRight
    self.pushUntilNone(root)

  def next(self) -> int:
    node = self.stack.pop()
    if self.leftToRight:
      self.pushUntilNone …

Category: leetcode

Read More

654-Maximum-Binary-Tree

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
    def build(i: int, j: int) -> Optional[TreeNode]:
      if i > j:
        return None

      maxNum = max(nums[i:j + 1])
      maxIndex = nums …

Category: leetcode

Read More

655-Print-Binary-Tree

Sat 17 May 2025

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

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

Category: leetcode

Read More

656-Coin-Path

Sat 17 May 2025

https://leetcode.com/problems/coin-path

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def cheapestJump(self, coins: List[int], maxJump: int) -> List[int]:
    if coins[-1] == -1:
      return []

    n = len(coins)
    # dp[i] := min cost to jump from i to n - 1 …

Category: leetcode

Read More

657-Robot-Return-To-Origin

Sat 17 May 2025

https://leetcode.com/problems/robot-return-to-origin

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def judgeCircle(self, moves: str) -> bool:
    return moves.count('R') == moves.count('L') and moves.count('U') == moves.count('D')
new Solution().judgeCircle()

Score: 5

Category: leetcode

Read More

658-Find-K-Closest-Elements

Sat 17 May 2025

https://leetcode.com/problems/find-k-closest-elements

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
    l = 0
    r = len(arr) - k

    while l < r:
      m = (l + r) // 2
      if x - arr[m …

Category: leetcode

Read More

66-Plus-One

Sat 17 May 2025

https://leetcode.com/problems/plus-one

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def plusOne(self, digits: List[int]) -> List[int]:
    for i, d in reversed(list(enumerate(digits))):
      if d < 9:
        digits[i] += 1
        return digits
      digits[i] = 0

    return [1 …

Category: leetcode

Read More
Page 49 of 77

« Prev Next »