Skip to content

Commit a896ce4

Browse files
committed
Sync LeetCode submission Runtime - 824 ms (60.33%), Memory - 36.1 MB (71.92%)
1 parent ba5b9d3 commit a896ce4

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p>
2+
3+
<p>A <strong>good subarray</strong> is a subarray where:</p>
4+
5+
<ul>
6+
<li>its length is <strong>at least two</strong>, and</li>
7+
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li>
8+
</ul>
9+
10+
<p><strong>Note</strong> that:</p>
11+
12+
<ul>
13+
<li>A <strong>subarray</strong> is a contiguous part of the array.</li>
14+
<li>An integer <code>x</code> is a multiple of <code>k</code> if there exists an integer <code>n</code> such that <code>x = n * k</code>. <code>0</code> is <strong>always</strong> a multiple of <code>k</code>.</li>
15+
</ul>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6
22+
<strong>Output:</strong> true
23+
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6
30+
<strong>Output:</strong> true
31+
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42.
32+
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13
39+
<strong>Output:</strong> false
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
47+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
48+
<li><code>0 &lt;= sum(nums[i]) &lt;= 2<sup>31</sup> - 1</code></li>
49+
<li><code>1 &lt;= k &lt;= 2<sup>31</sup> - 1</code></li>
50+
</ul>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Approach 1: Prefix Sum and Hashing
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
8+
prefix_mod = 0
9+
mod_seen = {0: -1}
10+
11+
for i in range(len(nums)):
12+
prefix_mod = (prefix_mod + nums[i]) % k
13+
14+
if prefix_mod in mod_seen:
15+
# ensures that the size of subarray is at least 2
16+
if i - mod_seen[prefix_mod] > 1:
17+
return True
18+
else:
19+
# mark the value of prefix_mod with the current index.
20+
mod_seen[prefix_mod] = i
21+
22+
return False

0 commit comments

Comments
 (0)