【leetcode】Array—— Minimum Size Subarray Sum(209)
来源:互联网 发布:单兵数字侦察软件 编辑:程序博客网 时间:2024/05/29 03:16
题目:
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.
For example, given the array [2,3,1,2,4,3]
and s = 7
,
the subarray [4,3]
has the minimal length under the problem constraint.
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
思路 O(n):维护两个指针,即一个窗口,统计两指针之间数字的累积和。
leetcode解释:Since the given array contains only positive integers, the subarray sum can only increase by including more elements. Therefore, you don't have to include more elements once the current subarray already has a sum large enough. This gives the linear time complexity solution by maintaining a minimum window with a two indices.
代码:
private int solveN(int s, int[] nums) { int start = 0, end = 0, sum = 0, minLen = Integer.MAX_VALUE; while (end < nums.length) { while (end < nums.length && sum < s) sum += nums[end++]; if (sum < s) break; while (start < end && sum >= s) sum -= nums[start++]; if (end - start + 1 < minLen) minLen = end - start + 1; } return minLen == Integer.MAX_VALUE ? 0 : minLen; }
思路 N logN:用一个长度为nums.length+1的数组统计nums数组的累计和,该求和数组为递增的,可以用binary search进行查找
leetcode解释:As to NLogN solution, logN immediately reminds you of binary search. In this case, you cannot sort as the current order actually matters. How does one get an ordered array then? Since all elements are positive, the cumulative sum must be strictly increasing. Then, a subarray sum can expressed as the difference between two cumulative sum. Hence, given a start index for the cumulative sum array, the other end index can be searched using binary search.
代码:
private int solveNLogN(int s, int[] nums) { int[] sums = new int[nums.length + 1]; for (int i = 1; i < sums.length; i++) sums[i] = sums[i - 1] + nums[i - 1]; int minLen = Integer.MAX_VALUE; for (int i = 0; i < sums.length; i++) { int end = binarySearch(i + 1, sums.length - 1, sums[i] + s, sums); if (end == sums.length) break; if (end - i < minLen) minLen = end - i; } return minLen == Integer.MAX_VALUE ? 0 : minLen; } private int binarySearch(int lo, int hi, int key, int[] sums) { while (lo <= hi) { int mid = (lo + hi) / 2; if (sums[mid] >= key){ hi = mid - 1; } else { lo = mid + 1; } } return lo; }
- 【leetcode】Array—— Minimum Size Subarray Sum(209)
- LeetCode刷题【Array】Minimum Size Subarray Sum
- leetcode——Minimum Size Subarray Sum
- LeetCode—Minimum Size Subarray Sum
- [Array]Minimum Size Subarray Sum
- [Leetcode 209, Medium] Minimum Size Subarray Sum
- LeetCode 209 - Minimum Size Subarray Sum
- leetcode 209 : Minimum Size Subarray Sum
- Leetcode NO.209 Minimum Size Subarray Sum
- leetcode-209 Minimum Size Subarray Sum
- Minimum Size Subarray Sum - LeetCode 209
- Leetcode #209 Minimum Size Subarray Sum
- Leetcode 209 Minimum Size Subarray Sum
- LeetCode(209)Minimum Size Subarray Sum
- [leetcode-209]Minimum Size Subarray Sum(java)
- leetcode 209: Minimum Size Subarray Sum
- [leetcode] 209 Minimum Size Subarray Sum
- [LeetCode 209] Minimum Size Subarray Sum
- ThinkPHP函数详解:A方法
- tnemelEkaePdniF.162
- HTTP基本认证
- android之回调函数的意义
- iOS 集成银联支付
- 【leetcode】Array—— Minimum Size Subarray Sum(209)
- OpenStack配置解析库oslo.config的使用方法
- linux下如何更改服务器的系统时间
- Angularjs Ng_repeat中实现复选框选中并显示不同的样式
- 字母统计
- iOS Please verify that your device’s clock is properly set, and that your signing certificate is not
- html+js 设为首页 完全兼容IE
- android原生的数据库实现(ContentProvider+SQLiteOpenHelper)
- 【Android】常见面试题(一)