851-Loud-And-Rich
https://leetcode.com/problems/loud-and-rich
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
852-Peak-Index-In-A-Mountain-Array
https://leetcode.com/problems/peak-index-in-a-mountain-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
853-Car-Fleet
https://leetcode.com/problems/car-fleet
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
854-K-Similar-Strings
https://leetcode.com/problems/k-similar-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
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 …
Read More
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