974-Subarray-Sums-Divisible-By-K

Fri 14 November 2025

https://leetcode.com/problems/subarray-sums-divisible-by-k

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

976-Largest-Perimeter-Triangle

Fri 14 November 2025

https://leetcode.com/problems/largest-perimeter-triangle

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

977-Squares-Of-A-Sorted-Array

Fri 14 November 2025

https://leetcode.com/problems/squares-of-a-sorted-array

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

978-Longest-Turbulent-Subarray

Fri 14 November 2025

https://leetcode.com/problems/longest-turbulent-subarray

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

98-Validate-Binary-Search-Tree

Fri 14 November 2025

https://leetcode.com/problems/validate-binary-search-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

983-Minimum-Cost-For-Tickets

Fri 14 November 2025

https://leetcode.com/problems/minimum-cost-for-tickets

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

985-Sum-Of-Even-Numbers-After-Queries

Fri 14 November 2025

https://leetcode.com/problems/sum-of-even-numbers-after-queries

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

986-Interval-List-Intersections

Fri 14 November 2025

https://leetcode.com/problems/interval-list-intersections

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

987-Vertical-Order-Traversal-Of-A-Binary-Tree

Fri 14 November 2025

https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

989-Add-To-Array-Form-Of-Integer

Fri 14 November 2025

https://leetcode.com/problems/add-to-array-form-of-integer

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 82 of 146

« Prev Next »