515-Find-Largest-Value-In-Each-Tree-Row

Sat 17 May 2025

https://leetcode.com/problems/find-largest-value-in-each-tree-row

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

    ans = []
    q = deque([root])

    while q:
      maxi = -math.inf
      for _ in range(len(q)):
        root = q …

Category: leetcode

Read More

516-Longest-Palindromic-Subsequence

Sat 17 May 2025

https://leetcode.com/problems/longest-palindromic-subsequence

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def longestPalindromeSubseq(self, s: str) -> int:
    # Dp(i, j) := LPS's length in s[i..j]
    @functools.lru_cache(None)
    def dp(i: int, j: int) -> int:
      if i > j …

Category: leetcode

Read More

517-Super-Washing-Machines

Sat 17 May 2025

https://leetcode.com/problems/super-washing-machines

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findMinMoves(self, machines: List[int]) -> int:
    dresses = sum(machines)

    if dresses % len(machines) != 0:
      return -1

    ans = 0
    average = dresses // len(machines)
    inout = 0

    for dress in machines …

Category: leetcode

Read More

518-Coin-Change-Ii

Sat 17 May 2025

https://leetcode.com/problems/coin-change-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def change(self, amount: int, coins: List[int]) -> int:
    dp = [1] + [0] * amount

    for coin in coins:
      for i in range(coin, amount + 1):
        dp[i] += dp[i - coin …

Category: leetcode

Read More

52-N-Queens-Ii

Sat 17 May 2025

https://leetcode.com/problems/n-queens-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def totalNQueens(self, n: int) -> int:
    ans = 0
    cols = [False] * n
    diag1 = [False] * (2 * n - 1)
    diag2 = [False] * (2 * n - 1)

    def dfs(i: int) -> None:
      nonlocal ans
      if …

Category: leetcode

Read More

520-Detect-Capital

Sat 17 May 2025

https://leetcode.com/problems/detect-capital

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def detectCapitalUse(self, word: str) -> bool:
    return word.isupper() or word.islower() or word.istitle()
new Solution().detectCapitalUse()

Score: 5

Category: leetcode

Read More

521-Longest-Uncommon-Subsequence-I

Sat 17 May 2025

https://leetcode.com/problems/longest-uncommon-subsequence-i

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findLUSlength(self, a: str, b: str) -> int:
    return -1 if a == b else max(len(a), len(b))
new Solution().findLUSlength()

Score: 5

Category: leetcode

Read More

522-Longest-Uncommon-Subsequence-Ii

Sat 17 May 2025

https://leetcode.com/problems/longest-uncommon-subsequence-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findLUSlength(self, strs: List[str]) -> int:
    def isSubsequence(a: str, b: str) -> bool:
      i = 0
      j = 0

      while i < len(a) and j < len(b):
        if a[i …

Category: leetcode

Read More

523-Continuous-Subarray-Sum

Sat 17 May 2025

https://leetcode.com/problems/continuous-subarray-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def checkSubarraySum(self, nums: List[int], k: int) -> bool:
    prefix = 0
    prefixToIndex = {0: -1}

    for i, num in enumerate(nums):
      prefix += num
      if k != 0:
        prefix %= k
      if prefix …

Category: leetcode

Read More

524-Longest-Word-In-Dictionary-Through-Deleting

Sat 17 May 2025

https://leetcode.com/problems/longest-word-in-dictionary-through-deleting

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

    for word in d:
      i = 0
      for c in s:
        if i < len(word) and c == word[i]:
          i …

Category: leetcode

Read More
Page 40 of 77

« Prev Next »