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
986-Interval-List-Intersections
https://leetcode.com/problems/interval-list-intersections
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
ans = []
i = 0
j = 0
while i < len(firstList) and j < len(secondList):
# Lo := the …
Read More
987-Vertical-Order-Traversal-Of-A-Binary-Tree
https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def verticalTraversal(self, root: Optional[TreeNode]) -> List[List[int]]:
ans = []
xToNodes = defaultdict(list)
def dfs(node: Optional[TreeNode], x: int, y: int) -> None:
if not node:
return
xToNodes[x …
Read More
989-Add-To-Array-Form-Of-Integer
https://leetcode.com/problems/add-to-array-form-of-integer
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
for i in reversed(range(len(num))):
k, num[i] = divmod(num[i] + k, 10)
while k > 0:
num …
Read More