forked from shuvodas0/postfix-infix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15 NOV Longest Perfect Piece
62 lines (26 loc) · 988 Bytes
/
15 NOV Longest Perfect Piece
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
class Solution {
public:
int longestPerfectPiece(int arr[], int N) {
// code here
priority_queue<pair<int ,int>> Max;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Min;
int left = 0, right = 0;
int ans = 0;
while(right < N)
{
//push element into queue
Min.push({arr[right], right});
Max.push({arr[right], right});
// check if left - right > 1
while(left < right and Max.top().first - Min.top().first > 1){
left++;
// check if min or max is out of window
while(Max.top().second < left) Max.pop();
while(Min.top().second < left) Min.pop();
}
ans = max(ans, right - left + 1);
right++;
}
return ans;
}
};