442-Find-All-Duplicates-In-An-Array

Sat 17 May 2025

https://leetcode.com/problems/find-all-duplicates-in-an-array

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

    for num in nums:
      nums[abs(num) - 1] *= -1
      if nums[abs(num) - 1] > 0:
        ans.append(abs(num))

    return …

Category: leetcode

Read More

443-String-Compression

Sat 17 May 2025

https://leetcode.com/problems/string-compression

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

    while i < len(chars):
      letter = chars[i]
      count = 0
      while i < len(chars) and chars[i] == letter:
        count …

Category: leetcode

Read More

444-Sequence-Reconstruction

Sat 17 May 2025

https://leetcode.com/problems/sequence-reconstruction

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def sequenceReconstruction(self, org: List[int], seqs: List[List[int]]) -> bool:
    if not seqs:
      return False

    n = len(org)
    graph = [[] for _ in range(n)]
    inDegree = [0] * n

    # Build …

Category: leetcode

Read More

445-Add-Two-Numbers-Ii

Sat 17 May 2025

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

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:
    stack1 = []
    stack2 = []

    while l1:
      stack1.append(l1)
      l1 = l1.next

    while l2:
      stack2.append(l2)
      l2 = l2.next

    head = None
    carry …

Category: leetcode

Read More

447-Number-Of-Boomerangs

Sat 17 May 2025

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

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

    for x1, y1 in points:
      count = defaultdict(int)
      for x2, y2 in points:
        ans += 2 * count[(x1 - x2)**2 …

Category: leetcode

Read More

448-Find-All-Numbers-Disappeared-In-An-Array

Sat 17 May 2025

https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
    for num in nums:
      index = abs(num) - 1
      nums[index] = -abs(nums[index])

    return [i + 1 for i, num in enumerate …

Category: leetcode

Read More

45-Jump-Game-Ii

Sat 17 May 2025

https://leetcode.com/problems/jump-game-ii

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

    # Implicit BFS
    for i in range(len(nums) - 1):
      farthest = max(farthest, i + nums[i])
      if …

Category: leetcode

Read More

450-Delete-Node-In-A-Bst

Sat 17 May 2025

https://leetcode.com/problems/delete-node-in-a-bst

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

Category: leetcode

Read More

451-Sort-Characters-By-Frequency

Sat 17 May 2025

https://leetcode.com/problems/sort-characters-by-frequency

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def frequencySort(self, s: str) -> str:
    ans = []
    bucket = [[] for _ in range(len(s) + 1)]

    for c, freq in Counter(s).items():
      bucket[freq].append(c)

    for freq in …

Category: leetcode

Read More

452-Minimum-Number-Of-Arrows-To-Burst-Balloons

Sat 17 May 2025

https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findMinArrowShots(self, points: List[List[int]]) -> int:
    ans = 0
    arrowX = -math.inf

    for point in sorted(points, key=lambda x: x[1]):
      if point[0] > arrowX:
        ans += 1 …

Category: leetcode

Read More
Page 35 of 77

« Prev Next »