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 ...
随机推荐
- vue-axios插件、django-cors插件、及vue如何使用第三方前端样式库:element/jQuery/bootstrap
目录 一.vue的ajax插件:axios 1.安装axios 2.axios参数 二.CORS跨域问题(同源策略) 1.Django解决CORS跨域问题方法 三.前端请求携带参数及Django后台如 ...
- mtk相机冷启动拆解
1 概述 冷启动大致可以分成以下几块内容: S0 (system) 主要是 Activity 的创建耗时(从 Touch up,即 ptr:up 开始) ptr:up S1 App 从 Activit ...
- jQuery实现QQ简易聊天框
实现效果: html代码: <section id="chat"> <div class="chatBody"></div> ...
- java 集合 + 常见面试题
1.1. 集合概述 1.1.1. Java 集合概览 从下图可以看出,在 Java 中除了以 Map 结尾的类之外, 其他类都实现了 Collection 接口. 并且,以 Map 结尾的类都实现了 ...
- 剑指 Offer 40. 最小的k个数 + 优先队列 + 堆 + 快速排序
剑指 Offer 40. 最小的k个数 Offer_40 题目描述 解法一:排序后取前k个数 /** * 题目描述:输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7. ...
- SpringMVC执行流程及源码分析
SpringMVC流程及源码分析 前言 学了一遍SpringMVC以后,想着做一个总结,复习一下.复习写下面的总结的时候才发现,其实自己学的并不彻底.牢固.也没有学全,视频跟书本是要结合起来一起, ...
- Java I/O流 03
I/O流·字符流 字符流FileReader * A:字符流是什么 * 字符流是可以直接读写字符的 IO流 * 字符流读取字符,就要先读取到字节数据,然后转换为字符:如果要写出字符,需要把字符转换为字 ...
- 【RocketMQ源码分析】深入消息存储(1)
最近在学习RocketMQ相关的东西,在学习之余沉淀几篇笔记. RocketMQ有很多值得关注的设计点,消息发送.消息消费.路由中心NameServer.消息过滤.消息存储.主从同步.事务消息等等. ...
- Redis 高并发带来的一些问题
前言 本文讲述Redis在遇到高并发时的一些问题.即遇到大量请求时需要思考的点,如缓存穿透 缓存击穿 缓存雪崩 热key处理.一般中小型传统软件企业,很难碰到这个问题.如果有大并发的项目,流量有几百万 ...
- rest framework ViewSet
ViewSets 路由选择确定要用于一个请求哪个控制器之后,控制器负责做出请求的感并产生相应的输出. - Ruby on Rails的文档 Django的REST框架允许你的逻辑一组在一个类中的相关意 ...