851-Loud-And-Rich

Sat 17 May 2025

https://leetcode.com/problems/loud-and-rich

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def loudAndRich(self, richer: List[List[int]], quiet: List[int]) -> List[int]:
    graph = [[] for _ in range(len(quiet))]

    for u, v in richer:
      graph[v].append(u)

    @functools …

Category: leetcode

Read More

852-Peak-Index-In-A-Mountain-Array

Sat 17 May 2025

https://leetcode.com/problems/peak-index-in-a-mountain-array

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

    while l < r:
      m = (l + r) // 2
      if arr[m] < arr[m + 1]:
        l = m + 1 …

Category: leetcode

Read More

853-Car-Fleet

Sat 17 May 2025

https://leetcode.com/problems/car-fleet

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:
    ans = 0
    times = [
        float(target - p) / s for p, s in sorted(zip(position, speed),
                                                 reverse=True …

Category: leetcode

Read More

854-K-Similar-Strings

Sat 17 May 2025

https://leetcode.com/problems/k-similar-strings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def kSimilarity(self, s1: str, s2: str) -> int:
    ans = 0
    q = deque([s1])
    seen = {s1}

    while q:
      for _ in range(len(q)):
        curr = q.popleft()
        if curr == s2 …

Category: leetcode

Read More

856-Score-Of-Parentheses

Sat 17 May 2025

https://leetcode.com/problems/score-of-parentheses

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

    for a, b in zip(S, S[1:]):
      if a + b == '()':
        ans += 1 << layer
      layer += 1 if a == '(' else …

Category: leetcode

Read More

857-Minimum-Cost-To-Hire-K-Workers

Sat 17 May 2025

https://leetcode.com/problems/minimum-cost-to-hire-k-workers

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def mincostToHireWorkers(self, quality: List[int], wage: List[int], k: int) -> float:
    ans = math.inf
    qualitySum = 0
    # (wagePerQuality, quality) sorted by wagePerQuality
    workers = sorted((w / q, q) for q …

Category: leetcode

Read More

858-Mirror-Reflection

Sat 17 May 2025

https://leetcode.com/problems/mirror-reflection

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def mirrorReflection(self, p: int, q: int) -> int:
    while p % 2 == 0 and q % 2 == 0:
      p //= 2
      q //= 2

    if p % 2 == 0:
      return 2
    if q % 2 …

Category: leetcode

Read More

859-Buddy-Strings

Sat 17 May 2025

https://leetcode.com/problems/buddy-strings

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def buddyStrings(self, A: str, B: str) -> bool:
    if len(A) != len(B):
      return False
    if A == B and len(set(A)) < len(A):
      return True

    diff = [(a, b …

Category: leetcode

Read More

86-Partition-List

Sat 17 May 2025

https://leetcode.com/problems/partition-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def partition(self, head: ListNode, x: int) -> ListNode:
    beforeHead = ListNode(0)
    afterHead = ListNode(0)
    before = beforeHead
    after = afterHead

    while head:
      if head.val < x:
        before.next = head
        before = head …

Category: leetcode

Read More

861-Score-After-Flipping-Matrix

Sat 17 May 2025

https://leetcode.com/problems/score-after-flipping-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def matrixScore(self, grid: List[List[int]]) -> int:
    # Flip rows with leading 0
    for row in grid:
      if row[0] == 0:
        self._flip(row)

    # Flip cols with 1s …

Category: leetcode

Read More
Page 65 of 77

« Prev Next »