您的位置:首页 > 其它

【Leetcode】Maximum Size Subarray Sum Equals k

2016-02-01 03:30 330 查看
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?

——————————————————————————————————————————————————————————————

I came across this idea when I was preparing for an interview. The original problem is "You are given an array in which you’ve
to find a contiguous subarray such that the sum of elements in it is equal to zero." I was thinking a method that sum up all the array and gradually kick out element from the array from left until the sum becomes smaller than the target. Like this:

1,2,3,4,5,6 and the target is 12.

We sum up all equals 21, and cut left one by one: 20 -> 18 -> 15 ->11, and suddenly 11<12 so we re-use previous left and start cutting the right. then it becomes 3,4,5 and this is the answer.

However..........

This is not the question. When I search the question, I found one in Leetcode and this one is obviously harder than the one I came across. 1) All element could be positive or negative or 0. 2) the target could be any number.

1. My naive solution is we use a bigger array to contains the accumulated sum from left to right. E.g., [1,2,3,4,5] -> [0,1,3,6,10,15]. Why we need this array as long as arr.length+1? Because we hope when we calculate the "interval", we wish from the very
start including the first element.

public int maxSubArrayLen(int[] nums, int k) {
if(nums==null || nums.length == 0)
return 0;
int[] store = new int[nums.length+1];
for(int i=0;i<store.length;i++){
if(i==0){
store[0] = 0;
}
else
store[i] = store[i-1] + nums[i-1];
}
int max = 0;
for(int i=0;i<store.length;i++){
for(int j=i;j<store.length;j++){
if((store[j] - store [i]) == k){
if(max < j-i)
max = j-i;
}
}
}
return max;
}


Be that as it may, this solution is very inefficient with the complexity of O(n^2). Then I started to look for a solution with higher efficiency, better to be O(n).

How come? The main problem is our double for-loop in the code so I apparently think about HashMap in order to use O(1) for searching the corresponding element.

public int maxSubArrayLen(int[] nums, int k) {
if (nums == null || nums.length == 0)
return 0;
int n = nums.length;
for (int i = 1; i < n; i++)
nums[i] += nums[i - 1];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, -1); // add this fake entry to make sum from 0 to j consistent
int max = 0;
for (int i = 0; i < n; i++) {
if (map.containsKey(nums[i] - k))
max = Math.max(max, i - map.get(nums[i] - k));
if (!map.containsKey(nums[i])) // keep only 1st duplicate as we want first index as left as possible
map.put(nums[i], i);
}
return max;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: