954-Array-Of-Doubled-Pairs
https://leetcode.com/problems/array-of-doubled-pairs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = Counter(A)
for key in sorted(count, key=abs):
if count[key] > count[2 * key]:
return False
count[2 * key] -= count …
Read More
957-Prison-Cells-After-N-Days
https://leetcode.com/problems/prison-cells-after-n-days
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def prisonAfterNDays(self, cells: List[int], N: int) -> List[int]:
nextDayCells = [0] * len(cells)
day = 0
while N > 0:
N -= 1
for i in range(1, len(cells) - 1 …
Read More
96-Unique-Binary-Search-Trees
https://leetcode.com/problems/unique-binary-search-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numTrees(self, n: int) -> int:
# G[i] := # Of unique BST's that store values 1..i
G = [1, 1] + [0] * (n - 1)
for i in range(2, n …
Read More
961-N-Repeated-Element-In-Size-2N-Array
https://leetcode.com/problems/n-repeated-element-in-size-2n-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def repeatedNTimes(self, A: List[int]) -> int:
for i in range(len(A) - 2):
if A[i] == A[i + 1] or A[i] == A[i + 2]:
return A[i …
Read More
962-Maximum-Width-Ramp
https://leetcode.com/problems/maximum-width-ramp
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxWidthRamp(self, nums: List[int]) -> int:
ans = 0
stack = []
for i, num in enumerate(nums):
if stack == [] or num <= nums[stack[-1]]:
stack.append(i)
for i, num …
Read More
963-Minimum-Area-Rectangle-Ii
https://leetcode.com/problems/minimum-area-rectangle-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minAreaFreeRect(self, points: List[List[int]]) -> float:
ans = math.inf
# For each A, B pair points, {hash(A, B): (ax, ay, bx, by)}
centerToPoints = defaultdict(list)
for ax …
Read More
964-Least-Operators-To-Express-Number
https://leetcode.com/problems/least-operators-to-express-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def leastOpsExpressTarget(self, x: int, target: int) -> int:
@functools.lru_cache(None)
def dfs(target):
if x > target:
return min(2 * target - 1, 2 * (x - target))
if x == target:
return …
Read More
966-Vowel-Spellchecker
https://leetcode.com/problems/vowel-spellchecker
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def spellchecker(self, wordlist: List[str], queries: List[str]) -> List[str]:
def lowerKey(word: str) -> str:
return '$' + ''.join([c.lower() for c in word])
def vowelKey(word: str) -> str …
Read More
969-Pancake-Sorting
https://leetcode.com/problems/pancake-sorting
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pancakeSort(self, A: List[int]) -> List[int]:
ans = []
for target in range(len(A), 0, -1):
index = A.index(target)
A[:index + 1] = A[:index + 1][::-1]
A …
Read More
97-Interleaving-String
https://leetcode.com/problems/interleaving-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
m = len(s1)
n = len(s2)
if m + n != len(s3):
return False
# dp[i][j] := true if s3 …
Read More