Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Last active January 29, 2025 19:06
Show Gist options
  • Save juanplopes/35e0e25c5fd8ff15f7e7c414eaea1bf9 to your computer and use it in GitHub Desktop.
Save juanplopes/35e0e25c5fd8ff15f7e7c414eaea1bf9 to your computer and use it in GitHub Desktop.
algoclub 2/2/2025
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
s = sum(nums)
s2 = 0
for i, x in enumerate(nums):
if s2 == s - s2 - x: return i
s2 += x
return -1
class NumArray:
def __init__(self, nums):
self.nums = [0] + nums
for i in range(1, len(self.nums)):
self.nums[i] += self.nums[i - 1]
def sumRange(self, left: int, right: int) -> int:
return self.nums[right+1] - self.nums[left]
class Solution:
def subarraySum(self, nums, k):
count = {0: 1}
answer = 0
s = 0
for x in nums:
s += x
answer += count.get(s - k, 0)
count[s] = count.get(s, 0) + 1
return answer
class Solution:
def subarraysDivByK(self, nums: List[int], k: int) -> int:
count = [1] + k * [0]
answer = 0
s = 0
for x in nums:
s += x
answer += count[(s - k) % k]
count[s % k] += 1
return answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment