Given an integer array
nums
and an integer k
, return the maximum length of a subarray that sums to k
. If there is not one, return 0
instead.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.
Constraints:
`1 <= nums.length <= 2 105
*
-104 <= nums[i] <= 104*
-109 <= k <= 109`题目大意:
最长子数组和等于k,求长度
解题思路:
一开始以为类似于LeetCode 209 Minimum Size Subarray Sum用同向双指针。但这题不是连续,因为此题含负数,不会连续大于等于target。考虑用presum的two sum法。
解题步骤:
N/A
注意事项:
- 加入presum[0] = 0,因为这样才可以得到以首元素开始的子数组和
- 若presum已经在hashmap中了,不要加入,因为要保证最长数组,如 [-1, 1], target = 0, index可以为0, 2
- max_len答案只要初始化为0,不用最小值,因为最大长度必为非负
Python代码:
1 | # presum[i] - presum[j] = k |
算法分析:
时间复杂度为O(n)
,空间复杂度O(n)