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.
[balabala] O(N)的做法是两指针法或者叫滑动窗口法, right 向前走直到sum > s, 然后left 向前走直到sum < s, 同时更新minLen. 网上找的O(nlogn) 方法不理解,先备忘着,实现之一如Method3, 实现2参见http://www.tuicool.com/articles/miQVzm3
// Method 2: 伸缩滑动窗口法,O(N), 实现2 public int minSubArrayLen2(int s, int[] nums) { if (nums == null || nums.length == 0) return 0; int left = 0, right = 0, sum = 0, minLen = nums.length; while (right < nums.length) { while (sum < s && right < nums.length) { sum += nums[right++]; } while (sum >= s && left < right) { minLen = Math.min(minLen, right - left); sum -= nums[left++]; } } return left > 0 ? minLen : 0; } // Method 1: 伸缩滑动窗口法,O(N),实现1 public int minSubArrayLen(int s, int[] nums) { if (nums == null || nums.length == 0) return 0; int left = 0, right = 0, sum = 0, minLen = nums.length; while (left <= right) { if (sum < s && right < nums.length) { sum += nums[right++]; } else if (sum < s && right == nums.length) { break; } if (sum >= s) { minLen = Math.min(minLen, right - left); if (minLen == 1) return 1; sum -= nums[left++]; } } return left > 0 ? minLen : 0; } // 在不下降的序列中寻找恰好比target小的数出现位置,也即最后一个比target小的数出现的位置 int binarySearchIncreaseLastSmaller(int l, int r, int target, int * nums) { if (l >= r) return -1; while (l < r - 1) { int m = l + ((r - l) >> 1); if (nums[m] < target) l = m; else r = m - 1; } if (nums[r] < target) return r; else if (nums[l] < target) return l; else return -1; } // Method 3: O (nlogn) int minSubArrayLen(int s, int * nums, int numsSize) { int * Sum = (int*)malloc(sizeof(int) * (numsSize + 1)), minL = numsSize + 1; Sum[0] = 0; for (int i = 1; i <= numsSize; i++) Sum[i] = Sum[i - 1] + nums[i - 1]; for (int i = 1; i <= numsSize; i++) { if (Sum[i] >= s) { int k = Sum[i]; int BeforePos = binarySearchIncreaseLastSmaller(0, i, Sum[i] - s + 1, Sum); if (BeforePos != -1 && i - BeforePos < minL) minL = i - BeforePos; } } return minL == numsSize + 1 ? 0 : minL; }
相关推荐
《leetcode-solutions》,刷算法题,需要有一定的英文阅读能力。。。
Algorithm-LeetCode-Sol-Res.zip,干净,易懂的解决方案和资源,为leetcode在线判断算法问题。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
IDEA 插件,lettcode刷题,leetcode-editor7.4版本下载进行本地导入(直接将压缩包拖进IDEA即可)
Algorithm-leetcode-spider.zip,leetcode公司,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
leetcode 答案解析 golang解答
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
leetcode-helper-1.7.1
然后进入到LeetCode-Spider目录中修改config.json,其中outputDir需要填写该工程的/docs/views文件夹路径 { "username": "aaa", "password": "bbb", "outputDir": "/Users/liuyao/Downloads/LeetCode-Blog-Test/docs...
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
leetcode-tag-dynamic programming
leetcode-tag-Tree
leetcode-tag-Stack
leetcode-问题-爬虫 目录由cd problems && npx leetcode-problems-crawler -r 1-10生成cd problems && npx leetcode-problems-crawler -r 1-10 用法 爬行问题1到5: $ npx leetcode-problem-crawler -r 1-5 爬行问题...
leetcode-tag-array