Last active
January 29, 2025 19:06
-
-
Save juanplopes/35e0e25c5fd8ff15f7e7c414eaea1bf9 to your computer and use it in GitHub Desktop.
algoclub 2/2/2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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