Given an array of integers nums and an integer limit, return the size of the longest continuous subarray such that the absolute difference between any two elements is less than or equal to limit.

In case there is no subarray satisfying the given condition return 0.

Example 1:

Input: nums = [8,2,4,7], limit = 4
Output: 2
Explanation: All subarrays are:
[8] with maximum absolute diff |8-8| = 0 <= 4.
[8,2] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4] with maximum absolute diff |8-2| = 6 > 4.
[8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
[2] with maximum absolute diff |2-2| = 0 <= 4.
[2,4] with maximum absolute diff |2-4| = 2 <= 4.
[2,4,7] with maximum absolute diff |2-7| = 5 > 4.
[4] with maximum absolute diff |4-4| = 0 <= 4.
[4,7] with maximum absolute diff |4-7| = 3 <= 4.
[7] with maximum absolute diff |7-7| = 0 <= 4.
Therefore, the size of the longest subarray is 2.

Example 2:

Input: nums = [10,1,2,4,7,2], limit = 5
Output: 4
Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.

Example 3:

Input: nums = [4,2,2,2,4,4,2,2], limit = 0
Output: 3

Constraints:

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 0 <= limit <= 10^9

题意:

  给出一个数组,求连续子串的最大长度,所求的子串要求满足 |max_element - min_element| < limit

思路:

  用两个双向队列来模拟,max_deque用来存储当前所在位置之前元素的最大值,min_deque用来存储当前所在位置之前所有元素的最小值。用两个指针leftPointer和rightPointer来寻找最长的subArray,通过对rightPointer向后迭代,更新max_deque 和 min_deque,同时通过shrink leftPointer来使subArray满足题目要求。

Code:

 1 class Solution {
2 public:
3 int longestSubarray(vector<int>& nums, int limit) {
4 deque<int> max_deque, min_deque;
5 int left = 0, ans = 0;
6 for (int right = 0; right < nums.size(); ++right) {
7 while (!max_deque.empty() && max_deque.back() < nums[right])
8 max_deque.pop_back();
9 while (!min_deque.empty() && min_deque.back() > nums[right])
10 min_deque.pop_back();
11 max_deque.push_back(nums[right]);
12 min_deque.push_back(nums[right]);
13 while (max_deque.front() - min_deque.front() > limit) {
14 if (max_deque.front() == nums[left]) max_deque.pop_front();
15 if (min_deque.front() == nums[left]) min_deque.pop_front();
16 left++;
17 }
18 ans = max(ans, right - left + 1);
19 }
20 return ans;
21 }
22 };

参考:

  https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609743/Java-Detailed-Explanation-Sliding-Window-Deque-O(N)

1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit的更多相关文章

  1. 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...

  2. [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  3. [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...

  4. [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  5. 674. Longest Continuous Increasing Subsequence最长连续递增子数组

    [抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...

  6. 674. Longest Continuous Increasing Subsequence@python

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  7. LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...

  8. LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18

    674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...

  9. [LC] 674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

随机推荐

  1. Linux 安装python 模块及库

    转载于https://blog.csdn.net/csdn_am/article/details/79924744 有时我们使用下载python 自带的pip 安装一些工具包时,会报如下错误 找不到满 ...

  2. xscan的安装和使用(作业整理)

    1.将学习通上下载的xscan.rar进行解压. 2.将缺少的.dll文件粘贴到软件解压目录中. 3.点击打开软件. 3.1在运行中除了发现缺少.dll文件的问题,我电脑又出现类似问题, 采取了关闭防 ...

  3. Java语言的魅力

    Java语言的简介 Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承.指针等概念,因此Java语言具有功能强大和简单易用两个特征.Java语言作为静态面 ...

  4. .NET MVC & Web API Cors让AJAX 实现跨域

    什么是Cors? CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpReq ...

  5. Java 哈希表(google 公司的上机题)

    1 哈希表(散列)-Google 上机题 1) 看一个实际需求,google 公司的一个上机题: 2) 有一个公司,当有新的员工来报道时,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该 ...

  6. python graphviz的使用(画图工具)

    参考文章1 参考文章2 一.graphviz安装及配置 graphviz实际上是一个绘图工具,可以根据dot脚本画出树形图等. 1.windows安装 安装graphviz软件:https://gra ...

  7. python-给一个参数n,例如3:先输出1,2,3,4,5,6,7,8,9,每三个数后换行,后输出1,4,7,2,5,8,3,6,9

    """ 2 定义一个函数,fn(n)其中n表示输入n行n列的矩阵,需要满足的要求是在n为 3时先输出 3 1 2 3 4 4 5 6 5 7 8 9 6 后输出 7 1 ...

  8. Linux Python2 升级到 Python3

    国内企业服务器用 CentOS 系统的比较多,CentOS8 系统已经将系统默认 Python 版本调整为了 Python3,但是 CentOS7 的存量还是很大,毕竟对企业生产服务来说稳定大于一切, ...

  9. E - Recursive sequence HDU - 5950 (矩阵快速幂)

    题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...

  10. PTA 链表逆置

    6-3 链表逆置 (20 分)   本题要求实现一个函数,将给定单向链表逆置,即表头置为表尾,表尾置为表头.链表结点定义如下: struct ListNode { int data; struct L ...