-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day30.cpp
67 lines (53 loc) · 1.16 KB
/
Day30.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
// Stack & Queue
// https://www.interviewbit.com/problems/stack-queue/
/*
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
*/
// Complete the given function
vector<int> solve(vector<int> &A, int B){
int n = A.size();
vector<int> result;
deque<int> dq;
for (int i = 0; i < B; i++) {
while (!dq.empty() && A[i] >= A[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
for (int i = B; i < n; i++) {
result.push_back(A[dq.front()]);
while (!dq.empty() && dq.front() <= i - B) {
dq.pop_front();
}
while (!dq.empty() && A[i] >= A[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
result.push_back(A[dq.front()]);
return result;
}
/*
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int> A(n);
for(int i = 0; i < n; i++){
cin>>A[i];
}
int B;
cin>>B;
vector<int> ans = solve(A, B);
for(int i = 0; i < ans.size(); i++){
cout<<ans[i]<<" ";
}
cout<<endl;
}
return 0;
}
*/