267-Palindrome-Permutation-Ii

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def generatePalindromes(self, s: str) -> List[str]:
    # Get character occurrence
    count = Counter(s)

    # Count odd one
    odd = sum(value & 1 for value in count.values())

    # can't form any …

Category: leetcode

Read More

268-Missing-Number

Sat 17 May 2025

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

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

    for i, num in enumerate(nums):
      ans ^= i ^ num

    return ans
new Solution().missingNumber()

Score: 5

Category: leetcode

Read More

269-Alien-Dictionary

Sat 17 May 2025

https://leetcode.com/problems/alien-dictionary

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def alienOrder(self, words: List[str]) -> str:
    graph = {}
    inDegree = [0] * 26

    self._buildGraph(graph, words, inDegree)
    return self._topology(graph, inDegree)

  def _buildGraph(self, graph: Dict[chr, Set[chr …

Category: leetcode

Read More

27-Remove-Element

Sat 17 May 2025

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

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

    for num in nums:
      if num != val:
        nums[i] = num
        i += 1

    return i
new Solution().removeElement()

Score …

Category: leetcode

Read More

270-Closest-Binary-Search-Tree-Value

Sat 17 May 2025

https://leetcode.com/problems/closest-binary-search-tree-value

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def closestValue(self, root: Optional[TreeNode], target: float) -> int:
    # If target < root.val, search left subtree
    if target < root.val and root.left:
      left = self.closestValue(root.left, target …

Category: leetcode

Read More

272-Closest-Binary-Search-Tree-Value-Ii

Sat 17 May 2025

https://leetcode.com/problems/closest-binary-search-tree-value-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def closestKValues(self, root: Optional[TreeNode], target: float, k: int) -> List[int]:
    q = deque()

    def inorder(root: Optional[TreeNode]) -> None:
      if not root:
        return

      inorder(root.left)
      q.append …

Category: leetcode

Read More

273-Integer-To-English-Words

Sat 17 May 2025

https://leetcode.com/problems/integer-to-english-words

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numberToWords(self, num: int) -> str:
    if num == 0:
      return "Zero"

    belowTwenty = ["",        "One",       "Two",      "Three",
                   "Four",    "Five",      "Six",      "Seven",
                   "Eight",   "Nine",      "Ten",      "Eleven",
                   "Twelve",  "Thirteen",  "Fourteen", "Fifteen",
                   "Sixteen", "Seventeen …

Category: leetcode

Read More

274-H-Index

Sat 17 May 2025

https://leetcode.com/problems/h-index

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def hIndex(self, citations: List[int]) -> int:
    n = len(citations)
    accumulate = 0
    count = [0] * (n + 1)

    for citation in citations:
      count[min(citation, n)] += 1

    # To find the largeset …

Category: leetcode

Read More

275-H-Index-Ii

Sat 17 May 2025

https://leetcode.com/problems/h-index-ii

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

    while l < r:
      m = (l + r) // 2
      if citations[m] >= len(citations) - m:
        r = m
      else:
        l …

Category: leetcode

Read More

276-Paint-Fence

Sat 17 May 2025

https://leetcode.com/problems/paint-fence

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

    # dp[i] := # Of ways to …

Category: leetcode

Read More
Page 22 of 77

« Prev Next »