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

Commit47f3fb2

Browse files
committed
Added JS 239-Sliding-Window-Maximum
1 parent73407d7 commit47f3fb2

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} k
4+
*@return {number[]}
5+
*/
6+
functionNode(value){
7+
this.value=value;
8+
this.prev=null;
9+
this.next=null;
10+
};
11+
12+
functionDeque(){
13+
this.left=null;
14+
this.right=null;
15+
this.size=0;
16+
this.pushRight=function(value){
17+
constnode=newNode(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+
returnthis.size;
28+
};
29+
this.popRight=function(){
30+
if(this.size==0)returnnull;
31+
constremovedNode=this.right;
32+
this.right=this.right.prev;
33+
if(this.right)this.right.next=null;
34+
this.size--;
35+
returnremovedNode;
36+
};
37+
this.pushLeft=function(value){
38+
constnode=newNode(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+
returnthis.size;
49+
};
50+
this.popLeft=function(){
51+
if(this.size==0)returnnull;
52+
constremovedNode=this.left;
53+
this.left=this.left.next;
54+
if(this.left)this.left.prev=null;
55+
this.size--;
56+
returnremovedNode;
57+
};
58+
};
59+
60+
varmaxSlidingWindow=function(nums,k){
61+
constoutput=[];
62+
letdeque=newDeque();
63+
letleft=0;
64+
letright=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+
returnoutput;
81+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp