325. Maximum Size Subarray Sum Equals k
Description
Given an integer array nums
and an integer k
, return the maximum length of a subarray that sums to k
. If there isn't one, return 0
instead.
Example 1:
Example 2:
Constraints:
1 <= nums.length <= 2 * 105
-104 <= nums[i] <= 104
-109 <= k <= 109
Tags
Hash Table
Solution
This is a prefix-sum problem, combining LeetCode 560. Subarray Sum Equals K and 525. Contiguous Array. Note that we only record the map from prefix-sum to its index when such prefix-sum does not exist in the map, since we need the longest subarray.
Complexity
Time complexity:
Space complexity:
Code
Last updated
Was this helpful?