-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarraySum.py
More file actions
24 lines (17 loc) · 874 Bytes
/
subarraySum.py
File metadata and controls
24 lines (17 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from collections import defaultdict
class Solution:
def subarraySum(self, nums, k):
# Dictionary to store counts of each running total we’ve seen
running_total_count = defaultdict(int)
running_total = 0 # The cumulative sum as we go through the list
count = 0 # Total number of subarrays that sum up to k
# Initialize with 0 to handle cases where subarrays start from index 0
running_total_count[0] = 1
# Loop through each number in nums
for num in nums:
running_total += num # Update the cumulative running total
# Check if (running_total - k) exists in the dictionary
count += running_total_count[running_total - k]
# Update the dictionary with the current running total
running_total_count[running_total] += 1
return count