【Leetcode】Maximum Size Subarray Sum Equals k
2016-02-01 03:30
330 查看
Example 1:
Given nums =
return
Example 2:
Given nums =
return
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.
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.
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; }
相关文章推荐
- [Regex Expression] Use Shorthand to Find Common Sets of Characters
- [Regex Expression] Find Sets of Characters
- STLport在vc6中的集成
- SDK Hello world(直接使用SDK封装)
- [Regular Expressions] Find Repeated Patterns
- 一个类有多个基类的内存布局
- openCV学习记录:滤镜:重影&暗调
- 写个测试程序看看磁盘映像文件中哪个扇区可以使用?
- 在服务中以当前用户身份启动一个程序
- curl详解
- [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符
- Linux学习笔记之yum安装mysql5.7
- [Regular Expressions] Find Plain Text Patterns
- ubuntu14.04:php7+apache2+mysql
- 在类有成员变量的场景下, 按照虚表原理, 模拟虚函数实现
- Codeforce 题目621C Wet Shark and Flowers(期望)
- 一个简单的DBHelper
- 视频课程计划
- java 将微信录音amr转换为mp3格式
- 在 Windows 平台上运行 FreeRTOS 示例程序