-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
61 lines (48 loc) · 1.43 KB
/
Solution.java
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
import java.util.HashMap;
import java.util.Map;
public class Solution {
// Brute Force approach
// This solution implements a brute force approach to
// problem by iterating through the whole array then
// adding all the current sum with every element after
// that index
// Time : O(n^2)
// Space : O(1)
public int subarraySum(int[] nums, int k) {
int n = nums.length;
int out = 0;
for (int i = 0; i < n; i++) {
int sum = nums[i];
if (sum == k) {
out++;
}
for (int j = i + 1; j < n; j++) {
sum += nums[j];
if (sum == k) {
out++;
}
}
}
return out;
}
// Prefix Sum approach
// This solution implements a prefix sum approach to
// problem by iterating through the whole array then
// adding all the current sum with every element after
// that index
// Time : O(n)
// Space : O(n)
public int subarraySumPresum(int[] nums, int k) {
int sum = 0, res = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (map.containsKey(sum - k)) {
res += map.get(sum - k);
}
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return res;
}
}