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
974-Subarray-Sums-Divisible-By-K
https://leetcode.com/problems/subarray-sums-divisible-by-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
ans = 0
prefix = 0
count = [1] + [0] * (K - 1)
for a in A:
prefix = (prefix + a) % K
ans += count[prefix …
Read More
976-Largest-Perimeter-Triangle
https://leetcode.com/problems/largest-perimeter-triangle
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestPerimeter(self, A: List[int]) -> int:
A = sorted(A)
for i in range(len(A) - 1, 1, -1):
if A[i - 2] + A[i - 1] > A[i]:
return …
Read More
977-Squares-Of-A-Sorted-Array
https://leetcode.com/problems/squares-of-a-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortedSquares(self, A: List[int]) -> List[int]:
n = len(A)
l = 0
r = n - 1
ans = [0] * n
while n:
n -= 1
if abs(A[l]) > abs(A …
Read More
978-Longest-Turbulent-Subarray
https://leetcode.com/problems/longest-turbulent-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxTurbulenceSize(self, A: List[int]) -> int:
ans = 1
increasing = 1
decreasing = 1
for i in range(1, len(A)):
if A[i] > A[i - 1]:
increasing = decreasing + 1 …
Read More
98-Validate-Binary-Search-Tree
https://leetcode.com/problems/validate-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def isValidBST(root: Optional[TreeNode],
minNode: Optional[TreeNode], maxNode: Optional[TreeNode]) -> bool:
if not root:
return True
if minNode and root.val …
Read More
983-Minimum-Cost-For-Tickets
https://leetcode.com/problems/minimum-cost-for-tickets
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
ans = 0
last7 = deque()
last30 = deque()
for day in days:
while last7 and last7[0][0] + 7 <= day:
last7 …
Read More
985-Sum-Of-Even-Numbers-After-Queries
https://leetcode.com/problems/sum-of-even-numbers-after-queries
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:
ans = []
summ = sum(a for a in A if a % 2 == 0)
for q in queries:
if …
Read More