Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite9b2665

Browse files
committed
Update 0239-sliding-window-maximum.js
1 parente2aab2b commite9b2665

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

‎javascript/0239-sliding-window-maximum.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,60 @@ var maxSlidingWindow = function (nums, k) {
8080
}
8181
returnoutput;
8282
};
83+
84+
/**
85+
*@param {number[]} nums
86+
*@param {number} k
87+
*@return {number[]}
88+
*/
89+
90+
// Deque Implementation using Lazy Deletion
91+
classLazyDeletionDeque{
92+
constructor(){
93+
this.deque=[];
94+
this.leftIdx=0;
95+
}
96+
97+
isEmpty=()=>{
98+
returnthis.deque.length===this.leftIdx;
99+
};
100+
push=(num)=>{
101+
this.deque.push(num);
102+
};
103+
popFront=()=>{
104+
this.leftIdx++;
105+
};
106+
popBack=()=>{
107+
!this.isEmpty()&&this.deque.pop();
108+
};
109+
front=()=>{
110+
returnthis.deque[this.leftIdx];
111+
};
112+
back=()=>{
113+
returnthis.deque[this.deque.length-1];
114+
};
115+
}
116+
117+
varmaxSlidingWindowWithLazyDeletionDeque=function(nums,k){
118+
constdeque=newLazyDeletionDeque();
119+
constanswer=[];
120+
letleftWindow=0;
121+
for(letrightWindow=0;rightWindow<nums.length;rightWindow++){
122+
constrightNum=nums[rightWindow];
123+
while(!deque.isEmpty()&&rightNum>deque.back()){
124+
deque.popBack();
125+
}
126+
deque.push(rightNum);
127+
128+
if(rightWindow>=k-1){
129+
constdequeFront=deque.front();
130+
constleftNum=nums[leftWindow];
131+
if(leftNum===dequeFront){
132+
deque.popFront();
133+
}
134+
answer.push(dequeFront);
135+
leftWindow++;
136+
}
137+
}
138+
returnanswer;
139+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp