File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ // submission link: https://leetcode.com/problems/sliding-window-maximum/
2+
3+
4+ struct maxQueue {
5+ deque<pair<int , int >> q; // we maintain decreasing order
6+ int _size;
7+ maxQueue () {
8+ _size = 0 ;
9+ }
10+ void push (int num) {
11+ int inFront = 0 ;
12+ while (!q.empty () && q.back ().first < num) {
13+ inFront += q.back ().second + 1 ; // previous_deletions + curent_deletions
14+ q.pop_back ();
15+ }
16+ q.push_back ({num, inFront});
17+ _size += 1 ;
18+ }
19+
20+ int max () {
21+ return q.front ().first ;
22+ }
23+
24+ void pop () {
25+ int num = q.front ().first ;
26+ int inFront = q.front ().second ;
27+ q.pop_front ();
28+ if (inFront) {
29+ inFront -= 1 ;
30+ q.push_front ({num, inFront});
31+ }
32+ _size -= 1 ;
33+ }
34+
35+ int size () {
36+ return _size;
37+ }
38+ };
You can’t perform that action at this time.
0 commit comments