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
970-Powerful-Integers
https://leetcode.com/problems/powerful-integers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def powerfulIntegers(self, x: int, y: int, bound: int) -> List[int]:
xs = {x**i for i in range(20) if x**i < bound}
ys = {y**i for i in …
Read More
972-Equal-Rational-Numbers
https://leetcode.com/problems/equal-rational-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isRationalEqual(self, S: str, T: str) -> bool:
def valueOf(s: str) -> float:
if s.find('(') == -1:
return float(s)
integer_nonRepeating = float(s[:s.find('(')])
nonRepeatingLength = s.find('(') - s …
Read More
973-K-Closest-Points-To-Origin
https://leetcode.com/problems/k-closest-points-to-origin
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
maxHeap = []
for x, y in points:
heapq.heappush(maxHeap, (- x * x - y * y, [x, y]))
if len …
Read More