Skip to content

Commit f5d00d1

Browse files
authored
Create count-of-interesting-subarrays.py
1 parent 75d19c6 commit f5d00d1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(m)
3+
4+
import collections
5+
6+
7+
# freq table, prefix sum
8+
class Solution(object):
9+
def countInterestingSubarrays(self, nums, modulo, k):
10+
"""
11+
:type nums: List[int]
12+
:type modulo: int
13+
:type k: int
14+
:rtype: int
15+
"""
16+
cnt = collections.Counter([0])
17+
result = prefix = 0
18+
for x in nums:
19+
if x%modulo == k:
20+
prefix = (prefix+1)%modulo
21+
result += cnt[(prefix-k)%modulo]
22+
cnt[prefix] += 1
23+
return result

0 commit comments

Comments
 (0)