1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
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 };
参考:
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit的更多相关文章
- 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
- LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18
674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...
- [LC] 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
随机推荐
- 为WebView 同步cookie
import android.os.Build;import android.text.TextUtils;import android.webkit.CookieManager;import and ...
- 原生JS快速实现拖放
原生 JS 快速实现拖放 拖放是很常见的一种交互效果,很多时候我们都会借助于第三方的控件来实现,其实用原生 js 实现起来也非常的方便.接下来我们就用原生 js 和 css 快速实现拖放效果 html ...
- 记录core中GRPC长连接导致负载均衡不均衡问题 二,解决长连接问题
题外话: 1.这几天收到蔚来的面试邀请,但是自己没做准备,并且远程面试,还在上班时间,再加上老东家对我还不错.没想着换工作,导致在自己工位上做算法题不想被人看见,然后非常紧张.估计over了.不过没事 ...
- 182. 查找重复的电子邮箱 + group by + having
182. 查找重复的电子邮箱 LeetCode_MySql_182 题目描述 方法一:使用笛卡尔积 # Write your MySQL query statement below select di ...
- python flask框架详解
Flask是一个Python编写的Web 微框架,让我们可以使用Python语言快速实现一个网站或Web服务.本文参考自Flask官方文档, 英文不好的同学也可以参考中文文档 1.安装flask pi ...
- 键盘--扫描码--ASCII码--显示器上的字符
在上一篇,我讲了键盘操作会产生扫描码以及如何解析Pause键和Print Screen键的扫描码. 在这一篇,我会说清楚"键盘上的输入为什么会出现在显示器上". 极简版 我们敲击键 ...
- RabbitMQ镜像队列集群搭建、与SpringBoot整合
镜像模式 集群模式非常经典的就是Mirror镜像模式,保证100%数据不丢失,在实际工作中也是用的最多的,并且实现集群比较的简单. Mirror镜像队列,目的是为了保证 RabbitMQ 数据的高可靠 ...
- java实现简易的图书馆管理系统
比较适合新手练手的一个简易项目 直接上源码 Book package code; /** * @author ztr * @version 创建时间:2021年3月4日 下午8:21:40 * 类说明 ...
- 开灯问题3_2(JAVA语言)
package 第三章; public class 开灯问题3_2 { public static void main(String[] args) { // TODO Auto-generated ...
- 想了解FlinkX-Oracle Logminer?那就不要错过这篇文章
FlinkX-Oracle Logminer模块是FlinkX基于Logminer对Oracle重做日志进行实时采集分析,可对Oracle进行实时同步也可以通过指定SCN或者时间戳从某个节点进行同步, ...