856-Score-Of-Parentheses

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

Fri 14 November 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

862-Shortest-Subarray-With-Sum-At-Least-K

Fri 14 November 2025

https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def shortestSubarray(self, A: List[int], K: int) -> int:
    n = len(A)
    ans = n + 1
    q = deque()
    prefix = [0] + list(itertools.accumulate(A))

    for i in range(n + 1 …

Category: leetcode

Read More

866-Prime-Palindrome

Fri 14 November 2025

https://leetcode.com/problems/prime-palindrome

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def primePalindrome(self, N: int) -> int:
    def getPalindromes(n: int) -> int:
      length = n // 2
      for i in range(10**(length - 1), 10**length):
        s = str(i)
        for j in …

Category: leetcode

Read More

867-Transpose-Matrix

Fri 14 November 2025

https://leetcode.com/problems/transpose-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def transpose(self, A: List[List[int]]) -> List[List[int]]:
    ans = [[0] * len(A) for _ in range(len(A[0]))]

    for i in range(len(A)):
      for j …

Category: leetcode

Read More

868-Binary-Gap

Fri 14 November 2025

https://leetcode.com/problems/binary-gap

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def binaryGap(self, n: int) -> int:
    ans = 0
    d = -32  # Distance between any two 1's, initialized to a reasonable small value

    while n:
      if n & 1:
        ans = max …

Category: leetcode

Read More
Page 72 of 146

« Prev Next »