Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int longestSubarray(vector<int>& A, int limit) {
- deque<int> maxd, mind;
- int i = 0, j;
- int ans = 0;
- for (j = 0; j < A.size(); ++j) {
- while(!maxd.empty() && A[j] > maxd.back()) maxd.pop_back();
- while(!mind.empty() && A[j] < mind.back()) mind.pop_back();
- maxd.push_back(A[j]);
- mind.push_back(A[j]);
- while(maxd.front() - mind.front() > limit){
- if(maxd.front() == A[i]) maxd.pop_front();
- if(mind.front() == A[i]) mind.pop_front();
- ++i;
- }
- ans = max(ans, j-i+1);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement