Skip to content

Commit 786a931

Browse files
committed
Sync LeetCode submission Runtime - 198 ms (20.69%), Memory - 38.8 MB (71.84%)
1 parent e8b0fb9 commit 786a931

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, an integer <code>modulo</code>, and an integer <code>k</code>.</p>
2+
3+
<p>Your task is to find the count of subarrays that are <strong>interesting</strong>.</p>
4+
5+
<p>A <strong>subarray</strong> <code>nums[l..r]</code> is <strong>interesting</strong> if the following condition holds:</p>
6+
7+
<ul>
8+
<li>Let <code>cnt</code> be the number of indices <code>i</code> in the range <code>[l, r]</code> such that <code>nums[i] % modulo == k</code>. Then, <code>cnt % modulo == k</code>.</li>
9+
</ul>
10+
11+
<p>Return <em>an integer denoting the count of interesting subarrays. </em></p>
12+
13+
<p><span><strong>Note:</strong> A subarray is <em>a contiguous non-empty sequence of elements within an array</em>.</span></p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [3,2,4], modulo = 2, k = 1
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> In this example the interesting subarrays are:
22+
The subarray nums[0..0] which is [3].
23+
- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
24+
- Hence, cnt = 1 and cnt % modulo == k.
25+
The subarray nums[0..1] which is [3,2].
26+
- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
27+
- Hence, cnt = 1 and cnt % modulo == k.
28+
The subarray nums[0..2] which is [3,2,4].
29+
- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
30+
- Hence, cnt = 1 and cnt % modulo == k.
31+
It can be shown that there are no other interesting subarrays. So, the answer is 3.</pre>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums = [3,1,9,6], modulo = 3, k = 0
37+
<strong>Output:</strong> 2
38+
<strong>Explanation: </strong>In this example the interesting subarrays are:
39+
The subarray nums[0..3] which is [3,1,9,6].
40+
- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
41+
- Hence, cnt = 3 and cnt % modulo == k.
42+
The subarray nums[1..1] which is [1].
43+
- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
44+
- Hence, cnt = 0 and cnt % modulo == k.
45+
It can be shown that there are no other interesting subarrays. So, the answer is 2.</pre>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5 </sup></code></li>
52+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
53+
<li><code>1 &lt;= modulo &lt;= 10<sup>9</sup></code></li>
54+
<li><code>0 &lt;= k &lt; modulo</code></li>
55+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Approach: Prefix Sum
2+
3+
# Time: O(n)
4+
# Space: O(min(n, modulo))
5+
6+
from collections import Counter
7+
8+
class Solution:
9+
def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
10+
n = len(nums)
11+
count = Counter([0])
12+
res = 0
13+
prefix = 0
14+
15+
for i in range(n):
16+
prefix += 1 if nums[i] % modulo == k else 0
17+
res += count[(prefix - k + modulo) % modulo]
18+
count[prefix % modulo] += 1
19+
20+
return res
21+

0 commit comments

Comments
 (0)