Maximum Size Subarray Sum Equals k LT325

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:

Given nums = [1, -1, 5, -2, 3], k = 3,
return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)

Example 2:

Given nums = [-2, -1, 2, 1], k = 1,
return 2. (because the subarray [-1, 2] sums to 1 and is the longest)

Follow Up:
Can you do it in O(n) time?

Idea 1. HashMap to store (prefixSum, the first index prefixSum ends) + prefix subarray sum.

Time complexity: O(n)

Space complexity: O(n)

 1 public class Solution {
 2     public int maxSubArrayLen(int[] nums, int k) {
 3         int maxLen = 0;
 4         Map<Integer, Integer> sumIndex = new HashMap<>();
 5         sumIndex.put(0, -1);
 6
 7         int sum = 0;
 8         for(int i = 0; i < nums.length; ++i) {
 9             sum += nums[i];
10             Integer left = sumIndex.get(sum - k);
11             if(left != null) {
12                 maxLen = Math.max(maxLen, i - left);
13             }
14             sumIndex.putIfAbsent(sum, i);
15         }
16         return maxLen;
17     }
18 }

原文地址:https://www.cnblogs.com/taste-it-own-it-love-it/p/10425222.html

时间: 2024-10-25 22:19:21

Maximum Size Subarray Sum Equals k LT325的相关文章

[LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

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. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

[Locked] Maximum Size Subarray Sum Equals k

Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest) Example 2: Given nums = [-2, -1, 2, 1], k = 1,return 2. (because the subarray [-1, 2] sums to 1 and is the longest) Follow U

Leetcode: Maximum Size Subarray Sum Equals k

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. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3, return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is th

LeetCode 325. Maximum Size Subarray Sum Equals k

类似sum的题都可以想一想可不可以把sum依次求出来,然后 [i+1,j] 的sum 直接用 sum[j]-sum[i]来求. 由于这道题有负数存在,没办法用sliding window来做,只能另辟蹊径. 用一个hashtable保存到目前为止的sum所对应的index,这样之后知道寻找 sum-k 在不在hashtable里就行了.如果遇到sum相同的情况,不要更新hashtable,因为更新了,subarray的size必然是减小的. class Solution { public: in

325. Maximum Size Subarray Sum Equals k

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: Given

[?]*Maximum Size Subarray Sum Equals k

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. Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the

[LintCode] 1844. subarray sum equals k II

Given an array of integers and an integer k, you need to find the minimum size of continuous subarrays whose sum equals to k, and return its length. if there are no such subarray, return -1. Example Example1 Input: nums = [1,1,1,2] and k = 3 Output:

560. Subarray Sum Equals K

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers

[leetcode 560. Subarray Sum Equals K]

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. Example 1: Input:nums = [1,1,1], k = 2 Output: 2 Note: The length of the array is in range [1, 20,000]. The range of numbers