Skip to content

Commit

Permalink
Create count-of-interesting-subarrays.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 11, 2023
1 parent 75d19c6 commit f5d00d1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Python/count-of-interesting-subarrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Time: O(n)
# Space: O(m)

import collections


# freq table, prefix sum
class Solution(object):
def countInterestingSubarrays(self, nums, modulo, k):
"""
:type nums: List[int]
:type modulo: int
:type k: int
:rtype: int
"""
cnt = collections.Counter([0])
result = prefix = 0
for x in nums:
if x%modulo == k:
prefix = (prefix+1)%modulo
result += cnt[(prefix-k)%modulo]
cnt[prefix] += 1
return result

0 comments on commit f5d00d1

Please sign in to comment.