1+ /**
2+ *@param {number[] } nums
3+ *@param {number } k
4+ *@return {number[] }
5+ */
6+ function Node ( value ) {
7+ this . value = value ;
8+ this . prev = null ;
9+ this . next = null ;
10+ } ;
11+
12+ function Deque ( ) {
13+ this . left = null ;
14+ this . right = null ;
15+ this . size = 0 ;
16+ this . pushRight = function ( value ) {
17+ const node = new Node ( value ) ;
18+ if ( this . size == 0 ) {
19+ this . left = node ;
20+ this . right = node ;
21+ } else {
22+ this . right . next = node ;
23+ node . prev = this . right ;
24+ this . right = node ;
25+ }
26+ this . size ++ ;
27+ return this . size ;
28+ } ;
29+ this . popRight = function ( ) {
30+ if ( this . size == 0 ) return null ;
31+ const removedNode = this . right ;
32+ this . right = this . right . prev ;
33+ if ( this . right ) this . right . next = null ;
34+ this . size -- ;
35+ return removedNode ;
36+ } ;
37+ this . pushLeft = function ( value ) {
38+ const node = new Node ( value ) ;
39+ if ( this . size == 0 ) {
40+ this . left = node ;
41+ this . right = node ;
42+ } else {
43+ this . left . prev = node ;
44+ node . next = this . left ;
45+ this . left = node ;
46+ }
47+ this . size ++ ;
48+ return this . size ;
49+ } ;
50+ this . popLeft = function ( ) {
51+ if ( this . size == 0 ) return null ;
52+ const removedNode = this . left ;
53+ this . left = this . left . next ;
54+ if ( this . left ) this . left . prev = null ;
55+ this . size -- ;
56+ return removedNode ;
57+ } ;
58+ } ;
59+
60+ var maxSlidingWindow = function ( nums , k ) {
61+ const output = [ ] ;
62+ let deque = new Deque ( ) ;
63+ let left = 0 ;
64+ let right = 0 ;
65+
66+ while ( right < nums . length ) {
67+ // pop smaller values from q
68+ while ( deque . right && nums [ deque . right . value ] < nums [ right ] ) deque . popRight ( ) ;
69+ deque . pushRight ( right ) ;
70+
71+ // remove left val from window
72+ if ( left > deque . left . value ) deque . popLeft ( ) ;
73+
74+ if ( right + 1 >= k ) {
75+ output . push ( nums [ deque . left . value ] ) ;
76+ left ++ ;
77+ } ;
78+ right ++ ;
79+ } ;
80+ return output ;
81+ } ;