-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathsubaray_sum_equals_k.go
88 lines (60 loc) · 3.64 KB
/
subaray_sum_equals_k.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,1,1], k = 2
Output: 2
Example 2:
Input: nums = [1,2,3], k = 3
Output: 2
### Explanation:
The code is designed to count the number of subarrays within the 'nums' array whose elements sum to a given target integer 'k'.
It uses a hashmap to efficiently keep track of cumulative sums and their counts.
Here's the code's key logic:
1. It initializes a hashmap `sumIndex` to store cumulative sums as keys and their counts as values.
2. It initializes variables `result` and `currentSum`.
3. It adds a key-value pair of `(0, 1)` to the `sumIndex` hashmap to represent the sum of an empty subarray (0) and its count (1).
4. It iterates through the elements of the 'nums' array.
5. For each element, it adds the element's value to `currentSum`.
6. It calculates the value to find in the hashmap by subtracting 'k' from the current cumulative sum, which is stored in the `toFind` variable.
7. It checks if the hashmap contains the value 'toFind' and, if found, adds the count of subarrays that sum to 'toFind' to the 'result'.
8. It updates the hashmap with the current cumulative sum. If the sum is already in the hashmap, it increments its count by 1. If it's not in the hashmap,
it adds it with a count of 1.
9. Finally, it returns the 'result,' which represents the total number of subarrays with a sum of 'k'.
### Time Complexity:
The time complexity of this code is O(n), where 'n' is the length of the 'nums' array. This is because the code iterates through the 'nums'
array once, and each iteration consists of constant-time operations (e.g., hashmap lookups and additions).
### Space Complexity:
The space complexity of this code is O(n), where 'n' is the length of the 'nums' array. The space is primarily used for the hashmap `sumIndex`,
which can have up to 'n' distinct cumulative sums. In the worst case, all elements are unique, resulting in 'n' distinct cumulative sums,
each with a count of 1.
In summary, this code efficiently counts subarrays with a sum of 'k' in O(n) time and uses O(n) space to store cumulative sums and their counts.
*/
func subarraySum(nums []int, k int) int {
// Create a map to store cumulative sums as keys and their counts as values.
sumIndex := make(map[int]int)
// Initialize the result to 0.
result := 0
// Initialize a variable to track the current cumulative sum.
currentSum := 0
// Initialize the map with a key-value pair representing the sum of an empty subarray (0) and its count (1).
sumIndex[0] = 1
// Iterate through the elements of the 'nums' slice.
for _, num := range nums {
// Add the current element to the cumulative sum.
currentSum += num
// Calculate the value to find in the map by subtracting 'k' from the current cumulative sum.
toFind := currentSum - k
// Check if the map contains the value 'toFind'.
if count, ok := sumIndex[toFind]; ok {
// If found, add the count of subarrays that sum to 'toFind' to the 'result'.
result += count
}
// Update the map with the current cumulative sum.
// If it's already in the map, increment its count by 1.
// If it's not in the map, add it with a count of 1.
sumIndex[currentSum]++
}
// Return the final result, which represents the total number of subarrays with a sum of 'k'.
return result
}