856-Score-Of-Parentheses
https://leetcode.com/problems/score-of-parentheses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
857-Minimum-Cost-To-Hire-K-Workers
https://leetcode.com/problems/minimum-cost-to-hire-k-workers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
858-Mirror-Reflection
https://leetcode.com/problems/mirror-reflection
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
859-Buddy-Strings
https://leetcode.com/problems/buddy-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
86-Partition-List
https://leetcode.com/problems/partition-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
861-Score-After-Flipping-Matrix
https://leetcode.com/problems/score-after-flipping-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
862-Shortest-Subarray-With-Sum-At-Least-K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
866-Prime-Palindrome
https://leetcode.com/problems/prime-palindrome
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
867-Transpose-Matrix
https://leetcode.com/problems/transpose-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
868-Binary-Gap
https://leetcode.com/problems/binary-gap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More