-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 824 ms (60.33%), Memory - 36.1 MB …
…(71.92%)
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<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> | ||
|
||
<p>A <strong>good subarray</strong> is a subarray where:</p> | ||
|
||
<ul> | ||
<li>its length is <strong>at least two</strong>, and</li> | ||
<li>the sum of the elements of the subarray is a multiple of <code>k</code>.</li> | ||
</ul> | ||
|
||
<p><strong>Note</strong> that:</p> | ||
|
||
<ul> | ||
<li>A <strong>subarray</strong> is a contiguous part of the array.</li> | ||
<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> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [23,<u>2,4</u>,6,7], k = 6 | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> [2, 4] is a continuous subarray of size 2 whose elements sum up to 6. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [<u>23,2,6,4,7</u>], k = 6 | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> [23, 2, 6, 4, 7] is an continuous subarray of size 5 whose elements sum up to 42. | ||
42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [23,2,6,4,7], k = 13 | ||
<strong>Output:</strong> false | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li> | ||
<li><code>0 <= nums[i] <= 10<sup>9</sup></code></li> | ||
<li><code>0 <= sum(nums[i]) <= 2<sup>31</sup> - 1</code></li> | ||
<li><code>1 <= k <= 2<sup>31</sup> - 1</code></li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Approach 1: Prefix Sum and Hashing | ||
|
||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
class Solution: | ||
def checkSubarraySum(self, nums: List[int], k: int) -> bool: | ||
prefix_mod = 0 | ||
mod_seen = {0: -1} | ||
|
||
for i in range(len(nums)): | ||
prefix_mod = (prefix_mod + nums[i]) % k | ||
|
||
if prefix_mod in mod_seen: | ||
# ensures that the size of subarray is at least 2 | ||
if i - mod_seen[prefix_mod] > 1: | ||
return True | ||
else: | ||
# mark the value of prefix_mod with the current index. | ||
mod_seen[prefix_mod] = i | ||
|
||
return False |