277-Find-The-Celebrity

Sat 17 May 2025

https://leetcode.com/problems/find-the-celebrity

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
# The knows API is already defined for you.
# Returns a bool, whether a knows b
# Def knows(a: int, b: int) -> bool:


class Solution:
  def findCelebrity(self, n: int) -> int:
    candidate …

Category: leetcode

Read More

278-First-Bad-Version

Sat 17 May 2025

https://leetcode.com/problems/first-bad-version

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def firstBadVersion(self, n: int) -> int:
    l = 1
    r = n

    while l < r:
      m = (l + r) >> 1
      if isBadVersion(m):
        r = m
      else:
        l = m + 1

    return l
new …

Category: leetcode

Read More

279-Perfect-Squares

Sat 17 May 2025

https://leetcode.com/problems/perfect-squares

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

    dp[0] = 0
    dp[1] = 1

    for i in range(2, n + 1):
      j = 1
      while j * j <= i …

Category: leetcode

Read More

28-Find-The-Index-Of-The-First-Occurrence-In-A-String

Sat 17 May 2025

https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def strStr(self, haystack: str, needle: str) -> int:
    m = len(haystack)
    n = len(needle)

    for i in range(m - n + 1):
      if haystack[i:i + n] == needle:
        return i …

Category: leetcode

Read More

280-Wiggle-Sort

Sat 17 May 2025

https://leetcode.com/problems/wiggle-sort

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def wiggleSort(self, nums: List[int]) -> None:
    # 1. if i is even, then nums[i] <= nums[i - 1]
    # 2. if i is odd, then nums[i] >= nums[i - 1 …

Category: leetcode

Read More

282-Expression-Add-Operators

Sat 17 May 2025

https://leetcode.com/problems/expression-add-operators

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

    # Start index, prev value, current evaluated value
    def dfs(start: int, prev: int, eval: int, path: List[str]) -> None …

Category: leetcode

Read More

283-Move-Zeroes

Sat 17 May 2025

https://leetcode.com/problems/move-zeroes

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def moveZeroes(self, nums: List[int]) -> None:
    j = 0
    for num in nums:
      if num != 0:
        nums[j] = num
        j += 1

    for i in range(j, len(nums)):
      nums …

Category: leetcode

Read More

285-Inorder-Successor-In-Bst

Sat 17 May 2025

https://leetcode.com/problems/inorder-successor-in-bst

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def inorderSuccessor(self, root: Optional[TreeNode], p: Optional[TreeNode]) -> Optional[TreeNode]:
    if not root:
      return None
    if root.val <= p.val:
      return self.inorderSuccessor(root.right, p)
    return self …

Category: leetcode

Read More

286-Walls-And-Gates

Sat 17 May 2025

https://leetcode.com/problems/walls-and-gates

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def wallsAndGates(self, rooms: List[List[int]]) -> None:
    kInf = 2**31 - 1
    m = len(rooms)
    n = len(rooms[0])
    dirs = [0, 1, 0, -1, 0]
    q = deque()

    for i …

Category: leetcode

Read More

287-Find-The-Duplicate-Number

Sat 17 May 2025

https://leetcode.com/problems/find-the-duplicate-number

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

    while slow != fast:
      slow = nums[slow]
      fast = nums[nums[fast]]

    slow = nums[0 …

Category: leetcode

Read More
Page 23 of 77

« Prev Next »