-
Notifications
You must be signed in to change notification settings - Fork 22
/
Maximum Size Subarray Sum Equals k.java
executable file
·63 lines (55 loc) · 1.82 KB
/
Maximum Size Subarray Sum Equals k.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
62
63
M
1531896141
tags: Hash Table, PreSum, Subarray
time: O(n)
space: O(n)
#### Map<preSumValue, index>
- use `Map<preSum value, index>` to store inline preSum and its index.
- 1. Build presum incline
- 2. Use map to cache current preSum value and its index: `Map<preSum value, index>`
- 3. Each iteration: calculate possible preSum candidate that prior target sequence. ex: `(preSum - k)`
- 4. Use the calculated preSum candidate to find index
- 5. Use found index to calculate for result. ex: calculate range.
```
/*
Given an array nums and a target value k, find the maximum length of a subarray that sums to k.
If there isn't one, return 0 instead.
Note:
The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range.
Example 1:
Input: nums = [1, -1, 5, -2, 3], k = 3
Output: 4
Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.
Example 2:
Input: nums = [-2, -1, 2, 1], k = 1
Output: 2
Explanation: The subarray [-1, 2] sums to 1 and is the longest.
Follow Up:
Can you do it in O(n) time?
*/
/*
Same concept as 2 sum
1. calcualte preSum as we go
2. store presum and the index in map<preSum, index>
3. if @ any index: map.containsKey(presum - k), then the starting index was available: [startIndex, i]
4. maintain global variable
*/
class Solution {
public int maxSubArrayLen(int[] nums, int k) {
if (nums == null || nums.length == 0) return 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int preSum = 0, max = 0;
for (int i = 0; i < nums.length; i++) {
preSum += nums[i];
if (map.containsKey(preSum - k)) {
max = Math.max(max, i - map.get(preSum - k));
}
if (!map.containsKey(preSum)) {
map.put(preSum, i);
}
}
return max;
}
}
```