-
Notifications
You must be signed in to change notification settings - Fork 359
/
s2.cpp
27 lines (27 loc) · 984 Bytes
/
s2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// OJ: https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
int longestSubarray(vector<int>& A, int k) {
deque<int> mx{A[0]}, mn{A[0]};
int N = A.size(), ans = 1, i = 0, j = 1;
while (j < N) {
while (j < N && mx.front() - mn.front() <= k) {
while (mx.size() && mx.back() < A[j]) mx.pop_back();
mx.push_back(A[j]);
while (mn.size() && mn.back() > A[j]) mn.pop_back();
mn.push_back(A[j]);
++j;
if (mx.front() - mn.front() <= k) ans = max(ans, j - i);
}
while (i < j && mx.front() - mn.front() > k) {
if (A[i] == mx.front()) mx.pop_front();
if (A[i] == mn.front()) mn.pop_front();
++i;
}
}
return ans;
}
};