【Leetcode】Maximum Size Subarray Sum Equals k
来源:互联网 发布:冰箱哪个牌子好 知乎 编辑:程序博客网 时间:2024/06/04 22:34
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; }
- 【Leetcode】Maximum Size Subarray Sum Equals k
- leetcode Maximum Size Subarray Sum Equals k
- Leetcode: Maximum Size Subarray Sum Equals k
- [leetcode] 325. Maximum Size Subarray Sum Equals k 解题报告
- LeetCode 325. Maximum Size Subarray Sum Equals k
- Leetcode 325. Maximum Size Subarray Sum Equals k
- Leetcode 325. Maximum Size Subarray Sum Equals k (Medium) (cpp)
- LeetCode 325. Maximum Size Subarray Sum Equals k
- leetcode 325 Maximum Size Subarray Sum Equals k
- [Leetcode] 325. Maximum Size Subarray Sum Equals k 解题报告
- [LeetCode325] Maximum Size Subarray Sum Equals k
- Maximum Size Subarray Sum Equals k
- Maximum Size Subarray Sum Equals k
- Maximum Size Subarray Sum Equals k
- Maximum Size Subarray Sum Equals k
- 325. Maximum Size Subarray Sum Equals k
- 325. Maximum Size Subarray Sum Equals k
- 325. Maximum Size Subarray Sum Equals k
- 第一篇博客
- 在 Windows 平台上运行 FreeRTOS 示例程序
- Codeforce 题目621C Wet Shark and Flowers(期望)
- 一个简单的DBHelper
- openCV学习记录:滤镜:碎片&暗调
- 【Leetcode】Maximum Size Subarray Sum Equals k
- 【Leetcode】Subsets
- 蓝桥杯 算法训练 2的次幂表示(构造)
- windows命令行长度的限制
- windows server上配置 apache + php + MySQL + wordpress
- git之安装配置篇
- ubuntu下git安装使用
- ios9 第三方SDK无法回调问题
- 【Leetcode】Reverse LinkedList