1classSolution {2publicintlongestSubarray(int[] nums,intlimit) {3//all the elements are decending4Deque<Integer> maxQueue =newArrayDeque<>();5//all the elements are increasing6Deque<Integer> minQueue =newArrayDeque<>();7intres = 0;8intstart = 0;9for(intend = 0; end < nums.length; ...
classSolution {public:intlongestSubarray(vector<int>& nums,intlimit) {intn =nums.size(); deque<int> deq;//降序队列 记录最大值deque<int> inq;//升序队列 记录最小值intl =0, r =0, ans =0;while(r <n) {intx =nums[r];while(deq.size() && nums[deq.back()] <=x) { deq.pop_ba...
class Solution { public int longestSubarray(int[] nums) { // 其实只需要统计0的数量 int maxResult = 0; int left = 0; int zeroCount = 0; for (int right = 0; right < nums.length; right++) { if (nums[right] == 0) { zeroCount++; } while (zeroCount > 1) { if (nums[left]...
数据范围比较小, 可以直接暴力循环, 也可以用灵神的分组循环做. classSolution{publicintlongestMonotonicSubarray(int[]nums){int max=1;int cnt=1;int n=nums.length;for(int i=1;i<n;i++){if(nums[i]>nums[i-1]){cnt++;max=Math.max(max,cnt);}else{cnt=1;}}cnt=1;for(int i=1;i<n;i+...
[leetcode] 978. Longest Turbulent Subarray Description A subarray A[i], A[i+1], …, A[j] of A is said to be turbulent if and only if: For i <= k < j, A[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;...
[leetcode] 845. Longest Mountain in Array Description Let’s call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < … B[i-1] < B[i] > B[i+1] > … > B[...
https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/ 给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit 。
链接:https://leetcode.cn/problems/longest-subarray-of-1s-after-deleting-one-element 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路是滑动窗口。题目告诉我们可以最多删除一个元素,并且找的是只包含 1 的子数组。那么我们可以换一个思考方式,我们去找最多只包含一个 0 的最长的...
sum:window内1的数量 while循环(纠正window size), we want to find a window that only contains a single zero and the rest of the elements are all ones. https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/discuss/708201/javaPython-3-Sliding-Window-with-at-...
https://github.com/grandyang/leetcode/issues/978 类似题目: Maximum Subarray 参考资料: https://leetcode.com/problems/longest-turbulent-subarray/ https://leetcode.com/problems/longest-turbulent-subarray/discuss/221935/Java-O(N)-time-O(1)-space ...