Skip to content

Commit fe92329

Browse files
authored
Create subarray-sums-divisible-by-k.py
1 parent ab6d2bf commit fe92329

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n)
2+
# Space: O(k)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def subarraysDivByK(self, A, K):
9+
"""
10+
:type A: List[int]
11+
:type K: int
12+
:rtype: int
13+
"""
14+
count = collections.defaultdict(int)
15+
count[0] = 1
16+
result, prefix = 0, 0
17+
for a in A:
18+
prefix = (prefix+a) % K
19+
result += count[prefix]
20+
count[prefix] += 1
21+
return result

0 commit comments

Comments
 (0)