-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.cpp
163 lines (141 loc) · 6.34 KB
/
1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//sliding windows + heap
//Runtime: 364 ms, faster than 14.29% of C++ online submissions for Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.
//Memory Usage: 34.6 MB, less than 100.00% of C++ online submissions for Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
int n = nums.size();
int ans = 0;
int slow = 0;
// sort(nums.begin(), nums.end());
// for(int i = 0; i < n; i++){
// cout << nums[i] << " ";
// }
// cout << endl;
priority_queue<pair<int,int>, vector<pair<int,int>>> pqMax; //max popped first
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pqMin; //min popped first
int wMaxIdx = 0, wMinIdx = 0;
for(int fast = 0; fast < n; fast++){
pqMax.push(make_pair(nums[fast], fast));
pqMin.push(make_pair(nums[fast], fast));
//find a valid slow, pop all invalid element from pqMin and pqMax
// cout << "before [" << slow << ", " << fast << "]" << endl;
// cout << "max: " << pqMax.top().first << ", " << pqMax.top().second << endl;
// cout << "min: " << pqMin.top().first << ", " << pqMin.top().second << endl;
while(pqMax.top().first - pqMin.top().first > limit){
if(pqMax.top().second < pqMin.top().second){
//remove max value
//can't simply use "slow = pqMax.top().second+1;"
slow = max(slow, pqMax.top().second+1);
pqMax.pop();
}else{
slow = max(slow, pqMin.top().second+1);
pqMin.pop();
}
// cout << "max: " << pqMax.top().first << ", " << pqMax.top().second << endl;
// cout << "min: " << pqMin.top().first << ", " << pqMin.top().second << endl;
}
// if(fast - slow + 1 > ans){
// cout << "after [" << slow << ", " << fast << "]" << endl;
// }
//fast - slow + 1: window size
ans = max(ans, fast - slow + 1);
//outside the window
while(pqMax.top().second < slow){
pqMax.pop();
}
while(pqMin.top().second < slow){
pqMin.pop();
}
}
return ans;
}
};
//sliding window + deque, revised from 1425. Constrained Subsequence Sum.cpp
//Runtime: 136 ms, faster than 42.86% of C++ online submissions for Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.
//Memory Usage: 35.1 MB, less than 100.00% of C++ online submissions for Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
int n = nums.size();
int ans = 0;
int slow = 0;
deque<pair<int,int>> maxQ;
deque<pair<int,int>> minQ;
int wMaxIdx = 0, wMinIdx = 0;
for(int fast = 0; fast < n; fast++){
while(maxQ.size() > 0 && maxQ.back().first < nums[fast]){
maxQ.pop_back();
}
while(minQ.size() > 0 && minQ.back().first > nums[fast]){
minQ.pop_back();
}
maxQ.push_back(make_pair(nums[fast], fast));
minQ.push_back(make_pair(nums[fast], fast));
//find a valid slow
while(maxQ.front().first - minQ.front().first > limit){
if(maxQ.front().second < minQ.front().second){
//remove max value
slow = max(slow, maxQ.front().second+1);
maxQ.pop_front();
}else{
slow = max(slow, minQ.front().second+1);
minQ.pop_front();
}
}
// cout << slow << ", " << fast << endl;
ans = max(ans, fast - slow + 1);
//outside the window
while(maxQ.front().second < slow){
maxQ.pop_front();
}
while(minQ.front().second < slow){
minQ.pop_front();
}
}
return ans;
}
};
//sliding window + deque, cleaner
//https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609771/JavaC%2B%2BPython-Deques-O(N)
//Runtime: 108 ms, faster than 71.43% of C++ online submissions for Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.
//Memory Usage: 32.8 MB, less than 100.00% of C++ online submissions for Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit.
//time: O(N), space: O(N)
class Solution {
public:
int longestSubarray(vector<int>& nums, int limit) {
int n = nums.size();
int ans = 0;
int slow = 0;
deque<int> maxQ;
deque<int> minQ;
for(int fast = 0; fast < n; fast++){
while(!maxQ.empty() && maxQ.back() < nums[fast]) maxQ.pop_back();
while(!minQ.empty() && minQ.back() > nums[fast]) minQ.pop_back();
/*
we don't need to record the index, why?
*/
maxQ.push_back(nums[fast]);
minQ.push_back(nums[fast]);
//find a valid slow, dealing with the data structure and slow pointer at the same time
while(maxQ.front() - minQ.front() > limit){
// cout << slow << ", " << fast << endl;
if(maxQ.front() == nums[slow])maxQ.pop_front();
if(minQ.front() == nums[slow])minQ.pop_front();
slow++;
}
// cout << "** " << slow << ", " << fast << " **" << endl;
ans = max(ans, fast - slow + 1);
// the data structure is already processed together with slow
// //outside the window
// while(maxQ.front().second < slow){
// maxQ.pop_front();
// }
// while(minQ.front().second < slow){
// minQ.pop_front();
// }
}
cout << endl;
return ans;
}
};