File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int longestSubarray (vector<int >& nums, int limit) {
4
+ deque<int > max_q;
5
+ deque<int > min_q;
6
+ int n = nums.size ();
7
+ int l = 0 ;
8
+
9
+ for (int r = 0 ; r < n; ++r) {
10
+ while (!max_q.empty () && nums[max_q.back ()] < nums[r]) {
11
+ max_q.pop_back ();
12
+ }
13
+ while (!min_q.empty () && nums[min_q.back ()] > nums[r]) {
14
+ min_q.pop_back ();
15
+ }
16
+ max_q.push_back (r);
17
+ min_q.push_back (r);
18
+
19
+ if (nums[max_q.front ()] - nums[min_q.front ()] > limit) {
20
+ ++l;
21
+ if (max_q.front () < l) {
22
+ max_q.pop_front ();
23
+ }
24
+ if (min_q.front () < l) {
25
+ min_q.pop_front ();
26
+ }
27
+ }
28
+ }
29
+ return n - l;
30
+ }
31
+ };
You can’t perform that action at this time.
0 commit comments