191-Number-Of-1-Bits

Sat 17 May 2025

https://leetcode.com/problems/number-of-1-bits

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

    for i in range(32):
      if (n >> i) & 1:
        ans += 1

    return ans
new Solution().hammingWeight()

Score: 5

Category: leetcode

Read More

198-House-Robber

Sat 17 May 2025

https://leetcode.com/problems/house-robber

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

    # dp[i]: = max money of robbing nums[0..i]
    dp …

Category: leetcode

Read More

199-Binary-Tree-Right-Side-View

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-right-side-view

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

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

    while q:
      size = len(q)
      for i in range(size):
        root = q.popleft …

Category: leetcode

Read More

2-Add-Two-Numbers

Sat 17 May 2025

https://leetcode.com/problems/add-two-numbers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
    dummy = ListNode(0)
    curr = dummy
    carry = 0

    while carry or l1 or l2:
      if l1:
        carry += l1.val
        l1 = l1.next …

Category: leetcode

Read More

20-Valid-Parentheses

Sat 17 May 2025

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

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

    for c in s:
      if c == '(':
        stack.append(')')
      elif c == '{':
        stack.append('}')
      elif c == '[':
        stack.append(']')
      elif not stack or stack.pop …

Category: leetcode

Read More

200-Number-Of-Islands

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def numIslands(self, grid: List[List[str]]) -> int:
    m = len(grid)
    n = len(grid[0])
    dirs = [0, 1, 0, -1, 0]

    def bfs(r, c):
      q = deque([(r, c …

Category: leetcode

Read More

201-Bitwise-And-Of-Numbers-Range

Sat 17 May 2025

https://leetcode.com/problems/bitwise-and-of-numbers-range

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

Score: 5

Category: leetcode

Read More

202-Happy-Number

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isHappy(self, n: int) -> bool:
    def squaredSum(n: int) -> bool:
      summ = 0
      while n:
        summ += pow(n % 10, 2)
        n //= 10
      return summ

    slow = squaredSum(n)
    fast = squaredSum …

Category: leetcode

Read More

203-Remove-Linked-List-Elements

Sat 17 May 2025

https://leetcode.com/problems/remove-linked-list-elements

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def removeElements(self, head: ListNode, val: int) -> ListNode:
    dummy = ListNode(0, head)
    prev = dummy

    while head:
      if head.val != val:
        prev.next = head
        prev = prev.next
      head = head.next …

Category: leetcode

Read More

204-Count-Primes

Sat 17 May 2025

https://leetcode.com/problems/count-primes

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

    isPrime = [False] * 2 + [True] * (n - 2)

    for i in range(2, int(n**0.5) + 1):
      if isPrime …

Category: leetcode

Read More
Page 15 of 77

« Prev Next »